Guessing game, and quitting it whenever

Hi guys, I created a guessing game, but I need to enhance it now to allow myself to keep guessing the number until it is guess correctly, or i choose to quit the program.

 12345678910111213141516171819202122232425262728293031323334353637383940414243 // William Lockard // Assignment 4 #include #include #include #include using namespace std; int numGen() { srand(time(0)); int randomnum =rand()%100+1; return randomnum; } int main() { int guess = numGen(); numGen(); while(1) { cout << "Guess a number between 1 and 100 " <> guess; if (guess > numGen()) { cout << "Your guess is too high. Enter another number " << endl; } else if (guess < numGen()) { cout << "Your guess is too low. Enter another number " << endl; } else { cout << "Congratulations that is the correct guess" << endl; return 0; } } }

This works fine, I am just having trouble using do-while loops and char values to allow the user to input "Quit" in the middle of the code if they wish to quit.
Could someone give me an idea how to go about it? I will attach what I've tried

I'm getting really frustrated, I think I'm having trouble where to put the {} but any help is greatly appreciated. Ive looked at other similar problems, and in my book and I think I'm setting up the do-while correctly
Last edited on
 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 #include #include #include #include using namespace std; int numGen() { srand(time(0)); int randomnum =rand()%100+1; return randomnum; } int main() { do{ char answer; int guess = numGen(); numGen(); while(1) { cout << "Guess a number between 1 and 100 " <> guess; if (guess > numGen()) { cout << "Your guess is too high. Do you want to continue? (Y/N)" << endl; cin >> answer; cout << "Guess again"; } else if (guess < numGen()) { cout << "Your guess is too low. Do you want to continue? (Y/N) " << endl; cin >> answer; cout << "Guess again"; } else { cout << "Congratulations that is the correct guess" << endl; return 0; } }} while (answer == 'Y' || answer == 'y');}

I'm completely lost. Just looking for some guidance. I've been out of power from the hurricane and trying to finish my homework for tonight
Here is a better way to quit out of something:

 123456789101112 bool Quit = false; while(!Quit) { cout << " Do you want to Quit? (q or Q) " << endl; cin >> answer; answer = std::toupper(answer); if (answer == 'Q') Quit = true; }

The use of toupper avoids the testing of the variable twice.

Also avoid the use of infinite loops - there are situations where this is valid, but this isn't one of them.

I also personally hate do loops - I find they are rarely needed, you can nearly always write a while or for loop instead.
I have no idea what toupper even is, it's not even recognizing it in visual studio. I'm sorry I'm in a basic c++ and havent learned it yet. I wouldn't even have any idea where to put that into my original code. I'm assuming the first line at the begining, but then lines 4-9 need to be added to each step?

I need to have it ask if it wants to quit after every guess.
Last edited on
std::toupper converts a char from lowercase to upper case.

 12345678910111213141516171819202122232425 bool Quit = false; while(!Quit) { if (guess > numGen()) { } else if (guess < numGen()) { } else { cout << "Congratulations that is the correct guess" << endl; return 0; } cout << " Do you want to Quit? (q or Q) " << endl; //you figure out where to put this code cin >> answer; answer = std::toupper(answer); if (answer == 'Q') Quit = true; }

You can fill in the rest of the code.
Last edited on
toupper is not a member of std

thats the error i'm getting. other than that, it looks ok
I'm an idiot. I got it all worked out. Thanks a ton for your help, you dont know how much I appreciate it in such short notice
 http://www.cplusplus.com/reference/std/locale/toupper/

I forgot to mention you need the locale header.
Topic archived. No new replies allowed.