Endless Loop and other Issues

This program is supposed to get the computer to guess a number that the user inputs. There are a couple of bugs:

1 - the computer generates "random" numbers in the same order every time I run it.
2 - the computer makes identical guesses.
3 - when the computer guesses the correct number, and the user inputs a 'c' for correct, the program enters an endless loop, but I can't find the problem.

Maybe another set of eyes can help me find my mistake?

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

int main (void)
{
	system ("CLS");
	char cHigh;
	int iNumber, iGuess;
	
	cout << "Input your magic number (between 1 and 100)!" << endl;
	cin >> iNumber;
	int iCount = 1;
	iGuess = rand() % 100 + 1;
        cout << "The computer guessed " << iGuess << "." << endl;
	cout << "Is this guess correct (C), too high (H) ,or too low (L)?" << endl;
	cin >> cHigh;
	int iHigh, iLow, k, m;
	iHigh = 100;
	iLow = 1;
		
	while (cHigh != 'C' || cHigh != 'c')
	{
		switch (cHigh)
		{
		case 'H':
		case 'h':
			iHigh = iGuess;
			k = iHigh - iLow;
			iGuess = rand() % k + iLow;
			iCount++;
			cout << "Guess number " << iCount << " is " << iGuess << endl;
			cout << "Is this guess correct (C), too high (H) ,or too low (L)?" << endl;
			cin >> cHigh;
			break;
		case 'L':
		case 'l':
			iLow = iGuess;
			m = iHigh - iGuess;
			iGuess = rand() % m + iLow;
			iCount++;
			cout << "Guess number " << iCount << " is " << iGuess << endl;
			cout << "Is this guess correct (C), too high (H) ,or too low (L)?" << endl;
			cin >> cHigh;
			break;
		case 'C':
		case 'c':
			break;
		default:
			cout << "I didn't understand your response.  Please enter C, H, or L: " << endl;
			cin >> cHigh;
		}
		cout << "The computer got it!  The magic number was " << iNumber << " and the computer's last guess was " << iGuess << "." << endl;
		cout << "It took the computer " << iCount << " guesses!" << endl;
	}
	system ("BREAK");
	return (0);
}
Last edited on
1 - the computer generates "random" numbers in the same order every time I run it.

This is because you haven't seeded the random number generator. Call the srand function at the beginning of the program. Using an identical seed will create the same set of random numbers. The usual way is to pass time(NULL) to srand so as to use a new seed each time.

2 - the computer makes identical guesses.

This is a side effect of the above.

3 - when the computer guesses the correct number, and the user inputs a 'c' for correct, the program enters an endless loop, but I can't find the problem.

Your loop condition, while (cHigh != 'C' || cHigh != 'c') is always true. Let's say cHigh is 'c', then it's not 'C' so the left condition is true so the whole condition is true. You have to change this condition to have the semantics you're looking for.

Also, you're printing the "The computer got it! ..." in each iteration. You should only print that after you've determined the computer got it right.
Thanks! Man, I feel like a moron! I should have caught that error in my while loop. Duh!

Thanks for the help with the random number issue. Problems solved!
Topic archived. No new replies allowed.