+ 1
Coding is hard
I Finally got my program to work to guess numbers (well it works as long as you don't guess 1) But when I run the EXE on my computer it all happens so fast it shuts down before you see anything so I attempted to add a while(true) with a break that asks for input to break. When I enter anything into the exit value it get stuck in an endless loop. Can anyone help with this? https://code.sololearn.com/c98eHh8lljYw/#cpp
9 ответов
+ 5
https://code.sololearn.com/c5vKUJ2siwxp/?ref=app
+ 4
instead of while(true) try using a boolean variable.
something like this should work.
bool quit = false;
while(!quit) {
// do stuff
// ask if user wants to quit
if(userquit == 1) {
quit = true; }
}
+ 4
You can just do an if statement like so
char userChoice;
cout << "Would you like to exit? Enter Y to exit";
cin >> userChoice;
if(userChoice=='Y' || userChoice == 'y') {
exit = true;
+ 3
you are accidently assigning false to exit in the if statement. you should use double equal signs. (line 64). the break statement will never be reached
+ 3
that should work. there is no need to assign a default value to userChoice though. but it is a good habit to get into.
dont forget the closing brace on the if statement like I did 😊
+ 1
how is this?
cout<<"Would you like to exit? Press y for yes and n for no: ";
char userChoice='n';
cin>> userChoice;
if(userChoice=='Y' or userChoice=='y'){
run=false;
+ 1
thanks for your help. it works flawlessly now. now I can move on with my life and learn the next chapter lol
0
I'm not sure how to get user input for the bool
0
like this? bool exit=false
while(!exit)
count<<"would you like to exit press y"";
char y=false cin. exit;
if(exit==y)
break;
?