What's wrong with the following?

I've been having trouble with creating this... You're supposed to pick a number and the computer has to guess it. So far it's working, but I can't figure out why the computer won't obey the continue when it guesses over or below what it's already guessed. Can anyone help?


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
57
58
59
60
61
62
63
64
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
using namespace std;

int main()
{
	cout<<"Write a new version of the Guess My Number program in which the player and the"
	    <<" computer switch roles. That is, the player picks a number and the computer must guess what it is.\n\n";

	cout<<"\tWelcome to Guess my Number\n\n";

	int myNumber;
	while(true)
	{
		cout<<"Pick a number, between 1 and 100, for the computer to guess: ";
		cin>>myNumber;

		if(myNumber >= 1 && myNumber <= 100)
		{
			break;
		}

		cout<<"The number wasn't between 1 and 100... Try again.\n\n";
	}



	srand(static_cast<unsigned int>(time(0)));
	int computerNumber = (rand()%100)+1;
	int lowestNumber = 0;
	int highestNumber = 100;
	int tries = 0;

	while(computerNumber != myNumber)
	{
		if(computerNumber < myNumber)
		{
			lowestNumber = computerNumber;
			computerNumber += (rand()%(100))+1;
		}

		else
		{
			highestNumber = computerNumber;
			computerNumber -= (rand()%(100))+1;
		}

		if(computerNumber <= lowestNumber || computerNumber >= highestNumber)
		{
			continue;
		}

		cout<<"\nThe computer Guesses: "<<computerNumber<<endl;

		++tries;
		Sleep(500);
	}

	cout<<"\nThe computer guessed your number in only "<<tries<<" tries!\n\n";

	return 0;
}


Thanks.
Last edited on
closed account (S6k9GNh0)
I appreciate the use of indented code and code tags. I'm almost in tears. T.T

Quick version: Look at the values of lowestNumber and highestNumber when the check is made. It's clear when looking at a debugger. I've gotta sleep...
Last edited on
You can get rid of the continue statement and that whole if block. It does nothing. The error in your program is here:
1
2
3
4
5
6
7
8
9
10
11
if(computerNumber < myNumber)
		{
			lowestNumber = computerNumber;
			++computerNumber; //Changing to this from computerNumber += (rand()%(100))+1;
		}

		else
		{
			highestNumber = computerNumber; //Changing to this from computerNumber -= (rand()%(100))+1;
			--computerNumber;
		}


Now the program will work, but will take a lot of tries to get the number. The trouble with constantly adding a rand() to your total is that the computer is guessing numbers out of range and also just firing randomly into a dark room, hoping to get an answer. You can use some simple if/else to improve the search the computer does, for example:
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
if(computerNumber < myNumber)
		{
			lowestNumber = computerNumber;
			if(computerNumber + 10 < highestNumber)
			{
			    computerNumber = computerNumber + 10;
			}
			else
			{
			    ++computerNumber;
			}
		}

		else
		{
			highestNumber = computerNumber;
			if(computerNumber - lowestNumber > 10)
			{
			    computerNumber = computerNumber - 10;
			}
			else
			{
			    --computerNumber;
			}
		}


Even just adding those two if statements drastically cuts down the number of guesses required.
Alright, this makes so much more sense to me now and I managed to make it work! Thanks!
Topic archived. No new replies allowed.