not working as intended


The program does nothing when the user inputs "h" "l" and "y"


while creating this program these conditions should be followed;


Create a program that asks the user to think of a number. The program will attempt to guess the number and ask for input from the user.

Based on that response, the program will attempt to narrow down the answer using a ‘binary search’ algorithm, until the guess is correct or the user chooses to end the program early.

1. Create a new, default console project named ‘NumberGuess’.

2. Change the default ‘hello world’ code to output the message “Think of a number between 1 and 100”.

3. Declare three integer variables: highest, lowest, and attempts.

4. Initialize highest to 100 and the others to zero.

5. Increment the number of attempts.

6. Declare an integer, ‘guess’. Initialize to the value returned by the formula lowest + ((highest - lowest) * 0.5)

7. Display the message “I guess {n}. Am I right?” (Display the value of guess instead of {n}) and on the next line: “’q’ to quit, ‘y’ if correct, ‘h’ if too high, ‘l’ if too low.”

8. Using an if,else if,else blocks as needed, implement the remaining steps.

9. If the user’s response is ‘Y’, display a message “I guessed it in {n} attempts” and cease all further attempts at guessing.

10. If the user’s response is ‘H’, reduce the highest possible guess limit to the current guess.

11. If the user’s response is ‘L’, increase the lowest possible guess limit to the current guess.

12. If the user’s response is ‘Q’, display a message ‘You quit. Bye.’ and cease all further attempts at guessing.

13. If the user makes any other response, display a message “I didn’t understand that response.”

14. Repeat the process of guessing from step 5 above.

15. When all attempts at guessing have ceased, for whatever reason, exit the program with an error code equal to the number of attempts.


When done, the program’s output should look something like this:

```
Think of a number between 1 and 100
I guess 50. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too
low.
h
I guess 25. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
h
I guess 12. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
l
I guess 18. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
l
I guess 21. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
l
I guess 23. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
h
I guess 22. Am I right?
'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low.
y
I guessed it in 7 attempts..




my code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Think of a number between 1 amd 100" << endl;

    int highest = 100;
    int lowest = 0;
    int attempts = 0;

    attempts++;

    int guess = lowest + ((highest - lowest) * 0.5);

 string userinput;
    cout << "I guess " << guess << ". Am I right?" << endl;
    cout << "'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low." << endl;

    getline (cin, userinput);

    while ( userinput != "y")
    {
        if (userinput == "h")
        {
            highest--;
        }
        else if (userinput == "l")
        {
            lowest++;
        }
        else if (userinput == "q")
        {
           cout << "You quit. Bye." << endl;\
           
        }
        else if (userinput == "y")
        {
            cout << "I guessed it in " << attempts << " attempts" << endl;        
        }
        else
          {
            cout << "I didn’t understand that response." << endl;
        }


    }

}
Last edited on
You are break ing out of the while loop. break ends the loop and the program continues after the loop, and thus ends.

Once you've fixed that, your next problem will be that the value of userinput never changes inside the loop.
I have removed break, but the value of userinput is not changing inside the loop
the value of userinput is not changing inside the loop

Of course not. No code inside your loop changes the value of userinput. The only code in your program that ever changes the value, is outside the loop.

If you want the value to change inside the loop, you need to put some code to change it inside the loop!
Last edited on
Once you've fixed that, your next problem will be that the value of userinput never changes inside the loop.


I have removed break, but the value of userinput is not changing inside the loop


Sometimes it's like I have this strange psychic ability :p

Here's the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while ( userinput != "y")
    {
        if (userinput == "h")
        {
            highest--;
        }
        else if (userinput == "l")
        {
            lowest++;
        }
        else if (userinput == "q")
        {
           cout << "You quit. Bye." << endl;\
           
        }
        else if (userinput == "y")
        {
            cout << "I guessed it in " << attempts << " attempts" << endl;        
        }
        else
          {
            cout << "I didn’t understand that response." << endl;
        }
    }

Can you point at the code inside this loop that you think should be changing the value of userinput? Then we can explain to you why it doesn't.
Last edited on
It... it's almost as if people were too lazy to read all the way to the end of a post. As if three whole sentences were too much for some people to make the effort to read.

But... but surely that couldn't be true? Could it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>


using namespace std;

int main()
{
    cout << "Think of a number between 1 amd 100" << endl;

    int highest = 100;
    int lowest = 0;
    int attempts = 0;

    

    int guess;

 char userinput;
 
 while ( userinput != 'y')
    {
        attempts++;
    guess = (highest + lowest) / 2;
    cout << "I guess " << guess << ". Am I right?" << endl;
    cout << "'q' to quit, 'y' if correct, 'h' if too high, 'l' if too low." << endl;

    cin >> userinput;

    
        if (userinput == 'h')
        {
            highest = guess;
        }
        else if (userinput == 'l')
        {
            lowest = guess;
        }
        else if (userinput == 'q')
        {
           cout << "You quit. Bye." << endl;\
           
        }
        else if (userinput == 'y')
        {
            cout << "I guessed it in " << attempts << " attempts" << endl;        
        }
        else
          {
            cout << "I didn’t understand that response." << endl;
        }


    }
return 0;
}
Last edited on
Topic archived. No new replies allowed.