# Number Guessing

• March 4th, 2013, 03:05 PM
Dash535
Number Guessing
Hey,

I'm going through the book "Programming: Principles and Practice Using C++" and I am currently on chapter four. I've to make a program where the users chooses a number and the computer is to guess it. One of the rules is it is to guess in no more than seven tries.

Here is my code:

Code:

```#include <iostream> #include <string> using std::cin; using std::cout; using std::string; int main(  ) {         int lowerBound = 1;         int median = 0;         int upperBound = 100;         string answer;         // Run through loop until the difference between bounds is 2         while ( upperBound - lowerBound >= 2 )         {                 // Calculate halfway point of range                 median = ( lowerBound + upperBound ) / 2;                 cout << "Is your number between " << lowerBound << " and " << median << "? ";                 cin >> answer;                 if ( answer == "y" || answer == "Y" )                 {                         // Number is within range                         upperBound = ( lowerBound + upperBound ) / 2;                 }                 else                 {                         // Number is not within range                         lowerBound = ( lowerBound + upperBound ) / 2;                 }         }         // Now there is two numbers, it is either either of them         cout << "Is your number << " << lowerBound << "? ";         cin >> answer;         if ( answer == "y" || answer == "Y" )         {                 // Number is the lower bound                 cout << "\n\nAnswer is: " << lowerBound << "\n\n";         }         else         {                 // Number is the upper bound                 cout << "\n\nAnswer is: " << upperBound << "\n\n";         }     return 0; }```
It guess with 8 or 9 tries. How could I modify it to decrease the number of tries?

Thanks,

Dash535
• March 4th, 2013, 05:29 PM
2kaud
Re: Number Guessing
Quote:

How could I modify it to decrease the number of tries?
By changing the code to be correct!:)

You're got several problems with the code which is why it can take more than 7 guesses. As you are trying this as an exercise, I'll first just give you some hints to see if you can work it out for yourself. You will learn more if you can figure it out for yourself rather then me just telling you the answer as the problem is logic not your use of the c++ language.

The first problem is that if you say a number is not between a range, the starting number of the new range is the same as the ending number of the previous range - when it should be one more.

Secondly, the while loop test is wrong. You need to be be testing that the range displayed is not the same.

Give it another try, step through your new code with the debugger if it still doesn't do it in 7 or less and see if you can figure it out.

Incidentially do you know why the max number of guesses is 7? (Hint it would still be 7 if the max number was 128).

If you still can't with the hints above, come back to us with your revised code and I'll give you more hints.

What do you think of Stroustrup's book?
• March 5th, 2013, 06:40 AM
Dash535
Re: Number Guessing
2kaud, thanks for your reply; It has been immensely helpful. I much prefer been pointed in the right direction, than being told the answer.

This problem has been annoying me for a few days now, but so far I've been going round in circles.

I've made a few changes. The most notable change is the adding of one to the lowerBound when it is changed.

I wasn't exactly sure how to change the test for the while loop. Would I have to add two variables that contain the previous bounds and then compare the new bounds to them?

Another change i made was to add a counter to the loop, to keep track of the number of tries.

I've run this quite a few times, and it gets it in seven tries.

Code:

```// Application.cpp #include <iostream> #include <string> using std::cin; using std::cout; using std::string; int main(  ) {         int count = 0;         int lowerBound = 1;         int median = 0;         int upperBound = 100;         string answer;         // Run through loop until the difference between bounds is 2         while ( true )         {                 // Calculate halfway point of range                 median = ( lowerBound + upperBound ) / 2;                 // Keep a track of how many tries                 ++count;                 if ( lowerBound == median )                 {                         // Now there is two numbers, it is either either of them                         cout << "Is your number << " << lowerBound << "? ";                         cin >> answer;                         if ( answer == "y" || answer == "Y" )                         {                                 // Number is the lower bound                                 cout << "\n\nAnswer is: " << lowerBound << "\n";                         }                         else                         {                                 // Number is the upper bound                                 cout << "\n\nAnswer is: " << upperBound << "\n";                         }                         break;                 }                 else                 {                         cout << "Is your number between " << lowerBound << " and " << median << "? ";                         cin >> answer;                         if ( answer == "y" || answer == "Y" )                         {                                 // Number is within range                                 upperBound = median;                         }                         else                         {                                 // Number is not within range                                 lowerBound = median + 1;                         }                 }         }         cout << "\nTries: " << count << "\n\n";     return 0; }```
I'd like to modify the loop to how you previously suggested, to see how that affects things.

As far as the book goes, it is the best book i've read yet on C++. I've bought many books and get disheartened quite quickly with them. My thought is that the Stroustrup book contains many exercises for you to do, and it is the trying to solve problems that is making things sink in.