So my professor says its a runtime error and I should play computer and go over it step by step. I know its something simple but I've spent far too much time going over this. Hopefully you guys can help.
Its a program for a bank atm. Works perfectly fine except for the fact that it repeats itself for withdrawals and deposits.
Here's a sample run.-------------------------------------
Welcome to Bank ATM.
What would you like to do?
Withdrawal(W)
Deposit(D)
Balance(B)
Quit(Q)
Balance
Your new balance is 5000
BWhat would you like to do?
Withdrawal(W)
Deposit(D)
Balance(B)
Quit(Q)
Withdrawal
How much money would you like to withdraw today?
598
Please collect your money. Your new balance is 4402
WWhat would you like to do?
Withdrawal(W)
Deposit(D)
Balance(B)
Quit(Q)
What would you like to do?
Withdrawal(W)
Deposit(D)
Balance(B)
Quit(Q)
And here's the source.-----------------------------------
#include <iostream>
#include <string>
using namespace std;
while ( !value)
{
cout << "What would you like to do?\n";
cout << "Withdrawal(W)\nDeposit(D)\nBalance(B)\nQuit(Q)" << endl;
getline(cin, choice);
char_choice = choice[0];
if ( (char_choice == 'Q') || (char_choice == 'q') )
{
cout << "\nThank you for using Bank ATM" << endl;
value = true;
}
else if ( (char_choice == 'W') || (char_choice == 'w') )
{
cout << "\nHow much money would you like to withdraw today?\n";
cin >> input;
if (input > balance)
{
cout << "\nYou do not have that much money in your account";
cout << "\nPlease enter a number less than or equal to ";
cout << balance << ": ";
}
else if (input < 0)
cout << "\nYou entered a negative number, please try again";
else
{
balance -= input;
cout << "\nPlease collect your money. Your new balance is ";
cout << balance << endl << endl;
}
}
else if ( (char_choice == 'D') || (char_choice == 'd') )
{
cout << "\nHow much money would you like to deposit today?";
cin >> input;
balance += input;
cout << "\nYour new balance is " << balance << endl << endl;
}
else if ( (char_choice == 'B') || (char_choice == 'b') )
{
cout << "\nYour new balance is " << balance << endl;
}
}
return(0);
}
Your program repeats continuously until the user selects to quit. But your char_choice variable continues to hold the value the user initially assigned to it. So if they select to make a deposit the code will run the first time as planned. Then it goes back to the top of the while loop reads through the code and sees the value already stored. Try clearing the buffer. Use cin.ignore (100, '\n') after the getline() function. This will clear the buffer and hopefully stop the repetition. I hope this helps.
1 2 3 4 5
cout << "What would you like to do?\n";
cout << "Withdrawal(W)\nDeposit(D)\nBalance(B)\nQuit(Q)" << endl;
getline(cin, choice);
cin.ignore (100, '\n');
char_choice = choice[0];
Also, just an idea. Try using a switch statement instead of all of the nested if statements. Nested if statements tend to look messy and are hard to read. A switch statement is much more concise and easier to understand. This is simply just a preference but will make your code much more readable.