Having some trouble with my number guessing game. The object is that the user thinks of a number between 1 and 100 and the computer tries to guess it in a set number of tries. The user inputs an "l" or an "h" if the guess is too low or too high, respectively. My problem is switching between the two. The first guess is always 50, so if that's too low it guesses 75. If that's too low, it guesses 88. Which works fine, except if you pick, let's say, 60. First guess 50. press "l" next guess 75, press "h" next guess is 37.

I've tried creating a new variable in order to reset the computer's guess each time through the loop but with no luck. Any help in the right direction would be appreciated.
Here's the code:

#include <iostream>
#include <string> 

using namespace std;
const int NUMBER_OF_GUESSES = 5;

int main()
{
    string firstName;
    int computerGuess;
    int numGuesses = 1;
    char answer;
    int max;
    int min;

cout << "What is your first name?" << endl;

cin >> firstName;

cout << firstName << ", please think of a number between 1 and 100, and I will try to"   << endl; 
cout << "guess the number in 5 attempts." << endl << endl;

cout << "Press h if I've guessed too high or l if I've guessed too low." << endl << endl;

computerGuess = 50;

while (numGuesses <= 5)

{
    cout << "Is the number " << computerGuess << "?" << endl;

cin >> answer;
    
if (answer == 'l')
{
    max = 100;
    min = computerGuess + 1;
    computerGuess = (max + min)/2;
    
    numGuesses ++;
}

else if (answer == 'h')
{
    max = computerGuess - 1;
    min = 0;
    computerGuess = (max + 1)/2;
    
    numGuesses ++;
}
    }    
        return 0;
}

you are close -- delete lines 36 and 46. Then delete the +1 from line 37 and the -1 from line 45. and line 45 should be min = computerGuess Finally, line 47 calculation is incorrect.

computerGuess = ((max - min)/2 ) + min;

AD is right that lines 36 and 46 don't belong. Additionally, min and max should both be initialized to their respective values when you declare them in lines 13 and 14.

How does the game end if the computer actually guesses the number?


I would caution you about reusing this code to implement a Binary Search, which it closely resembles. For a search to be able to exit in the case of the target value not existing, the +1 and -1 must remain in lines 37 and 45, else an infinite loop can occur.

Val

you are close -- delete lines 36 and 46. Then delete the +1 from line 37 and the -1 from line 45. and line 45 should be min = computerGuess Finally, line 47 calculation is incorrect.

computerGuess = ((max - min)/2 ) + min;

Did all of that and had the same problem. However, changed line 45 to max = computerGuess and that did the trick. With those changes it left me with max as an undeclared variable. So I set the values for max and min to 100 and 1. Not sure if those messed with your suggestion. Thanks a lot for your help.


How does the game end if the computer actually guesses the number?

That's a good question. I wanted to get the math figured out first, then tackle the exit. Probably by adding 'c' as an option in the high/low question, and then another else if statement. But that will still cause it to echo print the last guess afterwards. And won't exit the loop. Any suggestions?

That's a good question. I wanted to get the math figured out first, then tackle the exit. Probably by adding 'c' as an option in the high/low question, and then another else if statement. But that will still cause it to echo print the last guess afterwards. And won't exit the loop. Any suggestions?

Fixed it I think. Added the following statements:

[LIST=1]
[*]else if (answer == 'c')
[*]{
[*]    cout << "Got it " << firstName << endl;
[*]    numGuesses = NUMBER_OF_GUESSES +1;
[*]}
[*]else
[*]{
[*]    cout << "Try again. Next time press l, h, or c." << endl;
[*]    numGuesses = NUMBER_OF_GUESSES +1;
[*]}[/LIST]

Exits the loop without reprinting the last guess and added another exit in case of user error.

Thanks again for your help AD and Val. Reps to both of you. I'll test it out tonight and mark it solved if I don't have any trouble.

This question has already been answered. Start a new discussion instead.