nested loop for begginers like me

I'm trying to make this program loop until the customer decides that they're finished
I have one small problem in my code but I can't figure it out
what am i doing wrong?

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
#include <iostream>
using namespace std;

int main()
{
	int Answer, AllDone = 1;
	char CreditCardNumber[40];
	
	
	do
   {
	
	cout<<"if your ready to enter credit card number: "<<endl; 
	cout<<"(Enter: [1 for yes]- [2 for no])";
	cin>>Answer;
	cin.sync();
	cin.get ();
	
	if (Answer==1)
      { 
		cout<<"ready to process transaction\n";
		cout<<"please enter credit card number";
		cin>>CreditCardNumber;
		cin.sync();
		cin.get();
       } else
	//between this else and while is where the problem beggins
	//heres where it loops back to the beggining without displaying the rest
   }while (AllDone==1)
	   
			cout<<"would you like another transaction?"<<endl;
			cout<<"(Enter [1 for yes] - [2 for no])";
			cin>>AllDone;
			cin.sync();
			cin.get();
			
			
	   
// I want it to loop back to the top only if they have another transaction*******
			
	{ cout<<"thank you!!!";
	}
	         
 return 0;
}


what is my problem here???

P.S. this isn't homework I'm just trying to relearn C++
try adding {AllDone=0;}after the else, see if it works.
Last edited on
Personally I think you're being fancier with your brackets than you should be if you're struggling with nesting.

May I suggest you try using something like:
1
2
3
4
5
6
7
8
9
10
bool flag = true;

do{

cout << "do thangs";

if (thangs are done)
bool flag = false;

}while(flag)


You say the problem is that it loops back to the beginning - this is how a loop function works. Once you have reached the (while) and the condition is true, the loop repeats from the beginning. Meaning you have no exit condition inside your loop.
Last edited on
finally got it to work
Thank You everybody

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
#include <iostream>
using namespace std;

int main()
{
	int Answer, AllDone = 1;
	char CreditCardNumber[40];
	
	
	do
    {
	
	cout<<"if your ready to enter credit card number: "<<endl; 
	cout<<"(Enter: [1 for yes]- [2 for no])";
	cin>>Answer;
	cin.sync();
	cin.get ();
	
	     if (Answer==1)
	    { 
		cout<<"ready to process transaction\n";
		cout<<"please enter credit card number";
		cin>>CreditCardNumber;
		cin.sync();
		cin.get();
	    }
	//between this else and while is where the problem beggins
	//heres where it loops back to the beggining without displaying the rest
    
	   
			cout<<"would you like another transaction?"<<endl;
			cout<<"(Enter [1 for yes] - [2 for no])";
			cin>>AllDone;
		
			cin.get();
	}while (AllDone==1);
			
	   
// I want it to loop back to the top*******
			
	   { cout<<"thank you!!!";
	   cin.get();
	   }
	         
	return 0;
}
Topic archived. No new replies allowed.