Hi, I'm new to this C++ stuff. So if anyone can help that would be nice. I have this program to generate a random number and a the user inputs a number from the keyboard to correctly guess the number that the computer randomly chooses. but the numbers are always changing each time, which makes it hard to correctly guess the correct number. So the question is, is it possible to randomly choose a number without it changing each iteration?

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;


int main ( )
{
int guess;
char ans;


srand (time (0));
for (int i = 0; i < 100; i++)
{
int random = rand( );
int num = ((random % 100) + 1);


cout << "Guess a number between 1 and 100: ";
cin >> guess;


if (guess > num)
{
cout << "The number is lower. Try Again!!\n\n";
continue;


}
if (guess < num)
{
cout << "The number is higher. Try Again!!\n\n";
continue;



}
else if (guess = num)
{
cout << "You've guessed correctly!\n\n";
break;
}


}


return 0;
}

Dani AI

Generated

Short answer: the computer keeps choosing a new value because the code that picks the secret is inside the guessing loop. As suggested, pick the secret once before you start asking for guesses. Also check the comparison — make sure you compare the guess to the secret instead of accidentally assigning to it (as pointed out).

A concise, modern example that picks one secret number and then loops until the user finds it:

#include <iostream>
#include <random>
#include <limits>

int main() {
    std::mt19937 gen(std::random_device{}());
    std::uniform_int_distribution<int> dist(1, 100);
    int secret = dist(gen); // choose once

    int guess = 0;
    std::cout << "Guess a number between 1 and 100: ";
    while (true) {
        if (!(std::cin >> guess)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Please enter a valid number: ";
            continue;
        }
        if (guess < secret) {
            std::cout << "Higher. Try again: ";
        } else if (guess > secret) {
            std::cout << "Lower. Try again: ";
        } else {
            std::cout << "Correct!\n";
            break;
        }
    }
    return 0;
}

Notes and cautions: do not reseed the generator inside the loop — seed once at program start. If you stick with the old C API, seed once and call rand() only to produce the secret; avoid repeatedly using the modulo operator for ranges because it can bias results. For predictable testing use a fixed seed; for real randomness use a nondeterministic seed. Add input validation as shown so non-numeric input does not break the loop.

Following and advice will fix the changing-number problem and the logic bug.

Recommended Answers

All 3 Replies

else if (guess = num)

should be:

else if (guess == num)

Hi, I'm new to this C++ stuff. So if anyone can help that would be nice. I have this program to generate a random number and a the user inputs a number from the keyboard to correctly guess the number that the computer randomly chooses. but the numbers are always changing each time, which makes it hard to correctly guess the correct number. So the question is, is it possible to randomly choose a number without it changing each iteration?

Wrap your code in "Code Tags" - makes it easier for everyone to read. :)

Aside from Clinton's excellent spot, there is a slight flaw in your logic...

//Snipped #include's
int main ( )
{
	int guess;
	char ans;
	
	srand (time (0));
    for (int i = 0; i < 100; i++)
	{

So... everything after this curly brace (up til the closing brace) gets repeated 100 times.. including:

int random = rand( );
	    int num = ((random % 100) + 1);

I suggest putting these 2 lines before your loop starts, then you will only be selecting your random number once.


Also, this kind of "guessing game" would look much cleaner if you could say do....while(guess!=num) instead of for...

Thanks guys!!

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.