   Number Guess Code Review
So I have been following this book about C++ and one of the challenges was changing a game where you guess the computer's number to a game where the computer guesses your number. With a bit of help from Google, I managed to complete the game but have come across one issue. If someone wants to, they can crash the game by giving the computer no more numbers to choose from. Do you have any solutions on how I can stop the game from being broken?Is there a way the code can be improved?Thanks!// Guess My Number// The classic number guessing game#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){srand(static_cast<unsigned int>(time(0))); //seed random number generatorint guess = rand() % 100 + 1; //random number between 1 and 100int tries = 0;int highestNumber = 100;int lowestNumber = 1;char ready;char position;bool success;cout << "\tWelcome to Guess My Number";cout << "\n\nThink of a number between 1 and 100.";cout << "\nThe computer will try to guess your number.";cout << "\n\nAre you ready? (y/n): ";cin >> ready;do{cout << "\n\nMy Guess: " << guess << endl;cout << "\nHigh, Low or Success? (h/l/s): ";cin >> position;if (position == 'h'){highestNumber = guess - 1;guess = rand() % (highestNumber - lowestNumber + 1) + lowestNumber;success = false;}else if (position == 'l'){lowestNumber = guess + 1;guess = rand() % (highestNumber - lowestNumber + 1) + lowestNumber;success = false;}else if (position == 's'){cout << "\nWoo! I won!";success = true;}} while (success != true);return 0;}
if highestNumber == lowestNumber and they say the guess is wrong they have cheated so tell them that. EDIT: I don't know if it can happen in your code but if lowest > highest or highest < lowest that is another cheat on the user's part.;Thanks Paradigm Shifter, I will input that now and give it a whirl. EDIT: Where should I input the code? I tried it in the mix with the 'else if' but it didn't work.;Some brief observations:Your ready-variable is not used; the games starts even if the user is no ready. Not so much an issue with the design of the program from a coding point of view, but it is a surprise for the user.The success-variable is not necessary. It is only true when position is 's', so use that as a break condition.A quit option would be nice to break the game. Perhaps not so much in this particular case since the game is short enough to end within just a few moves on average, but in general you should consider it.You may want to move the win condition to after the loop instead. This can be argued though, since it depends on what you consider game loop logic. The win condition is not part of the loop but as a part of the exit condition, but on the other hand it is also a part of the  set of valid inputs during the game.A final else-statement taking care of invalid input would be nice so the user knows the input was invalid instead of wondering why the computer guessed the same number.;I tried following your coding style wherever I made changes. I'm guessing you haven't learned about functions yet, but this at least cuts down on some repeated code (notably, the let's guess a new number part).I removed all your comments, to more easily show where I made comments myself.// Guess My Number// The classic number guessing game#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){srand(static_cast<unsigned int>(time(0)));int guess = rand() % 100 + 1;int tries = 1; //Initialized to 1 instead of 0, guessing it in first = 1 guessint highestNumber = 100;int lowestNumber = 1;char ready; //This isn't really used currently.char position;bool success = false; //Initialize and don't worry about it if incorrect guessescout << "\tWelcome to Guess My Number";cout << "\n\nThink of a number between 1 and 100.";cout << "\nThe computer will try to guess your number.";cout << "\n\nAre you ready? (y/n): ";cin >> ready; //This is currently just ignored.do{cout << "\n\nMy Guess: " << guess << endl;cout << "\nHigh, Low or Success? (h/l/s): ";cin >> position;if (position == 's') //Checking success first. No need to do other stuff if we got it right{cout << "\nWoo! I won in " << tries << " attempts!"; //Added the tries to outputsuccess = true;}else //We didn't get it right{if (position == 'h' || position == 'l') //Did we get something too low or something too high?{if (position == 'h') //Too high, adjust highest{highestNumber = guess - 1;}else //Too low, adjust lowest{lowestNumber = guess + 1;}if (lowestNumber > highestNumber) //Check for lies{cout << "\nYou lied to me! I'm done playing...";success = true;}else //No lies detected, let's guess again{guess = rand() % (highestNumber - lowestNumber + 1) + lowestNumber;tries++;}}else //Character that isn't handled.{cout << "\nInvalid character input.";}}} while (success != true);return 0;}; Some brief observations:Your ready-variable is not used; the games starts even if the user is no ready. Not so much an issue with the design of the program from a coding point of view, but it is a surprise for the user.The success-variable is not necessary. It is only true when position is 's', so use that as a break condition.A quit option would be nice to break the game. Perhaps not so much in this particular case since the game is short enough to end within just a few moves on average, but in general you should consider it.You may want to move the win condition to after the loop instead. This can be argued though, since it depends on what you consider game loop logic. The win condition is not part of the loop but as a part of the exit condition, but on the other hand it is also a part of the  set of valid inputs during the game.A final else-statement taking care of invalid input would be nice so the user knows the input was invalid instead of wondering why the computer guessed the same number. Thanks for the tips. Just read through the code when I saw this and spotted quite a few things I didn't utilize at all.And after reading the new code CoreLactose suggested, I can see what you mean by how everything should be moved around.