I want to create an ATM simulation for a school project and the program is working alright until I try to find out what happens when I input all the wrong things (for if-else statements).
When it reaches the last one, the program somehow skips over the getline code and comes back to it when the if-statements returns false, which will always return false on the first run since I couldn't even type in the answer yet. (program isn't fully completed yet so some switch cases are not considered)
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void verified()
{
cout << "\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tAccount Verified!";
}
void notverified()
{
cout << "\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tInvalid PIN number.";
}
int withdraw(int a, int b)
{
int withdraw = 0;
withdraw = a - b;
return withdraw;
}
int main()
{
string pin, MainAccount, accNum1, accNum2, accNum3, pin1, pin2, pin3;
string yesno, yes, no;
int options;
float balance, withdrawAmount, depositAmount, finalWD;
accNum1 = "193763896";
accNum2 = "193549879";
accNum3 = "193285015";
pin1 = "092703";
pin2 = "122903";
pin3 = "122003";
cout << "\t\t\tATM Simulation";
cout << "\n-------------------------------------------------------------------";
cout << "\n\t\tWelcome to the ATM Simulation!";
cout << "\n-------------------------------------------------------------------";
cout << "\n\nPlease enter your Account Number\t: ";
getline(cin, MainAccount);
if (MainAccount == accNum1)
{
cout << "\nPlease enter your PIN\t\t\t: ";
getline(cin, pin);
if (pin == pin1)
verified();
else
{
notverified();
return 0;
}
}
else if (MainAccount == accNum2)
{
cout << "\nPlease enter your PIN\t\t\t: ";
getline(cin, pin);
if (pin == pin2)
verified();
else
{
notverified();
return 0;
}
}
else if (MainAccount == accNum3)
{
cout << "\nPlease enter your PIN\t\t\t: ";
getline(cin, pin);
if (pin == pin3)
verified();
else
{
notverified();
return 0;
}
}
else
{
cout << "\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tAccount not recognised.";
return 0;
}
if (MainAccount == accNum1)
balance = 5000;
else if (MainAccount == accNum2)
balance = 12000;
else if (MainAccount == accNum3)
balance = 18000;
TRANSACTION:
cout << "\n\n------------------------------Options------------------------------";
cout << "\n\nBalance Inquiry (1)";
cout << "\nCash Withdrawal (2)";
cout << "\nDeposit (3)";
cout << "\nChange Pin (4)";
cout << "\nAbout us (5)";
cout << "\nExit (6)";
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nPlease select a transaction: ";
cin >> options;
switch (options)
{
case 1:
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tBalance Inquiry";
cout << "\n\nYour account balance is " << fixed << setprecision(2) << balance << " php.";
break;
case 2:
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tCash Withdrawal";
WITHDRAW:
cout << "\n\nPlease enter the amount you wish to withdraw (Max amount is 5000.00 php per transaction): ";
cin >> withdrawAmount;
if (withdrawAmount <= 20000)
{
if (MainAccount == accNum1)
finalWD = withdraw(balance, withdrawAmount);
else if (MainAccount == accNum2)
finalWD = withdraw(balance, withdrawAmount);
else if (MainAccount == accNum3)
finalWD = withdraw(balance, withdrawAmount);
}
else
{
cout << "\nWithdrawal amount exceeded. Please enter an amount lower than or equal to 5000.00 php.";
goto WITHDRAW;
}
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nWithdrawal successful. Your new balance is " << fixed << setprecision(2) << finalWD << " php.";
break;
case 3:
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tDeposit";
DEPOSIT:
cout << "\n\nPlease enter the amount you wish to deposit (Max amount is 20000.00 php per transaction): ";
cin >> depositAmount;
if (depositAmount <= 20000)
{
if (MainAccount == accNum1)
balance = balance + depositAmount;
else if (MainAccount == accNum2)
balance = balance + depositAmount;
else if (MainAccount == accNum3)
balance = balance + depositAmount;
}
else
{
cout << "\nDeposit amount exceeded. Please enter an amount lower than or equal to 20000.00 php.";
goto DEPOSIT;
}
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nDeposit successful. Your new balance is " << fixed << setprecision(2) << balance << " php.";
break;
case 4:
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\n\t\t\tChange Pin";
break;
case 6:
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nThank you for partnering with us. Please come again!";
return 0;
break;
}
YESNO:
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nMake another transcation? (y/n) : ";
getline(cin, yesno);
yes = 'y';
no = 'n';
if (yesno == yes)
{
goto TRANSACTION;
}
else if (yesno == no)
{
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nThank you for partnering with us. Please come again!";
cout << "\n\n-------------------------------------------------------------------";
}
else
{
cout << "\n\n-------------------------------------------------------------------";
cout << "\n\nPlease type a valid option.";
goto YESNO;
}
return 0;
}
So this is what the output looks like:
-------------------------------------------------------------------
Welcome to the ATM Simulation!
-------------------------------------------------------------------
Please enter your Account Number : 193763896
Please enter your PIN : 092703
-------------------------------------------------------------------
Account Verified!
------------------------------Options------------------------------
Balance Inquiry (1)
Cash Withdrawal (2)
Deposit (3)
Change Pin (4)
About us (5)
Exit (6)
-------------------------------------------------------------------
Please select a transaction: 1
-------------------------------------------------------------------
Balance Inquiry
Your account balance is 5000.00 php.
-------------------------------------------------------------------
Make another transcation? (y/n) :
-------------------------------------------------------------------
Please type a valid option.
-------------------------------------------------------------------
Make another transaction? (y/n) : (this is where you can start typing)
I've been trying to solve this for hours but can't find what's going on, can someone explain?