I don't know how to create a loop to repeat and quit the program

Mar 17, 2012 at 2:17am
I have to use do-loop to prompt the user either to repeat or to quit the program. I started the program with declaring the variable as character and i don't know if it's right or not.


char quit = q


do {
// prompt the user if he/she wants to play again
cout << "Enter q to quit the program" ;
cin >> quit;

}


whatelse should I add to this?
Mar 17, 2012 at 2:20am
Put

1
2
3
if(quit == "q" || quit == "Q"){
return 0;
}
Last edited on Mar 17, 2012 at 2:22am
Mar 17, 2012 at 2:22am
what about repeating the program?
Mar 17, 2012 at 2:24am
Use a while loop. So do:

1
2
3
4
5
6

while(quit != "q") {
 // prompt the user if he/she wants to play again
 cout << "Enter q to quit the program" << endl;
 cin >> quit;
}


If you use this you do not need the if statement i provided earlier.
Last edited on Mar 17, 2012 at 2:26am
Mar 17, 2012 at 2:33am
then will it automatically repeat the program unless I enter 'q'?
Mar 17, 2012 at 2:34am
Yes, it will keep asking you to enter q, then when you do it will quit. You can also just enter random crap and press enter and it will still keep running until q is entered.
Last edited on Mar 17, 2012 at 2:34am
Mar 17, 2012 at 2:38am
Alright, thank you so much!! =)
Mar 17, 2012 at 2:39am
Your welcome :D
Topic archived. No new replies allowed.