+ 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

6th Jun 2017, 2:41 AM
Bryan
9 ответов
+ 5
https://code.sololearn.com/c5vKUJ2siwxp/?ref=app
6th Jun 2017, 3:11 AM
jay
jay - avatar
+ 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; } }
6th Jun 2017, 2:49 AM
jay
jay - avatar
+ 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;
6th Jun 2017, 1:04 PM
jay
jay - avatar
+ 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
6th Jun 2017, 2:57 AM
jay
jay - avatar
+ 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 😊
6th Jun 2017, 1:17 PM
jay
jay - avatar
+ 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;
6th Jun 2017, 1:11 PM
Bryan
+ 1
thanks for your help. it works flawlessly now. now I can move on with my life and learn the next chapter lol
6th Jun 2017, 1:22 PM
Bryan
0
I'm not sure how to get user input for the bool
6th Jun 2017, 12:41 PM
Bryan
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; ?
6th Jun 2017, 12:44 PM
Bryan