7 Replies - 266 Views - Last Post: 09 October 2012 - 11:34 PM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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

View PostSkydiver, 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......
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 hoople  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1