#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; }
7 Replies  272 Views  Last Post: 09 October 2012  11:34 PM
#1
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 050 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!:
Replies To: Random number game where comp asks me to think of a random number
#2
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
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
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
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
Re: Random number game where comp asks me to think of a random number
Posted 09 October 2012  10:35 PM
#7
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
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:
Your random number generation looks incorrect, as well.
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.
Page 1 of 1
