Random Number Generator

Hey guys, I am creating a random number generator and have run into a problem with my do while loop. it continually prints one of the lines of output infinitely. what am i missing here?
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	unsigned seed = time(0);
	srand(seed);
	int randomNum, guess;

	cout << "Guess A Number Between 1 and 100:   ";
	cin >> guess;
	randomNum = 1 + rand() % 100;

	do
	{
	
		cout << "\n\n";

		if (guess > randomNum)
		{
			cout << "Your guess is too high. Try again!\n";
		}
		else if (guess < randomNum)
		{
			cout << "Your guess is too low. Try again!\n";
		}
		else
		{
			cout << "You guessed the number. Congratulations! The number was " << randomNum;
		}
	} 
	while (guess != randomNum);
		cin.get();
		cin.get();

	return 0;
}
The guess variable is never updated inside the loop.
oh yeah, right. Got that now, thanks. but now I don't see any text appear when the number is guessed correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
do
	{
		if (guess > randomNum)
		{
			cout << "Your guess is too high. Try again!\n";
			cout << "Guess A Number Between 1 and 100:   ";
			cin >> guess;
		}
		else if (guess < randomNum)
		{
			cout << "Your guess is too low. Try again!\n";
			cout << "Guess A Number Between 1 and 100:   ";
			cin >> guess;
		}
		else
		{
			cout << "You guessed the number. Congratulations! The number was " << randomNum;
		}
	} 
	while (guess != randomNum);
The congratulations text will only appear if they guess correctly on the first try. If they get the right number on line 7 or line 13 after a wrong guess, the condition of the while loop on line 20 will be no longer be true and the loop will terminate.
Last edited on
You probably need to flush cout.
1
2
3
cout << flush;
// or 
cout.flush();


Or you could use endl. It will output a newline character and flush cout. I guess you want a newline after the output anyway.

cout << "You guessed the number. Congratulations! The number was " << randomNum << endl;
flushing cout/using endl didn't do anything to fix the problem, but probably a good idea either way.

If they get the right number on line 7 or line 13 after a wrong guess, the condition of the while loop on line 20 will be no longer be true and the loop will terminate.


this makes sense... very raw to C++, any suggestions?
try to put
1
2
cout << "Guess A Number Between 1 and 100:   ";
cin >> guess;


inside do ... while and before the tests, and remove it from if else blocks

1
2
3
4
5
6
7
do
{
     cout << "Guess A Number Between 1 and 100:   ";
     cin >> guess;
     if()
     //...
}
Last edited on
try to put
1
2
cout << "Guess A Number Between 1 and 100: ";
cin >> guess;


inside do ... while and before the tests, and remove it from if else blocks


that did the trick. thanks a million, peeps :)
Topic archived. No new replies allowed.