Chapter 4 Exercise 4 in PPP 2 Number-Guessing Game

I tried to do it using binary search, but I don't know how to get it to continue asking questions to guess the number. And the exercise asked to use the if-else construct and < and <=. I'm not sure how to use < and <= for this with binary search.

Here's my current 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
54
55
56
// Osman Zakir
// 12 / 22 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Exercise 4
// Execise specifications:
/**
 * Write a program to play a numbers guessing game. The user thinks of a
 *  number between 1 and 100 and your program asks questions to figure
 * out what the number is (e.g., “Is the number you are thinking of less than
 * 50?”). Your program should be able to identify the number after asking
 * no more than seven questions. Hint: Use the < and <= operators and the
 * if-else construct.
 */

#include "../../std_lib_facilities.h"

int main()
{
	char answer = ' ';
	int min = 1, max = 100;
	int middle = max / 2;
	int computer_number = middle;
	do
	{
		cout << "Is the number you are thinking of " << computer_number << "? (y(es)/n(o))\n";
		cin >> answer;
		cin.ignore();

		if (answer == 'y' || answer == 'Y')
		{
			cout << "So I found the number you were thinking of!\n";
			break;
		}
		else if (answer == 'n' || answer == 'N')
		{
			cout << "Should I go higher or lower? (h(igher) or l(ower)\n";
			cin >> answer;
			cin.ignore();

			if (answer == 'h' || answer == 'H')
			{
				min = middle + 1;
				computer_number = (max + min) / 2;
				continue;
			}
			else if (answer == 'l' || answer == 'L')
			{
				max = middle - 1;
				computer_number = (max + min) / 2;
				continue;
			}
		}
	} 
	while (answer == 'n' || answer == 'N');
	keep_window_open();
}
Last edited on
The problem is that after reading Y/N, you read H/L in the same variable.
So if tou read 'N', then you read 'H', so the loop stops.

Instead of checking if the answer is No, check if the answer is not Yes:
while (answer != 'y' && answer != 'Y');
> while (answer == 'n' || answer == 'N');
you've overwrite `answer' with `h' or `l' in line 37.
simply use another variable to get that value.

as an alternative, given that you break the loop when you reach the right answer, you may change the condition to while(true)


by the way, it looks like a conceptual error for the last statement of the loop to be a continue
Oh, so that was it. Thanks for pointing it out.

What about having it use < and <= to find the answer, though, since that's what the exercise is really asking for?

Edit:

So after taking care of that, now I have a problem where it gets stuck on 25 when I say "lower". I haven't tried it for "higher" yet.

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
56
// Osman Zakir
// 12 / 22 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Exercise 4
// Execise specifications:
/**
 * Write a program to play a numbers guessing game. The user thinks of a
 *  number between 1 and 100 and your program asks questions to figure
 * out what the number is (e.g., “Is the number you are thinking of less than
 * 50?”). Your program should be able to identify the number after asking
 * no more than seven questions. Hint: Use the < and <= operators and the
 * if-else construct.
 */

#include "../../std_lib_facilities.h"

int main()
{
	char found_answer = ' ';
	char high_low = ' ';
	bool found = false;
	int min = 1, max = 100;
	int middle = max / 2;
	int computer_number = middle;
	do
	{
		cout << "Is the number you are thinking of " << computer_number << "? (y(es)/n(o))\n";
		cin >> found_answer;
		cin.ignore();

		if (found_answer == 'y' || found_answer == 'Y')
		{
			cout << "So I found the number you were thinking of!\n";
			found = true;
		}
		else if (found_answer == 'n' || found_answer == 'N')
		{
			cout << "Should I go higher or lower? (h(igher) or l(ower)\n";
			cin >> high_low;
			cin.ignore();

			if (high_low == 'h' || high_low == 'H')
			{
				min = middle + 1;
				computer_number = (max + min) / 2;
			}
			else if (high_low == 'l' || high_low == 'L')
			{
				max = middle - 1;
				computer_number = (max + min) / 2;
			}
		}
	} 
	while (found == false);
	keep_window_open();
}


By the way, if anyone wants to see the code in the higher file, just let me know and I'll copy-paste it.
Last edited on
Your 'middle' variable never goes above 51, and the program guesses the same number repeatedly '75', which is because (100+51)/2 is 75 so your program won't be able to go above 75. This sounds like the same reason why you get stuck at 25 when you attempt to go lower. Hope I helped!
Last edited on
Topic archived. No new replies allowed.