# Number Guessing Game Program

Page 1 of 1

## 4 Replies - 3717 Views - Last Post: 23 July 2007 - 07:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=30618&amp;s=836b560a91906662ce6cddf8326f28f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 monkjessica

Reputation: 0
• Posts: 10
• Joined: 18-July 07

# Number Guessing Game Program

Posted 19 July 2007 - 01:19 PM

I am having problems making my program for a number guessing game run. I keep getting hung up on line 19 ( the( rand/time) line. I would appreciate any help debugging this and making it run properly. Thanks!!

```//Assignment 3 Part II

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int num;
int guess;
int diff;
bool done;
int noOfGuesses= 0;

[color=#FF0000]	num = (rand + time(0))% 100;[/color]
done = false;

while ((noOfGuesses < 5) && (!done))
{
cout << "Enter an integer greater than or equal to 0 and less than 100: ";
cin >> guess;
cout << endl;
noOfGuesses++;

if (guess == num)
{
cout << "Winner!. You guessed the correct number." << endl;
done = true;
}
else
{
diff == abs(num - guess);
if (guess < num);
{
if (diff >= 50)
cout << "Your guess is very low." << endl;
else if (30 <= diff && diff < 50)
cout << "Your guess is low." << endl;
else if (15 <= diff && diff < 30)
cout << "Your guess is moderately low." << endl;
else if (0 < diff && diff < 15)
cout << "Your guess is somewhat low." << endl;
}
{
if (diff >= 50)
cout << "Your guess is very high." << endl;
else if (30 <= diff && diff < 50)
cout << "Your guess is high." << endl;
else if (15 <= diff && diff < 30)
cout << "Your guess is moderately high." << endl;
else if (0 < diff && diff < 15)
cout << "Your guess is somewhat high." << endl;
}

if (noOfGuesses != 5)
cout << "Guess again!" << endl;
}
}//end while

if (!done);
cout << "You lose! The correct number is " << num << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Number Guessing Game Program

### #2 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: Number Guessing Game Program

Posted 19 July 2007 - 01:40 PM

hi, try num = (rand() + time(0))% 100;

### #3 zyruz

Reputation: 1
• Posts: 31
• Joined: 13-August 05

## Re: Number Guessing Game Program

Posted 19 July 2007 - 02:00 PM

you need to seed rand, with srand(time(0));
then just call rand() normaly..

```num = (rand + time(0))% 100;
```

do:
```   srand(time(0));

num = rand() % 100;
```

one other probleme I see is:
diff == abs(num - guess);

this wont give diff annything as, it only check if diff is the same as abs(num - guess).

so, remove one of the '=' and it will probarly be better

zy

This post has been edited by zyruz: 19 July 2007 - 02:12 PM

### #4 crzyfro

Reputation: 0
• Posts: 16
• Joined: 09-January 07

## Re: Number Guessing Game Program

Posted 22 July 2007 - 06:02 PM

zyruz, on 19 Jul, 2007 - 02:00 PM, said:

you need to seed rand, with srand(time(0));
then just call rand() normaly..

```num = (rand + time(0))% 100;
```

do:
```   srand(time(0));

num = rand() % 100;
```

one other probleme I see is:
diff == abs(num - guess);

this wont give diff annything as, it only check if diff is the same as abs(num - guess).

so, remove one of the '=' and it will probarly be better

zy

so what would the code look like all together?

### #5 zyruz

Reputation: 1
• Posts: 31
• Joined: 13-August 05

## Re: Number Guessing Game Program

Posted 23 July 2007 - 07:34 AM

it is 2-3 things to change, wont be a challange I bet...
but here it is:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int num;
int guess;
int diff;
bool done;
int noOfGuesses= 0;

srand(time(0));   // seed the rand function

num = rand() % 100; // call rand.
cout << num <<'\n';

done = false;

while ((noOfGuesses < 5) && (!done))
{
cout << "Enter an integer greater than or equal to 0 and less than 100: ";
cin >> guess;
cout << endl;
noOfGuesses++;

if (guess == num)
{
cout << "Winner!. You guessed the correct number." << endl;
done = true;
}
else
{
diff = abs(num - guess); // == will check if a statment is corect or not.
if (guess < num) // dont use; after a if statment.
{
if (diff >= 50)
cout << "Your guess is very low." << endl;
else if (30 <= diff && diff < 50)
cout << "Your guess is low." << endl;
else if (15 <= diff && diff < 30)
cout << "Your guess is moderately low." << endl;
else if (0 < diff && diff < 15)
cout << "Your guess is somewhat low." << endl;
}
else
{
if (diff >= 50)
cout << "Your guess is very high." << endl;
else if (30 <= diff && diff < 50)
cout << "Your guess is high." << endl;
else if (15 <= diff && diff < 30)
cout << "Your guess is moderately high." << endl;
else if (0 < diff && diff < 15)
cout << "Your guess is somewhat high." << endl;
}

if (noOfGuesses != 5)
cout << "Guess again!" << endl;
}
}//end while

if (!done);
cout << "You lose! The correct number is " << num << endl;

return 0;
}

```