Computer that calculates a secret number

Hello. I've been having some trouble with my recent program. I'm trying to make a computer which calculates a number the user decides secretly for him- or herself. The problem is basically, that because my numbers are integer and not floating point numbers, I'll get my numbers rounded down every so often. Please do help, I'm kinda stuck.

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

// ovelse.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

int main()
{
	
	using namespace std;
	
	cout << "Think of a number between 1 and 100. I will try to guess it." << endl;
	cout << "If the number is too high, press h. If the number is too low, press l. If the number is correct, press c." << endl;

	int Guess = 50;
	static int Number = 50;
	for (int iii=0; ; )
	{
		cout << "Er " << Guess << " det tal du tænkte på? ";
		char Answer;
		cin >> Answer;


		Number = Number/2;

		if (Answer == 'l')
		{
			Guess = Guess+Number;
		}

		else if (Answer == 'h')
		{
			Guess = Guess-Number;
		}

		else if (Answer == 'c')
		{
		cout << "Jeg gættede rigtigt! Juhuuu...." << endl;
		break;
		}
	}
		
	

	cin.clear();
	cin.ignore(255, '\n');	
	cin.get();

	return 0;
}
Instead of Number, use two variables: minNumber (initially 1) and maxNumber (initially 100). Then search between those bounds.
1
2
if (Answer == 'l') minNumber = Guess+1;
if (Answer == 'h') maxNumber = Guess-1;


Also, I don't think making Number a static variable is doing anything for you.
Topic archived. No new replies allowed.