So im doing an ATM programming project assigned by my teacher and im stuck. basically the program is to loop until the user wants to exit. i know im supposed to use a while loop. but i cant get the program to loop back to the main menu, it just loops at one point. for example when the user wants to see their account balances the program just loops the balances over and over and instead of showing them once then going back to the main menu.
it could be much easier if you attached the code, maybe you have a logic error, however have you tried goto for going back to the main menu?
over and over and instead of showing them once then going back to the main menu.
I dunno what kinda loops u r using but this is just an example bty there many ways to do that by using if, dowhile...... , in the loop you can do something like this
1 2 3 4 5 6 7 8
START:
I = 0; << default is zero
.....
.....
++I << loop counter
if(I == 1){goto START;} <<loop for one time
#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
void showMenu ();
int main ()
{
int mainMenu, depositMenu;
double myDollar, cBalance(0), sBalance(0);
cout << "=========================================================" << endl
<< " Welcome to MyBank ATM" << endl
<< " Your Friend in the Bussiness Enviroment" << endl
<<"=========================================================\n\n";
do
{
cout << "How can I help you today?" << endl
<< " 1) Deposit money" << endl
<< " 2) Check your balance" << endl
<< " 3) Nothing. Exit" << endl;
cin >> mainMenu;
if (mainMenu == 1)
{
cout << "Thank you for depositing funds today" << endl;
do
{
cout << "Please make a selection" << endl
<< " 1) Deposit into checking" << endl
<< " 2) Deposit into savings" << endl
<< " 3) Back to main menu" << endl;
cin >> depositMenu;
if (depositMenu == 1)
{
cout << "We are in your checking account" << endl
<< "Please enter ammount you wish to deposit = ";
cin >> myDollar;
cout << endl;
cBalance = cBalance + myDollar;
}
elseif (depositMenu == 2)
{
cout << "We are in your savings account" << endl
<< "Please enter ammount you wish to deposit = ";
cin >> myDollar;
cout << endl;
sBalance = sBalance + myDollar;
}
else
{
cout << "Invalid entry. Try again" << endl;
}
} while (depositMenu != 3);
}
elseif (mainMenu == 2)
{
cout << "Checking balance" << endl
<< "Your checking account = " << cBalance << endl
<< "Your savings account = " << sBalance << "\n\n"
<< "What would you like to do next?" << endl;
}
else
{
cout << "Invalid entry. Try again \n\n";
}
} while (mainMenu != 3);
return 0;
}
now my problem i think is at line 55. when i hit 3 it should just go back to the main menu, but instead it displays the "invalid entry. try again" message and then goes back to the main menu. Also at the end when i want to exit the program by pressing 3 the same message show up.
if i reset the depositMenu to 3 at the end of each case everything will go to the main menu. i only want to go back to the main menu when i hit 3. the only time i want the "invalid entry" message to appear is when a number that is not a 1,2, or 3 is entered.