Have the computer guess your number.

OK so I'm stuck on another problem.
I'm trying to write a program that you guess a number in your head 1-100 and then the computer asks you if its lower than 50 to start the process of elimination... I tired of and tried and failed at it - maybe someone here can help me :)

It should also not take more than 7 guess for the computer to figure out the problem either. I'm trying to make the formula so when the two numbers ( highernum and lowernum) match up it will automatically spit out the number you guess but I'm trying to figure out the formula with no avail.... any ways here my code - it also asks me in the beginning the same question twice which I want to get fixed also:

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
#include <iostream>
using namespace std;

int main()
{
	int input;
	int highernum = 100;
	int lowernum = 1;
	cout << "Guess a number between 1 and 100\n"
		 << "When your ready to begin press enter.";
	cin.get();
	cout << "Is the number you are thinking of lower than " << highernum / 2 << "?\n- 1 for yes - 2 for no - 3 to quit -\nEnter number: ";

	while (cin >> input)
		if (input == 1)
		{
			lowernum += 0;
			highernum = (lowernum + highernum) / 2;
			
			cout <<  "\nIs the number you are thinking of lower than " << highernum << "?\n- 1 for yes - 2 for no - 3 to quit -\nEnter number: ";
		}
		else if(input == 2)
		{
			lowernum = highernum + 1;
			highernum = (highernum + lowernum) -2;
			cout << "Is the number you are thinking of lower than " << highernum << "?\n- 1 for yes - 2 for no - 3 to quit -\nEnter number: ";
		}
		else if(input == 3)
		{
			cout << "The program will now terminate...\n";
			cin.get();
			return 1;
		}
		else if(lowernum = highernum )
		{
			cout << "I guess that your number is " << highernum << "!\n";
			break;
		}
		
	return 0;
}


Thanks again :)
Last edited on
closed account (NUj6URfi)
Please use code tags.

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

 #include <iostream>
 using namespace std;

 int main() {
 int input = 0;
 int highernum = 100;
 int lowernum = 1;
 cout << "Guess a number between 1 and 100\nWhen your ready to begin press enter.";
 cin.get();
 cout << "Is the number you are thinking of lower 50\n'y' or 'n'\n";

 while (cin >> input)
 if (input == 'y')
 {
 lowernum += 0;
 highernum = (lowernum + highernum) / 2;


 cout << "Is the number you are thinking of lower than " << highernum << "?\n";
 }
 else if(input == 'n') {
 lowernum = highernum + 1;
 highernum = (highernum + lowernum) / 2;
 cout << "Is the number you are thinking of lower than " << highernum << "?\n";
 }
 else if(lowernum = highernum ) {
 cout << "I guess that your number is " << highernum << "!\n";
 break;
 }

 return 0;
 }
I'm sorry I don't follow?
closed account (NUj6URfi)
Please use code tags in the future.
My bad.... thanks again- I caught on now.... new to forum sorry :) Now who can help me with my problem :D
Ok I figured another way to fix this.... anyone knows how I can delete this post so I can save space for the forums?

-Thanks
Topic archived. No new replies allowed.