# Random number game where comp asks me to think of a random number

Page 1 of 1

## 7 Replies - 424 Views - Last Post: 09 October 2012 - 11:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294962&amp;s=c4f139e2914f55f50ecb670fb4c4c7cf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

# Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 04:23 PM

I'm to create a code that has the computer ask me to think of a random number between 0-50 and computer gets 5 guesses. I am to write the program so the computer always guesses half way inside the possible interval. When I run it, it allows me to enter a number but then says floating exception (core dumped)? Help me please!:

```#include<iostream> /*included to allow for cout/cin to be used*/
#include<ctime> /*included to allow time() to be used*/
#include<cstdlib> /*include to allow rand() and srand() to be used*/

using namespace std;

int main() {
int num, compGuess, numGuesses, upperLim, lowerLim;
srand ( time ( NULL ) );
numGuesses = 0;

cout << "Enter a number: " << endl;
cin >> num;

do{
compGuess = rand() % ( upperLim - lowerLim ) / 2;

cout << "Computer, enter your number: " << endl;
cout << compGuess;
numGuesses++;

if ( num > compGuess ){
cout << "Too low!" << endl;
lowerLim = compGuess / 2;
}
else if ( num < compGuess ){
cout << "Too high!" << endl;
upperLim = compGuess / 2;
}
else if ( num == compGuess ) {
cout << "Winner winner chicken dinner!" << endl;
}
}
while ( num != compGuess && numGuesses <= 5);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Random number game where comp asks me to think of a random number

### #2 Skydiver

• Code herder

Reputation: 4371
• Posts: 14,107
• Joined: 05-May 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 05:00 PM

You didn't initialize upperLim ad lowerLim. How do you know you are what kind of modulus operation is happening on line 16?

### #3 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 07:33 PM

Yes, you are correct. I did a few changes and below is what I got. However, the output is not correct. It always says too low and lets say I put 25, it guesses 5 and says too low and then enters zero four times and says too low.

```#include<iostream> /*included to allow for cout/cin to be used*/
#include<ctime> /*included to allow time() to be used*/
#include<cstdlib> /*include to allow rand() and srand() to be used*/

using namespace std;

int main() {
int num, compGuess, numGuesses, upperLim, lowerLim;
srand ( time ( NULL ) );
numGuesses = 1;
upperLim = 1;
lowerLim = 50;

cout << "Enter a number: " << endl;
cin >> num;

do{
compGuess = rand() % ( upperLim - lowerLim ) / 2;

cout << "Computer, enter your number: " << endl;
cout << compGuess;
numGuesses++;

if ( num > compGuess ){
cout << "Too low!" << endl;
lowerLim = compGuess / 2;
}
else if ( num < compGuess ){
cout << "Too high!" << endl;
upperLim = compGuess / 2;
}
else if ( num == compGuess ) {
cout << "Winner winner chicken dinner!" << endl;
}
}
while ( num != compGuess && numGuesses <= 5);
return 0;
}

```

### #4 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 08:03 PM

I'm attaching my code again....I think I got it right even though the computer always guesses low numbers? At the same time, I'm supposed to take half of each guess the computer guesses half way between each guess. Help would be appreciated.#include<iostream> /*included to allow for cout/cin to be used*/
```#include<ctime> /*included to allow time() to be used*/
#include<cstdlib> /*include to allow rand() and srand() to be used*/

using namespace std;

int main() {
int num, compGuess, numGuesses, upperLim, lowerLim;
srand ( time ( NULL ) );
numGuesses = 1;
upperLim = 50;
lowerLim = 1;

cout << "Enter a number: " << endl;
cin >> num;

do{
compGuess = rand() % (50 + 1) / 2;

cout << "Computer, enter your number: " << endl;
cout << compGuess;
numGuesses++;

if ( num > compGuess ){
cout << " Too low!" << endl;
compGuess = compGuess / 2;
}
else if ( num < compGuess ){
cout << " Too high!" << endl;
compGuess = compGuess / 2;
}
else if ( num == compGuess ) {
cout << " Winner winner chicken dinner!" << endl;
}
}
while ( num != compGuess && numGuesses <= 5);
return 0;
}

```

### #5 Skydiver

• Code herder

Reputation: 4371
• Posts: 14,107
• Joined: 05-May 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 10:33 PM

What's the point of lines 25 and 29 for post #4? You are going to overwrite the value anyway the time around the loop.

### #6 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 10:35 PM

Skydiver, on 09 October 2012 - 10:33 PM, said:

What's the point of lines 25 and 29 for post #4? You are going to overwrite the value anyway the time around the loop.

Can you give me any advice on what to do? Just pointers maybe......

### #7 Skydiver

• Code herder

Reputation: 4371
• Posts: 14,107
• Joined: 05-May 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 11:20 PM

Here's a major hint: Have you seen the algorithm for binary search? http://en.wikipedia....rithm#Iterative

### #8 hoople

Reputation: 2
• Posts: 8
• Joined: 06-September 12

## Re: Random number game where comp asks me to think of a random number

Posted 09 October 2012 - 11:34 PM

Hopefully this code snippet helps:
```if( number > guess ) {
cout << "Too low!" << endl;
lower = guess + 1;
} else if( number < guess ) {
cout << "Too high!" << endl;
upper = guess - 1;
}

```

Your random number generation looks incorrect, as well.