Random Number guessing program

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;


}
Could you be more specific? What exactly confuses you about the "within 10%" part? The math? Coding it once you know the math?
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..
10% of 25 is 2.5
Last edited on
Wouldn't you divide for the less than?
No, the close range is from 5-2.5 to 5+2.5
http://gyazo.com/b07dfcee85ce747651f336b1c838248f
Here's the problem in case I worded it wrong.
But wouldn't it be within 10% of the random number?
Btw, you might want to look into seeding your random number.

http://www.cplusplus.com/reference/cstdlib/srand/
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;


}
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;
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;
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.