Almost done! I just need help looping back to the start.

I've got my ATM machine running pretty smoothly except for enabling the user to retry entering their pin. If it's incorrect the program just shuts down but I'd like to have it loop back to try again. Suggestions or advice?

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

void showMenu ();
int main ()
{
  int mainMenu, depositMenu, withdrawMenu; 
	double myDollar, cBalance(1000), sBalance(1000), pin, acctnumber;

	cout <<  "=========================================================" << endl
		<< "Welcome to Ricardo's ATM" << endl 
		<< "Your banking is our business! " << endl
		<<"=========================================================\n\n";
    
  cout << "Please enter your account number: ";  //Prompts the user to input their account number (simulates card swipe).
  cin >> acctnumber;
  cout << endl;
  
  cout << "Please enter your pin number: ";  //Prompts the user to input their pin number.
  cin >> pin;
  cout << endl;

if (pin == 1234)
{
	do
	{
	  cout <<  "=========================================================" << endl;
    cout << "How can I help you today?" << endl //main menu
			<< "	1) Deposit money" << endl
			<< "	2) Check your balance" << endl
			<< "	3) Withdraw money" << endl
      << "	4) Nothing. Exit" << endl;
    cout <<  "=========================================================" << endl;
		cin >> mainMenu;

		if (mainMenu == 1)
		{
			cout << "Thank you for depositing funds today" << endl; //depositing menu
			do
			{
		  cout <<  "=========================================================" << endl;
      cout << "Please make a selection" << endl
					<< "	1) Deposit into checking" << endl
					<< "	2) Deposit into savings" << endl
					<< "	3) Back to main menu" << endl;
      cout <<  "=========================================================" << endl;
				cin >> depositMenu;
				if (depositMenu == 1)
				{
					//Depositing into your checking account.
					cout << "Your checking account has been successfully accessed!" << endl
						<< "Please enter amount you wish to deposit = ";
					cin >> myDollar;
					cout << endl;
					cBalance = cBalance + myDollar;
					
				}
				else if (depositMenu == 2)
				{
					//Depositing into your savings account.
					cout << "Your savings account has been successfully accessed!" << endl
						<< "Please enter amount you wish to deposit = ";
					cin >> myDollar;
					cout << endl;
					sBalance = sBalance + myDollar;
				
				}
				else if (depositMenu != 3) 
				{
					cout << "Invalid entry. Try again" << endl;	
				}

			} while (depositMenu != 3);
		}
      if (mainMenu == 3)
		{
			cout << "Thank you for withdrawing funds today" << endl;
			do
			{
			//Withdrawing menu.
		  cout <<  "=========================================================" << endl;
      cout << "Please make a selection" << endl
					<< "	1) Withdraw from checking" << endl
					<< "	2) Withdraw from savings" << endl
					<< "	3) Back to main menu" << endl;
      cout <<  "=========================================================" << endl;
				cin >> withdrawMenu;
				if (withdrawMenu == 1)
				{
				//Withdrawing from your checking account.
					cout << "Your checking account has been successfully accessed!" << endl
						<< "Please enter amount you wish to withdraw = ";
					cin >> myDollar;
					cout << endl;
					cBalance = cBalance - myDollar;
					
				}
				else if (withdrawMenu == 2)
				{
				//Withdrawing from your savings account.
					cout << "Your savings account has been successfully accessed!" << endl
						<< "Please enter amount you wish to withdraw = ";
					cin >> myDollar;
					cout << endl;
					sBalance = sBalance - myDollar;
				
				}
				else if (withdrawMenu != 3) 
				{
					cout << "Invalid entry. Try again" << endl;	
				}

			} while (withdrawMenu != 3);
		}
		else if (mainMenu == 2)
		{
		//Option #2 Showing your balances.
      cout <<  "=========================================================" << endl;
			cout << "Your balances are displayed below: " << endl
				<< "Your checking account = " << cBalance << endl
				<< "Your savings account = " << sBalance << "\n\n"
				<< "What would you like to do next?" << endl;
      cout <<  "=========================================================" << endl;
		}
		else if (mainMenu != 4) 
		{
			cout << "Invalid entry. Try again \n\n";
		}
	} while (mainMenu != 4);
}
else 
  {
    cout << "Invalid pin number entered.";
  } 
	return 0;
}
have you considered the while loop?
Yes, my problem is getting it to back to the beginning to try again after:

1
2
3
4
5
6
else 
  {
    cout << "Invalid pin number entered.";
  } 
	return 0;
}
Consider putting pin entry in it's own function. The pin entry function loops until the user enters a correct pin, or has tried too many times. Pin entry returns either true (pin is good) or false (too many tries). Program exits if pin entry returns false.
closed account (j3Rz8vqX)
Suggestion:
Encapsulate line 21 to 137 in a do-while loop; while pin not equal to 1234.
for clarity, I suggest moving the specific cases (options you have for menu items) into specific functions for processing.

i.e. function for depositing money, function for checking balance, function for withdraw money

Topic archived. No new replies allowed.