# Thread: Help!!! Number guessing game

1. ## Help!!! Number guessing game

i'm not asking for someone to write the program for me because i already did that. the problem is that i can't get it to generate a random number. i could put in a number and have people guess but i want it to create a new number on its own every time it is started up.

2. Are you familiar with rand()? I believe there is a FAQ on how to use it. If you've already tried it but it doesn't work then post your effort here and details on how it's not working.

3. was playing around with the rand() but my program just froze!!!! but i think i remembered what i did so i'll send you it in a pm as soon as i write it over again.

4. just realized that i don't know how to send a private message

5. Don't send a private message. Post the code and problems here. You might not even be allowed to send a PM until you've been around a while.

If you don't want to post the exact code for some reason, come up with a small example that does the same thing. Look at the FAQ and try to implement the examples there, then move it into your code. At the very least you can post the line of code that uses rand().

6. i think that i just sent it to you

7. my first program, kinda sloppy. i know how to add a loop so i'm not worried about that
using Borland C++ 4.5

Code:
```//Asst. 1
//Guessing game

#include <iostream.h> // allows input and output to be displayed.

int main()
{
int x;
cout << "Do you think that you are able to compete with Nostradamus?\n";
cout << "1 for yes, 0 for no\n";
cin >> x;
if( x == 0 ){
cout << "Too bad, thought you were special.\n";
}
else{
cout << "Really!? Then let's see what you got.\n";
cout << "I'm thinking of a number between 1 and 100.\n";
cout << "Think you can guess it with ten tries?\n";
}
int y;

cin >> y;
if( y == 24){
cout << "Wow, you must be Nostradamus reincarnated.\n";
}
else
if (y < 24-11){
cout << "You're way off, don't bother trying again\n";
}
else
if (y > 21+11){
cout << "What were you thinking?\n";
}
else
if (y >= 24-10){
cout << "You're warming up\n";
}
else
if (y <= 24+10){
cout << "You're warming up\n";
}
else
if (y <= 24+5){
cout << "You're burning up\n";
}
else
if (y >= 24-5){
cout << "You're burning up\n";
}

return 0;

}```

8. Code:
```cin >> y;
if( y == 24){```
this makes no sense. if i answer "y" as in "yes", i think i can guess your number; the program stops.

9. nevermind, it's just that your prompt is a little confusing.

10. you will however need a loop so the user can continue guessing.

something like:
Code:
```while(guess != myNumber)
{
cin >> guess;

if(guess == myNumber)
{
cout << "You got it!" << endl;
}

else
{
cout << "keep guessing..." << endl;
}
}```

11. This:
Code:
```	if( y == 24){
cout << "Wow, you must be Nostradamus reincarnated.\n";
}
else
if (y < 24-11){
cout << "You're way off, don't bother trying again\n";
}
else
if (y > 21+11){
cout << "What were you thinking?\n";
}
else
if (y >= 24-10){
cout << "You're warming up\n";
}
else
if (y <= 24+10){
cout << "You're warming up\n";
}
else
if (y <= 24+5){
cout << "You're burning up\n";
}
else
if (y >= 24-5){
cout << "You're burning up\n";
}```
Is more conventionally written as:
Code:
```if (y == 24) {
cout << "Wow, you must be Nostradamus reincarnated.\n";
}
else if (y < 24-11){
cout << "You're way off, don't bother trying again\n";
}
else if (y > 21+11){
cout << "What were you thinking?\n";
}
else if (y >= 24-10){
cout << "You're warming up\n";
}
else if (y <= 24+10){
cout << "You're warming up\n";
}
else if (y <= 24+5){
cout << "You're burning up\n";
}
else if (y >= 24-5){
cout << "You're burning up\n";
}```
It looks like you just need to add:
Code:
```srand(time(0));
int x = rand() &#37; 100; // An example for an integer in the range [0, 100)```
Then change your literal 24 to x.

12. i'm trying these right now, thanx a lot.

13. You guys mean like this?
Code:
```//Asst. 1
//Guessing game

#include <iostream.h> // allows input and output to be displayed.

int main()
{
int x;
cout << "Do you think that you are able to compete with Nostradamus?\n";
cout << "1 for yes, 0 for no\n";
cin >> x;
if( x == 0 ){
cout << "Too bad, thought you were special.\n";
}
else{
cout << "Really!? Then let's see what you got.\n";
cout << "I'm thinking of a number between 1 and 100.\n";
cout << "Think you can guess it with ten tries?\n";
}
srand(time(0));
int g = rand() &#37; 100; // An example for an integer in the range [0, 100)
int y
while(y != g){
cin >> y;

if( y == g){
cout << "Wow, you must be Nostradamus reincarnated." <<endl ;
}
else if (y < g-11){
cout << "You're way off, don't bother trying again." << endl;
}
else if (y > g+11){
cout << "What were you thinking?" << endl;
}
else if (y > g-10){
cout << "You're warming up" << end1;
}
else if (y < g+10){
cout << "You're warming up" << end1;
}
else if (y <= g+5){
cout << "You're burning up" << end1;
}
else if (y >= g-5){
cout << "You're burning up" end1;
}
}

return 0;```

14. That's pretty close. You're missing a semi-colon, though, and you'll want to initialize y to a number that can't be the correct number (like -1 maybe). Also, as you probably know from your research on rand() you have to #include <stdlib.h> and <time.h> for the rand(), srand() and time() functions.

Did you try running it?

15. yea, and got three errors. it's probably because i didn't do #include <stdlib.h> and <time.h> for the rand(), srand() and time() functions.