Member Avatar for Member #165061

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

Dani AI

Generated

A simple, reliable rule is to measure how far the guess is from the secret number and map that distance to a hint. is right that you must first decide what "hot" and "cold" mean. 's "too high/too low" idea is useful for directional guidance, and 's use of a distance check is the core of the solution.

Algorithm (step-by-step)

  • Compute distance = absolute(guess - secret).
  • If distance == 0 → correct.
  • If distance == 1 → "Hot" (one away).
  • If distance == 2 → "Cold" (two away).
  • Otherwise say "Too high" or "Too low".
    Use a clear loop (for 1..3 attempts) and validate input each time so the logic stays simple and predictable.

Example (modern, compact C++ style)

#include <iostream>
#include <random>
#include <string>
#include <cmath>

std::string hint_for(int guess, int secret) {
    int d = std::abs(guess - secret);
    if (d == 0) return "Correct — you won!";
    if (d == 1) return "Hot (very close)";
    if (d == 2) return "Cold (close)";
    return guess < secret ? "Too low" : "Too high";
}

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dist(1, 10);
    int secret = dist(gen);

    for (int attempt = 1; attempt <= 3; ++attempt) {
        int g;
        std::cout << "Guess (" << attempt << "/3): ";
        if (!(std::cin >> g)) break;   // simple input validation
        std::cout << hint_for(g, secret) << '\n';
        if (g == secret) break;
    }
}

Notes and quick fixes

  • Use <random> instead of legacy srand/rand for better results.
  • Be consistent about the allowed range (your code printed 0..LIMIT but generated 1..10).
  • Prefer '\n' for ordinary output; use std::endl only when you need to flush.
  • Keep attempt handling explicit (a for-loop is clearer than the mixed pre-read/while pattern in the original).

Recommended Answers

All 4 Replies

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 Member #165061

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.