C++ Guess the number help

G

gambler

Guest
hello,
this is my first time at this place- just wanted to know if I could get some serious programming help. I've got a problem with this program -I've been workn' on it for like five days!!-As of now it's an infinite loop!!!-any suggestions? ( I know very little C++ and not any C!!)


#include <iostream>
#include <cstdlib>

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

double guessed_num;
char response;
double rand_num;
double i;

int main()
{
rand_num = 1+ rand() % 1000;
do
{
cout << "I have a number between 1 & 1000.";
cout << "\nCan you guess my number?";
cout << "\n\nPlease type your first guess: ";
cin >> guessed_num;


if(guessed_num < 1 || guessed_num > 1000)
{
cout << "\nPlease enter a number between 1 & 1000";
cin >> guessed_num;
}

while (guessed_num != rand_num)
{
if(guessed_num < rand_num)
cout << "\nToo low. Try again.";

else if(guessed_num > rand_num)
cout << "\nToo high. Try again.";
}
cout<< "Excellent You've guessed the correct
number";
cout<< "would you like to play again (y or n)?";
cin >> response;

} while(response != 'n')
return 0;
}
 
when you say stuck in an infinite loop, do u mean it never gets the correct number, or it hangs at a certain point?
 
I think your problem is with the last while. Try putting a ';' at the end of it. I think you require one with a do-while iteration.

Actually, the while above that might be the problem, you see you'll have to ask for the new guess inside the loop because if they enter it wrongly the user won't get a chance to change it.

Code:
while (guessed_num != rand_num)
{
if(guessed_num < rand_num)
cout << "\nToo low. Try again.";

else if(guessed_num > rand_num)
cout << "\nToo high. Try again.";

cout << "Enter new number: ";
cin >> guessed_num;
}

for example, because as it stands guessed_num is always the same.

Or you could do :

Code:
while (guessed_num != rand_num)
{
if(guessed_num < rand_num)
cout << "\nToo low. Try again.";

else if(guessed_num > rand_num)
cout << "\nToo high. Try again.";

break;
}

but that would be a kind of pointless use of a while statement and it'll tell the user they've won even if they haven't.
 
mortiz said:
you see you'll have to ask for the new guess inside the loop because if they enter it wrongly the user won't get a chance to change it.
Yep, that's most of it. And yeah, you do need a semicolon on your third to last line (closing the do-while loop). There are a few other things though:

There are a few times where you cin >> guessed_num, and you only check for out of bounds once (with that if statement). You should make the if a while, and place copy one like it after every cin >> guessed_num (or, better yet, create a function that returns the guessed num, and guarantees that the returned value is within bounds).

You're using doubles for the random numbers. There's no need; use ints.

The rand_num will be the same each time you play the game. Move it inside the do-while loop.

And please, please don't use global variables. They are evil (most of the time): when programs get larger, they are a source of hard to track bugs. Stay local (put their declarations inside main).

I hope this helps!

:cheers:
 
Also, to aviod getting the same sequence of random numbers (as you will currently) put this line at the start of your code:

srand(GetTickCount());

This will 'seed' the random number generator with the number returned by gettickcount (which is the number of miliseconds since you're computer was turned on).
 
Thank you guys so much every little thing helped!!-highly appreciate it!!!
 
GetTickCount() is windows specific, best to use one of the standard time functions.
 
Back
Top