Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runaway loop.. 1

Status
Not open for further replies.

mover50

Programmer
Aug 3, 2004
77
0
0
US
Here is the code running GNU GCC C++ on linux.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()

{
int magic;
int guess;

magic = rand();

do {
cout << "Enter your guess: ";
cin >> guess;
if (guess == magic) {
cout << "** Right ** ";
cout << magic << " is the number.\n";
}
else {
cout << "...Sorry, you are wrong.";
if (guess > magic)
cout << " Your guess is too high.\n";
else
cout << " Your guess is too low.\n";
}
} while(guess != magic);


return 0;
}
End of program
cout << " Your guess is too low.\n"; is the runaway code and is executing over and over again without doing the while test when guess is < magic.

guess >= magic it works ok.

Is this a compiler bug or did I code something wrong?

Thanks,

Kent
 
This program runs while guess is not equal to number. What's wrong?..
 
When testing initially, print the value of magic so that you can see whether it is getting it right or wrong.
 
There is an error in this snippet design. If an user types not-a-number at 1st pass, cin failed forever (no any cin.clear() or another error handling). In that case guess is not initialized (forever;) and you have unbreakable loop because of (undefined) guess is not equal to magic (with probability ~1;)...
 
On another note, you never initialized the PRNG... (look up srand)
 
There is an error in this snippet design. If an user types not-a-number at 1st pass, cin failed forever (no any cin.clear() or another error handling). In that case guess is not initialized (forever;) and you have unbreakable loop because of (undefined) guess is not equal to magic (with probability ~1;)...[\i]

Huh??? How would you code it?

Kent (the worrier)
 
It works for me, but here are the suggested improvements:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int magic;
	int guess;
	
	srand( static_cast<unsigned int>(time( NULL )) );
	magic = rand();
	
	do
	{
		cout << "Enter your guess: ";
		cin >> guess;

		if ( cin.fail() == true )
		{
			cin.clear();
			cin.ignore();
			cout << "Only enter numbers: [0-9]" << endl;
			continue;
		}

		if ( guess == magic )
		{
			cout << "** Right ** ";
			cout << magic << " is the number.\n";
		}
		else
		{
			cout << "...Sorry, you are wrong.";
			if ( guess > magic )
			{
				cout << " Your guess is too high.\n";
			}
			else
			{
				cout << " Your guess is too low.\n";
			}
		}
	} while( guess != magic );
	
	return 0;
}
 
Thanks cpjust,

That stopped the runaway, however the cout << "Only enter numbers: [0-9]" << endl; never did execute.

I did a man srand and read it, and am not quite sure how it works. What is RAND_MAX and how is it set?

How would I use the rand() to generate a number say between 1-100?

Thanks,

Kent
 
RAND_MAX = 0x7FFF.

That cout line will only execute if you enter a letter or some other non-number.

rand() returns between 0 & RAND_MAX, so I would do this:
Code:
int  randNum = ((rand() / RAND_MAX) * 100) + 1;
 
int randNum = ((rand() / RAND_MAX) * 100) + 1;

Won't you always get 1 doing that?

How about:

int randNum = (rand()/%100) + 1;

or something like that..

Kent

Kent (the worrier)
 
oops

I mean.

Code:
int  randNum = (rand()%100)  + 1;

That seems to generate a number between 1 and 100, at least it does for me.


Kent
 
Yes, now that I've had a few more hours of sleep today, that would be a better way of doing it. ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top