Help with guessing game im making

• 06-04-2002
Unregistered
Help with guessing game im making
Im making a guessing game in c++. THis is my first attempt at a program. I cant figure out how to do it. Heres my code, i dont know whats wrong:

#include <iostream.h>
int main ()
{
int guess;
int num;
cout<<"Enter a guess";
cin>>guess;
if(guess = num)
{
cout>>"You guessed the right number!";
}
else
{
cout>>"Try again"
}
}
• 06-04-2002
GodLike
hmmm

try to give to the GUESS
something
some number

becouse the GUESS can be -34343434
and you never guess it
• 06-04-2002
btq
also use == instead of =.
and also << istead of >> for cout on the last one...
so:

#include <iostream.h>
int main ()
{
int guess;
int num=4; //or any other number
cout<<"Enter a guess";
cin>>guess;
if(guess == num)
{
cout << "You guessed the right number!";
}
else
{
cout << "Try again" ;
}
}

yipyep!
• 06-04-2002
RedRum
the num should have a value as godlike said.
e.g. int num=33;

Aslo the equlity sign is ==
if(guess==num)

and you should return a value to main.
• 06-04-2002
kas2002
You should use the rand() function too I think thats what it is. Ti create a random number. Use like rand()%100 to get a number less the 100.

Code:

also use == instead of =.
and also << istead of >> for cout on the last one...
so:

#include <iostream.h>
int main ()
{
int guess;
int num; //or any other number
num=rand()%100;//creates a random number between 1 -99
cout<<"Enter a guess";
cin>>guess;
if(guess == num)
{
cout << "You guessed the right number!";
}
else
{
cout << "Try again" ;
}
}

cplusplus home is a good resource site. By the way if anyone knows anybetter ones would they let me know cause i have pretty much read everything on this on
• 06-04-2002
Unregistered
I have one more question...
It exits the program too fast when i run it. I cant see weather or not the guess was right. Is there some way to make it pause untill the user hits a key, like the SLEEP command in QBasic.
• 06-04-2002
Okiesmokie
Code:

#include <windows.h>
..
Sleep(x); // where X is the number of milliseconds to pause the program for

• 06-05-2002
mazo
There are many ways to make a pause before exiting a program.
You can include <conio.h> and then use
Code:

while (!kbhit());
or <stdio.h> and getchar();
In DOS you can use sleep(int number_of_seconds); or delay(int number_of_miliseconds); (maybe this will work also under another OS, but probably not good..)
• 06-05-2002
Flikm
you could also try adding maybe 3 tries before it tells you the answer, because its kinda illogical to be guessing millions of times for one number. and if you make the limit, say, one hundred and negative one hundred, check if the number that the user inputs is larger or smaller than the limit. just some more things to gain more experience :D.
• 06-05-2002
Unregistered
why so many include stuff?
Why do i have to include all this stuff? How am i suposed to know which to include in my programs? What do they do? Are they like libraries are in qbasic?
• 06-05-2002
Traveller
Yes, the header files are like libraries in qbasic. They enable you not to have to worry about codeing some of the everyday routine things such as writing to the screen and getting input and checking to see if a key is hit, ect. You will learn what each header file offers you by experience. By the way you can also open those files and read through them, which will give you the best understanding of what they offer.
• 06-05-2002
sean
If you're learning C++, forget everything you ever learned about BASIC. If you keep comparing stuff, it doubles your learning time. You have to include all that stuff because it's the code for what you're doing. When using higher level languages like C, the goal is to not write code if someone already has.
• 06-05-2002
sean
Also, it'll help you get a better answer next time if you specify what is going wrong with the problem. You don't have to know the source, just say what ou don't like about that. And no, it couldn't be -34343434. It's an int - ints don't have that range. It all comes down to what is in the binary code of the memory location that was next. It's not a random number (however unpredictable it may be)
• 06-06-2002
Unregistered
ok, need help with multiple tries
What I want is to have 3 tries and then have it ask if the user wants to try again. How do I do this? Heres the code I have now:

#include <iostream.h>
#include <windows.h>

int main ()
{
int guess;
int num=4; //or any other number

cout<<"Enter a guess: ";
cin>>guess;

if(guess == num)
{
cout << "You guessed the right number!";
Sleep(1000);
}
else
{
cout << "Try again" ;
Sleep(1000);
}
}
• 06-06-2002
Ryce
Code:

#include <iostream.h>
#include <windows.h>

int main ()
{
int guess;
int trys=1;
int num=4; //or any other number

cout<<"Enter a guess number " << trys << ": ";
cin>>guess;
cin.ignore(80,'\n'); // i uselly need that

while(trys <=4) {// 4 tries
if(guess == num)
{
cout << "You guessed the right number!";
Sleep(1000);
return 0;
}
else
{
cout << "Try again" ;
Sleep(1000);
}

}
cout << " Sorry, but you faild!!!" << endl << "The number was " << num << endl;
cin.get();
return 0;
}

should work maybe a typo here or there, im to lazy to compile