Random Number guessing program
Nov 23, 2014 at 5:15am UTC
Can someone check this program for me?
It's supposed to create a random number with a max given by the user. Then the user has to guess. The computer tells if they are too high or too low. And it says "hot" if they are withing 10% and "cold" if not. The within 10% part is what's confusing me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
int guess;
int guessMe;
int x;
cout<< "Enter a maximum number" <<endl;
cin>>x;
guessMe = rand() % x + 1;
do {
cout<<"Guess a number between 1 and " << x <<endl;
cin>>guess;
if (guess>guessMe){
cout << "Too high" <<endl;
}
if (guess<guessMe){
cout << "Too low" <<endl;
}
if ((guess> guessMe + (guessMe*0.10)) || (guess< guessMe + (guessMe*0.10))){
cout << "Hot" <<endl;
}
else {
cout << "cold " <<endl;
}
}while (guess != guessMe);
cout<< "BITCH YOU GUESSED IT" <<endl;
}
Nov 23, 2014 at 5:22am UTC
Could you be more specific? What exactly confuses you about the "within 10%" part? The math? Coding it once you know the math?
Nov 23, 2014 at 5:36am UTC
Well I'm pretty sure I understand the math but I may be wrong. So say the max is 25. And the random number generate is 5. Wouldn't within 10% of 5 be 5-(5*.10) or 5+(5*.10)? Or am I off..
Nov 23, 2014 at 5:40am UTC
10% of 25 is 2.5
Last edited on Nov 23, 2014 at 5:42am UTC
Nov 23, 2014 at 5:41am UTC
Wouldn't you divide for the less than?
Nov 23, 2014 at 5:42am UTC
No, the close range is from 5-2.5 to 5+2.5
Nov 23, 2014 at 5:44am UTC
Nov 23, 2014 at 5:46am UTC
But wouldn't it be within 10% of the random number?
Nov 23, 2014 at 5:47am UTC
Nov 23, 2014 at 5:50am UTC
Noted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
srand(time(NULL));
int guess;
int guessMe;
int x;
cout<< "Enter a maximum number" <<endl;
cin>>x;
guessMe = rand() % x + 1;
do {
cout<<"Guess a number between 1 and " << x <<endl;
cin>>guess;
if (guess>guessMe){
cout << "Too high" <<endl;
}
if (guess<guessMe){
cout << "Too low" <<endl;
}
if ((guess> guess + (guess*0.10)) || (guess< guess + (guess*0.10))){
cout << "Hot" <<endl;
}
else {
cout << "cold " <<endl;
}
}while (guess != guessMe);
cout<< "BITCH YOU GUESSED IT" <<endl;
}
Nov 23, 2014 at 6:40am UTC
Here, I updated your code with a little better formatting for readability during output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
srand(time(NULL));
int guess;
int guessMe;
int x;
cout<< "Enter a maximum number: " ;
cin>>x;
guessMe = rand() % x + 1;
cout << endl; // Added
do {
cout<<"\nGuess a number between 1 and " << x;
cout << ": " ;
cin>>guess;
if (guess>guessMe)
{
cout << "\nToo high" <<endl;
}
if (guess<guessMe)
{
cout << "\nToo low" <<endl;
}
if ((guess> guess + (guess*0.10)) || (guess< guess + (guess*0.10)))
{
cout << "Hot" <<endl;
}
else
{
cout << "Cold " <<endl;
}
}while (guess != guessMe);
cout<< "\nBITCH YOU GUESSED IT" <<endl;
Nov 23, 2014 at 6:44am UTC
Really, all you need to work on is the 10% higher or lower.
Try starting with this and working with some different algorithms.
1 2 3 4 5 6 7 8 9 10
guessMe = rand() % x + 1;
cout << "Number is: " << guessMe; // Add an output and get within 10% on your input
cout << endl;
do {
cout<<"\nGuess a number between 1 and " << x;
cout << ": " ;
cin>>guess;
Nov 23, 2014 at 7:35pm UTC
Okay, I think I got. Can someone check for me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
srand(time(NULL));
int guess;
int guessMe;
int x;
cout<< "Enter a maximum number" <<endl;
cin>>x;
guessMe = rand() % x + 1;
do {
cout<<"Guess a number between 1 and " << x <<endl;
cin>>guess;
if (guess>guessMe){
cout << "Too high" <<endl;
}
if (guess<guessMe){
cout << "Too low" <<endl;
}
if ((guess< guessMe + (guessMe*0.10)) && (guess> guessMe - (guessMe*0.10))){
cout << "Hot" <<endl;
}
else {
cout << "cold " <<endl;
}
}while (guess != guessMe);
cout<< "BITCH YOU GUESSED IT" <<endl;
}
Topic archived. No new replies allowed.