numbers guessing game

closed account (1AozwA7f)
Reading Stroustrup: Programming -- Principles and Practice Using C++, chapter 4, exercise 4:

"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 < and <= operators and the if-else construct."

What I've managed so far, but what I have seems to be lacking:

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
  #include "../../std_lib_facilities.h"

int half(int guess);
int going_up(int i);

int main()
{
	int i = 100;
	i = half(i);
	i = going_up(i);
	i = half(i);
	i = going_up(i);
}

int going_up(int i)
{
	int temp = i;
	char answer = 'y';
	while (answer == 'y')
	{
		i = temp + (i / 2);
		cout << "Is the number you are thinking of more than " << i << " (y or n)? ";
		cin >> answer;
	}
	return i;
}

int half(int i)
{
	char answer = 'y';
	while (answer == 'y')
	{
		i /= 2;
		cout << "Is the number you are thinking of less than " << i << " (y or n)? ";
		cin >> answer;
	}
	return i;
}
Topic archived. No new replies allowed.