Trying to create an algorithm that an ATM would use, but after selecting a choice to enter card number or exit, the code keeps looping back to this question. Still new to C++ and this has me stumped. I believe that my issue is at the beginning of the code because I can't get far into the program before it starts to loop.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// abstract class
struct Person
{
string name;
virtual void printName() = 0;
};
//Account class
struct Account : public Person
{
long cardNumber;
int pin; // 4 digit pin
double balance;
Account()
{
cardNumber = 0;
pin = 1234;
balance = 0;
}
Account(long c, int p, double bal)
{
cardNumber = c;
pin = p;
balance = bal;
}
void withdraw()
{
double amount;
cout << "Enter amount less than " << balance << ": ";
cin >> amount;
balance -= amount;
checkBalance();
}
void checkBalance()
{
cout << "Your balance is: " << balance << endl;
}
void changePin()
{
int Pin;
cout << "ENTER NEW PIN: ";
cin >> Pin;
pin = Pin;
cout << "PIN CHANGED \n";
}
void printName()
{
cout << "Name of Person: ";
cout << name << endl;
}
};
struct ATM
{
vector<Account> accounts;
void Login()
{
long cardNumber;
cout << "Enter cardNumer: ";
cin >> cardNumber;
for (int i = 0; i < accounts.size(); ++i)
{
if (cardNumber == accounts[i].cardNumber)
{
long pin;
cout << "Enter PIN: ";
cin >> pin;
if (pin == accounts[i].pin)
{
int choice = 0;
cout << "1. WITHDRAW\n";
cout << "2. CHECK BALANCE\n";
cout << "3. CHANGE PIN\n";
cout << "4. EXIT\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: accounts[i].withdraw();
break;
case 2: accounts[i].checkBalance();
break;
case 3: accounts[i].changePin();
break;
case 4: cout << "THANK YOU FOR USING\n";
break;
default: cout << "NO A VALID CHOICE BYE!!\n";
}
}
else
cout << "WRONG PIN BYE!!";
}
}
}
};
int main()
{
int choice = 0;
ATM A;
Account temp;
temp.name = "John Doe";
temp.cardNumber = 123456789;
temp.pin = 1234;
temp.balance = 30000;
A.accounts.push_back(temp);
temp.name = "David Joe";
temp.cardNumber = 987654321;
temp.pin = 4321;
temp.balance = 10000;
A.accounts.push_back(temp);
while (choice != 2)
{
cout << "1. LOGIN\n";
cout << "2. EXIT\n";
cout << "ENTER CHOICE: ";
cin >> choice;
switch (choice)
{
case 1: A.Login();
break;
case 2: cout << "BYE!!\n";
break;
default: cout << "ENTER A VALID CHOICE\n";
}
}
}
Any help is much appreciated.