Member Avatar for jpknoob

Alright guys? I've just started doin a bit of C++, but i'm havin a few problems!!! I'm makin a numbers game and so far have managed to program the game, but i'm wantin to juice it up a bit! Can anyone give me some help or hints how to include a function that will give the user a hint as to whether they are close or not (hot or cold)????
Here's my code;

#include<iostream.h>
#include<stdlib.h>  //RANDOM NUMBER GENERATOR
#include <ctime> 
int main()
{
 int random;  //RANDOM NUMBER TO BE GUESSED
 int usersGuess;
 
 char reply = 'y';
 const int LIMIT=10; // MAX RANDOM NUMBER
 cout << "Welcome to the game\n\n"
  << "I will generate a number between 1 and 10\n\n"
  << "and you have 3 chances to guess it\n\n";
 
    srand((unsigned)time(0)); 
   
    int lowest=1, highest=10;  //RANDOM NUMBER RANGE
    int range=(highest-lowest)+1; 
    random = lowest+int(range*rand()/(RAND_MAX + 1.0));        
 
 cout <<"Guess a number between 0 and "
    <<LIMIT << "\n";
   cout << "Input guess : ";
   cin >> usersGuess;
   int attempts = 1;
   while (attempts <3)
   {
    if (usersGuess > random || usersGuess < random)
    {
     cout << "GUESS AGAIN\n";
     cout << "Input guess : ";
     cin >> usersGuess; 
     attempts++;
    }
    else
    {
    attempts = 3;
    cout << "YOU WON\n";
    }
   }

 return(0);
}

Initially, your help is:
Read this
and this

You have to figure out somehow what hot and cold means in relation to the answer they have at the moment. Once you decide that, write down the steps needed to calculate the hotness they currently have, then convert that to code.

It's your choice how to define the terms, that we can't really help you with.

I mean I guess you could do something like

if (usersGuess > random) {
    cout << "Too high!" << endl;
}
    if (usersGuess < random) {
    cout << "Too low!" << endl;
}
    else {
    cout << "You got it! The number was "
    cout << random << endl << endl;
};

To do it, in a simple way. I noticed you using "\n" for your line breaks. Why not do what I did above and, using namespace std, use endl after each cout statement?

By the way, off topic, but WaltP nice avatar. I just realized what it said on the chalkboard... Hehe.

Member Avatar for jpknoob

Hey, cheers for the help,but ishould have been more specific, what do you expect frpm a noob!!! I'm wantin the cout to be cold if it's 2 numbers away, hot if it's one. Can you offer any help? It's confusing me!!!

do you mean something like:

if((usersGuess - random == 1) || (usersGuess - random == -1))

or

if(abs(usersGuess - random) == 1)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.