HiLo Game glitches?

Hey guys, below is my code for a HiLo game. Currently when you play the game, even when you guess the correct number, it says it's too high or low, until you guess the number again? It used to work fine, until I added the error message and the option to play again, which is required by my teacher. Whats wrong with my code?
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
#include <iostream>
#include <ctime>

using namespace std;

int main ()
{
int range;
int num = 0;
int guess = 0;
char answer;
srand(time(0));


cout << "Hello! Welcome to the HiLo game!" << endl;
cout << "To start, please enter the range: ";
cin >> range;

while (answer != 'N' && answer != 'n')
do
	{
		int tries = 0;
		num = rand() % range + 0;
		cout << "Enter a guess between 0 and "<<range<<":";
		cin >> guess;
			tries++;
		if (guess > num)
			{cout << "Too high! Try again!\n\n" << endl;}
		else if (guess < num)
			{cout << "Too low! Try again!\n\n" << endl;}
		else
			{cout << "\nCorrect! It took you  " << tries << " tries!\n";
			cout << "Would you like to play again? (Y for yes N for no)";
			cin >> answer;}
			if ( guess == -999)
				{cout << "The correct number was:" << num << endl;}
			if ( guess > range || guess < 0)
				{cout << "Error: invalid guess, try again." << endl;
				tries--;}
			}
		while (guess != num);

system("pause");
}
Hi there,

I think you have a curly brace too much:

31
32
33
34
else
{cout << "\nCorrect! It took you  " << tries << " tries!\n";
cout << "Would you like to play again? (Y for yes N for no)";
cin >> answer;} <-- //this one here 


Please note that your style of indentation is partly responsible for this, it makes open and closing braces rather hard to spot. Have a look at following link for alternatives: http://en.wikipedia.org/wiki/Indent_style

All the best,
NwN

Topic archived. No new replies allowed.