// hi-low guesser
#include <iostream>
#include <string> 
using namespace std;
 const int
NUMBER_OF_GUESSES = 5;
int main()
{
    string firstName;    
    int computerGuess;    
    int numGuesses = 5;   
    char answer;    
    int min;
    int max;


        cout << "please think of a number between 1 and 100, and I will try to." << endl;

        cout << "guess the number in less than 7 attempts." << endl;

        cout << "Press h if I've guessed too high or l if I've guessed too low." << 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 ++;}    

        cout << " yes i guessed your number i win." << endl;

return 0;
}

kinda new to this thing so with sorry if im missing anything or did anything wrong .so my problem is quite obvious when you run the code through the compiler its a problem with my logic and i have been struggling to find out how to fix it for a while, if anybody can help just reply thanks

Here's a little psseudo code that might help you with the logic, the specific code is up to to you. I assumed the guesses are to be whole numbers, if not don't reduce the guess to an integer.

    max=100
    min=1
    guess loop until 7 guesses are done

        guess = int((max-min)/2) + min
        response 
        validate response is hi, low or done
        if response is hi
            max = guess
        else
        if response is low
            min = guess
        else
        if response is done
            Correct answer in less than 7 guesses I win

    next guess 
    7 guesses without the answer I lose
commented: thanks for the help but sorry my problem with my logic was that if i stated to guess and it had to go higher and then the number went to high and i had to tell it to go lower it would go to the opposite end of the number spectrum +0

re:comment

If you re-write your code to follow the logic of my pseudo code it will take care of the problem. The logic in your code is just too flawed, it needs a re-write.

commented: ok thanks +0

If your code works after, please remember to mark this solved. If not feel free to ask for more help. thx

PS you don't need to vote to leave a comment, just use the area below for general comments.

so i am kinda still haveing a problem with how i should redo your pseudo code and how i should re-write it, sofar ive just come up with a bunch of errors and the program just loops the initiale guess continuously

try switching to getline

ok so i changed my logic but now when the compiler asks me if the initial guess is to high or low no matter what i enter it skips to the next line and then shows nothing can ya do you know why this might be happening. my code if so far error free for me but there could be a problem the compiler cant detect

// hi-low guesser
#include <iostream>
using namespace std;
 const int numguess = 7;
int main()
{   
    int numGuesses = 7;
    int computerGuess;       
    char answer;    
    int min = 1;
    int max = 100;
    computerGuess = 50;

        cout << "please think of a number between 1 and 100" << endl;

        cout << "I will try to guess the number in less than 7 attempts." << endl;

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

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

        cin >> answer;    

        while (numGuesses <= 7);

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

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

        cout << "yes I guessed your number I win." << endl;

return 0;
}

like I said you need a major re-write not just a tweak. Here's the code based on my pseudo code:

#include <iostream>
#include <string>
using namespace std;

    int main()
    {
        string firstName;
        int computerGuess;
        char answer = ' ';
        int min=1;
        int max=100;
        bool GoodResponse = false;

        cout<<"please think of a number between 1 and 100, and I will try to."<<endl;
        cout<<"guess the number in less than 7 attempts."<<endl;
        cout<<"Press h if I've guessed too high or l if I've guessed too low and y if I'm correct."<<endl;

        for(int I=0;I<8;I ++)
        {
            computerGuess = int((max-min)/2) + min;
            cout << "Is the number "<< computerGuess << "?"<< endl;
            while(GoodResponse == false)
            {
                cin >> answer;
                if ((answer == 'l')||(answer =='h')||(answer == 'y'))
                    GoodResponse=true;
            }
            GoodResponse=false;
            if(answer == 'l')
                min=computerGuess;
            else if(answer == 'h')
                max=computerGuess;
            else
            {
                cout << " yes I guessed your number in "<< I  <<" guesses.  I win." << endl;
                cin >> answer;
                return 0;            
            }

            answer = ' ';
        }
        cout <<"Oh no I took too many guesses I lose"<<endl;
        cin >> answer
        return 0;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.