### Guess My Number - Reverse Roles

Hey everyone.

I started reading the book "Beginning C++ Through Game Programming" and I've worked up to Chapter 2 so far. I have a few questions regarding the code that I have put together for the Guess My Number game. The computer is supposed to be guessing the number that I've thought of before hand, in which it does, but if you play the game enough, you'll notice that the computer will also guess the max and min values every once in a while, which doesn't make sense.

Also I was wondering if there are any parts of my code that I can clean up, or for some reason have included that doesn't have any effect on the code. I've included the entire game code below. I've included lines to show the change of the max and min values to help me during the coding process to make the sure the numbers were being replaced with each user input. I've been using 2012 Visual Studio Express for writing the code. Thank you.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````// Guess My Number // The classic number guessing game #include "stdafx.h" #include #include #include #include int main() { std::string userInput; srand(time(0)); // seed random number generator int max = 100; int min = 1; std::cout << "\tWelcome to Guess My Number\n\n"; do { int theNumber = rand() % (max - min) + min; // random number between max and min std::cout << "\nThe computer's guess is " << theNumber << std::endl; std::cout << "\nIs the number too high, too low, or correct? (high, low, correct): "; // get user input std::cin >> userInput; if (userInput == "high") // states that the userInput was high and replaces the max value { std::cout << "\nThe guess was too high. The new Max and Min are... \n\n"; max = theNumber; // replaces the computer's guess with the max value std::cout << "Max: " << max << std::endl; std::cout << "Min: " << min << std::endl; } if (userInput == "low") // states that the userInput was high and replaces the max value { std::cout << "\nThe guess was too low. The new Max and Min are... \n\n"; min = theNumber; // replaces the computer's guess with the min value std::cout << "Max: " << max << std::endl; std::cout << "Min: " << min << std::endl; } } // states that the computer guessed the number and ends the game while (userInput != "correct"); std::cout << "\nThe computer's guess was spot on. Good game! \n\n"; std::cout << "\n\nThanks for playing.\n\n"; return 0; }``````
Do you mean it guesses the original min and max or the new min and max? In the latter case, why doesn't that make sense? Consider the range of posible values the computer can guess.

By the way, I also have this book - unfortunately it was really disappointing because so much of it was in the console, it took too long to teach you how to move away from the console and make graphical games.
I should have been more clear. I've not seen the program guess 100 or 1 on the start but while playing the game if my max and min at the time are 67 and 63, it sometimes guesses with either 67 or 63. I guess what I would like to see would the max and min values NEVER be options once they have been already chosen.

I have noticed that a lot of the book is going to be in the console which is alright because I'm just learning, but what book do you suggest would be a good path from there?
There isn't really a book that teaches how to use a graphics library like SFML or SDL. I recommend looking up SFML.

As for your problem, look more carefully at the code you use to generate a guess - line 26 in the code you posted.
Maybe I'm missing on how to set the range because I was thinking just a simple subtraction of 1 from the max value and an addition of 1 to the min value would keep the random number generator from picking the max and min values. Any other ideas or hints to go from here?

Thanks
Did you check order of operations? Be careful when mixing addition and subtraction.
I haven't worked on "If Then" statements yet, but is there a way if the random number equaled either the max or min value, that it would go back and re-run the random equation?
What do you mean you haven't worked on "If Then" statements yet? You've got some on lines 33 and 44.

And no, you should not re-run the random equation. You should design your program to always guess in a valid range.

On lines 38 and 49, try condensing the range there instead.
I guess I meant to say that I've not used "then".

I think I figured out the random number generator equation that I've been looking for. I've run the program about 15 times now and I've hadn't had any guesses that were the max and min values. Here is the change in the code...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````// Guess My Number // The classic number guessing game #include "stdafx.h" #include #include #include #include int main() { std::string userInput; srand(time(0)); // seed random number generator int max = 100; int min = 1; int tries = 0; std::cout << "\tWelcome to Guess My Number\n\n"; do { int theNumber = rand() % (max - (min + 1)) + (min + 1); // random number between max and min ++tries; std::cout << "\nThe computer's guess is " << theNumber << std::endl; std::cout << "\nIs the number too high, too low, or correct? (high, low, correct): "; // get user input std::cin >> userInput; if (userInput == "high") // States that the userInput was high and replaces the max value { std::cout << "\nThe guess was too high. The new Max and Min are... \n\n"; max = theNumber; // replaces the computer's guess with the max value std::cout << "Max: " << max << std::endl; std::cout << "Min: " << min << std::endl; } if (userInput == "low") // states that the userInput was low and replaces the min value { std::cout << "\nThe guess was too low. The new Max and Min are... \n\n"; min = theNumber; // replaces the computer's guess with the min value std::cout << "Max: " << max << std::endl; std::cout << "Min: " << min << std::endl; } } // states that the computer guessed the number and ends the game while (userInput != "correct"); std::cout << "\nThe computer's guess was spot on. Good game!"; std::cout << "\nIt took " << tries << " attempts to guess your number.\n\n\n"; std::cout << "Thanks for playing.\n\n"; return 0; }``````

Line 27 is the change for the random number generator. I'd appreciate it if you ran it and see if you had any problems with it.

This article helped me out as well. Thanks a lot for the help and feedback L B.

http://www.cplusplus.com/reference/cstdlib/rand/
Mindtrix wrote:
I guess I meant to say that I've not used "then".
 ``1234`` ``````if(/* "if" */) { /* "then" */ }``````

Glad you got it to work ;)
Marked as solved.
Topic archived. No new replies allowed.