C++ Looping Problem

[/code]ive just started learning C++ at college and ive got this task to which means i have to create a program of how an actual ATM bank works. So far ive come up with this

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
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int main(){

	int menuChoice=0;
	int PIN=1234; //PIN is the name of the variable
	bool pin=true; 
	bool isLooping = false;
	

	cout<<"\n\tWelcome and please insert your card"<<endl<<endl;
	Sleep(1500);
	system("CLS");
	
	while(isLooping==false){
		cout<<"\n\tEnter your PIN"<<endl;
		cin>>PIN;
		system("CLS");
		
	
		if(PIN  == 1234){
			cout<<"\t\nPlease choose one of the following options:"<<endl<<endl;
			cout<<"\t\n	1.Cash Out"<<endl;
			cout<<"\t\n	2.Statement"<<endl;
			cout<<"\t\n	3.Other options"<<endl;
			cin>>menuChoice;
			isLooping = true;
			system("CLS");
		}
		else if (PIN>=false){
			cout<<"\n\tIncorrect"<<endl;			
		}
		
		if(menuChoice==1){
			cout<<"\n\tYour balance today is: 9999.99\t"<<endl;
			Sleep(1500);

				cout<<"\n\tContinue?"<<endl;
				cout<<"\n\t >> 1.Yes <<"<<endl; 
				cout<<"\n\t >> 2.No <<"<<endl;
				cin>>menuChoice;
				system("CLS");
			

				if(menuChoice==1)
				{
					cout<<"\n\t >>Withdraw Amount<<"<<endl;
					Sleep(1000);
					cout<<"\n\t\t 1.5 Pounds"<<endl;
					Sleep(200);
					cout<<"\n\t\t 2.10 Pounds"<<endl;
					Sleep(200);
					cout<<"\n\t\t 3.20 Pounds"<<endl;
					Sleep(200);
					cout<<"\n\t\t 4.30 Pounds"<<endl;
					Sleep(200);
					cout<<"\n\t\t 5.40 Pounds"<<endl;
					Sleep(200);
					cout<<"\n\t\t 6.Triple Digit Value(000's)"<<endl;
					Sleep(2000);
				}
				
				if(menuChoice==2)
				{
					cout<<"\n\t Thank You for using our services"<<endl;
					system("\n\tpause");
					//cin>>menuChoice;
				}
				return 0;
				
		}

			if(menuChoice==2){
				cout<<"\n\tPrinting Statement"<<endl;
				Sleep(1500);
				cout<<"\n\tPrinting Complete"<<endl;
				Sleep(1500);
				cout<<"\n\tContinue?"<<endl;
				cout<<"\n\t 1.Yes"<<endl; 
				cout<<"\n\t 2.No"<<endl;
				cin>>menuChoice;
				system("CLS");
				

				

				/*if(menuChoice==1){
					cout<<"\t\nPlease choose one of the following options:"<<endl<<endl;
					cout<<"\t\n	1.Cash Out"<<endl;
					cout<<"\t\n	2.Statement"<<endl;
					cout<<"\t\n	3.Other options"<<endl;
					cin>>menuChoice;
					//isLooping = true;//
					system("CLS");
					}*/

			}
	}


		cin.get();
		cin.get();
}

/*

while(){

	
}


} */



what i want to do is after the part where it says continue and you either press 1 or 2 it should take you back to the main menu instead of having to recode everything again.

i know i need to use some sort of a loop but i dnt know how...any ideas??? also can you guys make it quite easy to understand.
Last edited on
Can you recopy your code back here into code tags so it is not a pain to look at >.<?
hey are at the bottom, and they end up as: [codea] Your code goes here [/codea], but without the a's in them.
Last edited on
I dont really get what you mean
Just highlight your code and click the "<>" button under Format.
you frm brighton?? its ur tag see.
kay what you can do is have lots of functions, so you can call them in a loop in main, you can have a loop within a loop too.

soo


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

function 1 

function 2

main ()
{


while (running = true)
{

menu

function 1


if(input==2)
{


function 2

}
input = 0


}




}






my psudo code is a bit lazy but you see what i did there, you can manage quite a lot with loos

loops even
Last edited on
@khm3dia

I think what devonrevenge is trying to say is use a switch statement that has a case for each menu option. From each case call a function that carries out that menu option. That will cut down on repeatedly testing the menu option variable.

Also put the whole into a loop like this:

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
#include <iostream>
#include <locale>  //need this for std::toupper

//using namespace std   <--- don't do this

using std::cin;   //either do this, or put std:: before each std thing
using std::cout;  //I do this for things used a lot
using std::endl;  //then use std:: for things that are not

//Put function declarations for each menu option  here


int main() {

     bool Quit = false;
     char answer = 'N';

    while(!Quit) {

           //your code here

//function to show the menu
//get the menu option
//switch statement to process menu options

           cout << "Do you want to Quit? Y or N" << endl;
           cin >> answer;
          
           answer = std::toupper(answer);  //avoids testing answer twice
           if(answer == 'Y')  //test variable once
                 Quit = true;
    }

return 0;  //if all is well, something else if not
} //end of main

//definitions of functions here


HTH
Topic archived. No new replies allowed.