Trouble with random number generation

I'm trying to make the range smaller when the computer is guessing a number.
I got the higher end, but I don't know how to make the lower end range higher when the guessed number is too low.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        range = 100

  	do
	{
		
		guess = rand() % range + 1; // random number between 1 and 100
		cout << "Enter a guess: " << guess;
		++tries;
		
		if (guess > myNumber)
		{		
			range = 100 - (100 - guess);
			cout << " Too high!\n\n";
		}
		if (guess < myNumber)
		{
			range =     // <-- HERES THE ISSUE
			cout << " Too low!\n\n";
		}
			
	}  while (guess != myNumber);
	
You use a constant on line 6 to indicate the lowest number in the range. range itself does not indicate a range. It indicates the highest number in the range, so the formula on line 6 is wrong.

To fix: Use a (separate) variable for the lowest number in the range. Adjust line 6 to be correct and use that variable to help calculate the range. Have line 17 set the value of that variable.
Topic archived. No new replies allowed.