0

When I start the program I always get a random number, that works fine. However when I guess the number and then say I want to play again, the number I am going to guess is the same as it was before. I tried putting there srand(time(0)); but that does not work for me. I am only a beginner in C++ so could someone tell me how can I fix it or what to do with it? Here is my code:

#include <iostream>
#include <string>
#include <random>
#include <time.h>

using namespace std;

int main()
{
    srand(time(0));
    int NumberToGuess = rand() % 1000;
    string UserGuess;
    int GuessCount = 0;
        
    bool ContinuePlaying = true;

    while (ContinuePlaying) 
    {
        cout << "Guess a number between 0 and 999" << endl;
        cin >> UserGuess;
        GuessCount++;

        int UserNumber = stoi(UserGuess);

        if (UserNumber == NumberToGuess) 
        {
            cout << "You guessed correctly. Congratulations you won the game." << endl;
            cout << "It took you " << GuessCount << " guesses to guess the number." << endl;
            cout << "Do you want to play again (y/n)?" << endl;

            string playAgain;

            cin >> playAgain;

            if (playAgain == "y") 
            {
                int NumberToGuess = rand() % 1000;
                GuessCount = 0;
            }

            else if (playAgain == "n") 
            {
                cout << "Thank you for playing" << endl;
                ContinuePlaying = false;
            }

            else
            {
                cout << "Error: 505";
                ContinuePlaying = false;
            }
        }

        else if (UserNumber > NumberToGuess) 
        {
            cout << "My number is lower, please try again." << endl;
                
        }

        else if (UserNumber < NumberToGuess) 
        {
            cout << "My number is higher, please try again." << endl;
                
        }
    }
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
AkOs49
  • 1
  • Fyi, `rand()` and `srand` *both* require `cstdlib` (or `stdlib.h`) inclusion. Not that I *ever* use those in modern c++ now that we finally have a nice random library. – WhozCraig Jan 04 '21 at 16:17
  • 2
    In `if (playAgain == "y") {...}` the line `int NumberToGuess = rand() % 1000; ` must be `NumberToGuess = rand() % 1000;`, you create a new local variable there. – Lukas-T Jan 04 '21 at 16:17
  • Does this answer your question? [Why does rand() yield the same sequence of numbers on every run?](https://stackoverflow.com/questions/9459035/why-does-rand-yield-the-same-sequence-of-numbers-on-every-run) – Oussama Ben Ghorbel Jan 04 '21 at 16:18
  • Duplicate of https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c – Victor Gubin Jan 04 '21 at 16:19
  • try remove int before NumberToGuess, like this if (playAgain == "y") { NumberToGuess = rand() % 1000; GuessCount = 0; } – matmahnke Jan 04 '21 at 16:20
  • Please see [this PRNG library reference](https://en.cppreference.com/w/cpp/numeric/random), C++ have since many years have much better ways to generate (pseudo) random numbers. – Some programmer dude Jan 04 '21 at 16:21

3 Answers3

2

Welcome to C++ and its quirks. The problem is with this chunk of code:

        if (playAgain == "y") 
        {
            int NumberToGuess = rand() % 1000;
            GuessCount = 0;
        }

You're not assigning the new random value to NumberToGuess, you're creating a new variable with the same name that shadows the other variable, assigning your value to it, the promptly discarding it, so the original variable stays the same. Simply remove the int part:

        if (playAgain == "y") 
        {
            NumberToGuess = rand() % 1000;
            GuessCount = 0;
        }
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • For what it's worth, C# treats your code as an error, you're not allowed to shadow variables in an inner scope when you have a variable with the same name in an outer scope. – Blindy Jan 04 '21 at 16:20
  • C++ compilers **also** [warn about this typo](https://godbolt.org/z/jc15qf). To miss this typo would require suppressing/ignoring compiler warnings. – Drew Dormann Jan 04 '21 at 16:33
  • Right, but my point was that the C# language itself doesn't allow this. It's not a warning you can ignore, it's an error that blocks the binary from being generated. – Blindy Jan 04 '21 at 16:34
  • Also, interestingly enough, the `gcc`-like compilers seem to only warn that the variable isn't used, which means the warning wouldn't be there if you also used the shadowing variable. MSVC also warns about the shadowing itself: `warning C4456: declaration of 'NumberToGuess' hides previous local declaration` – Blindy Jan 04 '21 at 16:40
1

The problem is here:

if (playAgain == "y") 
{
     int NumberToGuess = rand() % 1000;
     GuessCount = 0;
}

Here you define a brand new and independent variable NumberToGuess whose life-time will end at the closing }.

Don't define a new variable, just assign to the one you already have:

if (playAgain == "y") 
{
     NumberToGuess = rand() % 1000;
     GuessCount = 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1
if (playAgain == "y") 
{
    int NumberToGuess = rand() % 1000;
    GuessCount = 0;
}

Your problem is here. int NumberToGuess is declaring a new variable inside the scope of that if and setting it to rand() % 1000. If you only want to update the existing variable, drop that int. So:

if (playAgain == "y") 
{
    NumberToGuess = rand() % 1000;
    GuessCount = 0;
}
scohe001
  • 15,110
  • 2
  • 31
  • 51