ATM program loop trouble

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, do while...... , 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 
Last edited on
ok i replaced the whiles with do-whiles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cmath>
#include <string>

using namespace 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;
					
				}
				else if (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);
		}
		else if (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.
Last edited on
you need to reset depositMenu!? as default in your case is 3... LOL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

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";
	
	cout << "How can I help you today?" << endl
		<< "	1) Deposit money" << endl
		<< "	2) Check your balance" << endl
		<< "	3) Nothing. Exit" << endl;
	cin >> mainMenu;

	while (mainMenu != 3)
	{
		if (mainMenu == 1)
		{
			cout << "Thank you for depositing funds today" << endl
				<< "Please make a selection" << endl
				<< "	1) Deposit into checking" << endl
				<< "	2) Deposit into savings" << endl
				<< "	3) Back to main menu" << endl;
			cin >> depositMenu;
			
			while (depositMenu != 3)
			{
				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;
					depositMenu = 3; //here
					
				}
				else if (depositMenu == 2)
				{
					cout << "We are in your savings account" << endl
						<< "Please enter ammount you wish to deposit ";
					cin >> myDollar;
					cout << endl;
					sBalance = sBalance + myDollar;
					depositMenu = 3; //here
				
				}
				else 
				{
					cout << "Invalid entry. Try again" << endl;	
					depositMenu = 3; //here
				}
			}
		}
		else if (mainMenu == 2)
		{
			cout << "Checking balance" << endl
				<< "Your checking account = " << cBalance << endl
				<< "Your savings account = " << sBalance << endl;
		}
		else
		{
			cout << "Invalid entry. Try again" << endl;
		}
	}

	return 0;
}
Last edited on
cuz it's a logic error!! wait I will show you
Last edited on
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.
Last edited on
the message appears cuz 3 is part of that scope >>> else

i replaced as below take a look!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cmath>
#include <string>

using namespace 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;
					
				}
				else if (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 if (depositMenu != 3) //<<<<<<<<<<<<here
				{
					cout << "Invalid entry. Try again" << endl;	
				}

			} while (depositMenu != 3);
		}
		else if (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 if (mainMenu != 3) //<<<<<<<<<<<<here
		{
			cout << "Invalid entry. Try again \n\n";
		}
	} while (mainMenu != 3);

	return 0;
}
Last edited on
@joneele now the "invalid message" doesnt show up at all for
1
2
3
4
else if (mainMenu != 3) //<<<<<<<<<<<<here
		{
			cout << "Invalid entry. Try again \n\n";
		} 

theres gotta be something i missed in the chapter.
Last edited on
haha im stuuupid
i typed in
1
2
3
4
		else if (deposit != 3)
		{
			cout << " Invalid entry. Try again\n\n";
		}

instead of
1
2
3
4
		else if (mainMenu != 3)
		{
			cout << " Invalid entry. Try again\n\n";
		}

ps so its not always necessary to have an else statement to follow an if statement? i always thought you ended an if statement with an else statement.
Last edited on
I do not think you r stupid at all ~~~
have u been drinking svedka?
Last edited on
Topic archived. No new replies allowed.