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?
#include <iostream>
usingnamespace 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++
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.
#include <iostream>
usingnamespace 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;
}