Infinite loop

So I'm a little new to c++ and have had this problem with programs I try to loop with. When I run it and select the option to try again the output( 0 is too small try again) repeats infidently.Any clues what the problem might be? BTW I'm programing on my transformer tablet with g++ and a program called c4droid
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
# include <iostream>
# include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	char again = 'Y';
	do
	{
		srand(time(NULL));
		int random = rand() % 100 + 1;
		cout << " Guess the number I'm thinking of , 1-100" << endl;
		int number, guess;
		cin >> number;
		while (number < random || number > random)
		{
			if (number < random)
			{
				cout << number << "  is too small\nTry again" << endl;
				cin >> number;
				guess++;
			}
			if (number > random)
			{
				cout << number << " is too big\nTry again" << endl;
				cin >> number;
				guess++;
			}
		}
		if (number == random)
		{
			cout << "You got it, it only took " << guess <<
				" guesses.\nWant to pay again?" << endl;
			cin >> again;
		}
	}
	while (again == 'Y' || again == 'y');
	cout << "Goodbye" << endl;
	return 0;
}
Last edited on
It compiles and runs fine, the only thing i changed was that i initialized the variables number and guess. I would also recommend that you only seed rand once, before the do...while loop.
One thing I would do is move the seed to srand to just before the do loop. You only need to see once, and the way you have it you seed it every time you start the do loop, which will be before each game. Other than that, nothing jumps out at me really. Other thing is maybe on the 2nd if inside the while loop change it to else if.

Oh, and you haven't included <time.h>.

And lastly, you declared int guess but haven't initialized or assigned it a value. So any time you output guess it's an undefined value.

With adding <time.h> and initializing guess it works fine for me, and I moved the srand to right after line 10.
Last edited on
I run the code and it works for me...
There are other things you might want to change, though.
For instance, the first time I guessed the number in about 8 guesses, but the program told me

You got it, it only took 245523422 guesses. Want to pay again?

(pay = play).

That´s because when you create variable int guess, you did not initializate it (give it an initial value = 0) so the portion of memory reserved for it had something and the program took it for the initial value. Understand?

When you say number < random || number > random you are saying "all the cases where random is different from number" (either it´s lower or it´s higher, so it´s always different). You may want to express it as random != number. It´s the same, but makes the code more easy to understand.
Last edited on
Thank you for the advice I implemented it into my code but the problem still persists. I don't think it's my tablet because the same thing happened on my tower which runs code blocks. So any advice?
Last edited on
I dont know what to say, it runs on my netbook, codeblocks... can you describe a bit more what error do you get?
Not really an error but I'll show you an example
Lets say the number is 5

Guess the number I'm thinking of 1-100
(I enter 5)

It only took you 1 guess.
Want to play again
(I enter yes)

0 is too small
Try again
0 is too small
Try again

And just repeats that until I force quit the terminal emulator ( console)
Really don´t know. It works in my machine...
I found a little problem: guess should initialize as 1. Say you guess the number in the first attempt, guess never gets to increment.

About the other problem, sorry, i can´t help you...
Hmm Thank you for your time and help with the mistakes you could find.
Could it be the compiler?
Ok, I just got the same error: so when you finish playing it asks
want to play again?
you probably typed "yes". Try putting only 'y'.

The rest of the letters are read as if they where int (for number)!
Thank you so much it worked. Sorry if I'm being a little impractical here but how could i modify it so yes works? I've read a little about get. Char but i don't really understand the concept so sorry to ask but do you have the time to explain how that works?
you could fix this by making var again a string and, after reading it, resizing it to 1, so you only use its first char...
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
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;


int main()
{
	string again =  "y";
	do
	{
		srand(time(NULL));
		int random = rand() % 100 + 1;
		cout << " Guess the number I'm thinking of , 1-100" << endl;
		int number, guess = 1;
		cin >> number;
		while (number != random)
		{
			if (number < random)
			{
				cout << number << "  is too small\nTry again" << endl;
				cin >> number;
				guess++;
			}
			if (number > random)
			{
				cout << number << " is too big\nTry again" << endl;
				cin >> number;
				guess++;
			}
		}
		if (number == random)
		{
			cout << "You got it, it only took " << guess <<
				" guesses.\nWant to pay again?" << endl;
			cin >> again;
			again.resize(1);
		}
	}
	while (again == "y" || again == "Y");
	cout << "Goodbye" << endl;
	return 0;
}


Notice how I´ve changed the declaration of namespace at the beggining, I´ve changed again´s type to string, and the trick is in line 37.
Now you can type "y", "yes", "yabadabadoo", and it will resize string to its first character only.
Alright thank you for teaching me this new concept. But quick question i thought "Using namespace std"
Was bad habit?
I don´t know really. I didn´t understand that part, yet. All I know is using namespace std; saves me all the std::cout, std::cin kind of thing. And when I first compiled after changing char to string compiler told me that string had not been declared... obviously it took it as a variable name, not type or class, so I went back to what I´ve been doing so far and always worked.
I wasn´t trying to correct anything, just wanted you to note it.
Glad to be helpful! Greetings from Argentina!
Alright thank you again for your help and effort.
Topic archived. No new replies allowed.