Play again option in C++ not working for number guessing game

Viewed 688

When the user inputs 'Y' to try again, the game runs but only gives the user 1 try instead of 3 tries. Program works fine the first time it runs with 3 tries. I'm guessing something is wrong with my loop that it does not reset the number of tries? Let me know if there's any other way I could write my code to make it cleaner/better. Thanks a bunch.

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int guessNum;
int randomNum;
int Tries = 0;

int startGame()
{
    cout << "Number: ";
    cin >> guessNum;
    
    return guessNum, Tries;
}

int main(int a, int b)
{
    while (true) {

     a = guessNum;
     b = Tries;
     char ans;

     // Random number 
     srand(time(NULL));
     randomNum = rand() % 20 + 1;

        // Introduction
        cout << "Guess a number between 1 to 20. You have three attempts." << endl;

        do
        {
            startGame();

            if (guessNum < randomNum)
            {
                cout << "Wrong! It is too low." << endl;
            }

            else if (guessNum > randomNum)
            {
                cout << "Wrong! It is too high." << endl;
            }

            Tries++;
        }

        while (guessNum != randomNum && Tries < 3);

        if (guessNum != randomNum)   // Wrong answer & run out of tries
        {
            cout << "Oops.. All attempts used. The answer is " << randomNum << endl;
        }

        else if (guessNum == randomNum) // User guessed correct number
        {
            cout << "Yes! You are correct!" << endl;
        }

        cout << "Try again?";
        cin >> ans;
        cin.ignore();

        if (ans == 'N')
        {
            cout << "Thanks for playing!";
            break;
        }

    }
}

EDITED V1


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

int guessNum;

int startGame()
{
    cout << "Number: ";
    cin >> guessNum;
    
    return guessNum;
}

int main()
{
    while (true) {

     int randomNum;
     int Tries = 0;
     char ans;

     // Random number 
     srand(time(NULL));
     randomNum = rand() % 20 + 1;

        // Introduction
       cout << endl << "Guess a number between 1 to 20. You have three attempts." << endl;

        do
        {
            startGame();

            if (guessNum < randomNum)
            {
                cout << "Wrong! It is too low." << endl;
            }

            else if (guessNum > randomNum)
            {
                cout << "Wrong! It is too high." << endl;
            }

            Tries++;
        }

        while (guessNum != randomNum && Tries < 3);

        if (guessNum != randomNum)   // Wrong answer & run out of tries
        {
            cout << "Oops.. All attempts used. The answer is " << randomNum << endl;
        }

        else if (guessNum == randomNum) // User guessed correct number
        {
            cout << "Yes! You are correct!" << endl;
        }

        cout << "Try again? Y/N: ";
        cin >> ans;
        cin.ignore();

        ans = toupper(ans);
        if (ans == 'N')
        {
            cout << endl << "Thanks for playing!";
            break;
        }

        else
        {
            Tries = 0;
        }

    }
}

3 Answers

Actually, your program has several defects.

Firstly, If you wonder why the game behaves unexpected way after the first one, You did not set back the Tries to 0 after playing the game.

And, int startgame() should return only one variable. You are trying to return guessnum and Tries at the same time. The only reason the first game is running as expected is that you are using global variables, which is also considered as a bad practice(Some company may fire you if you use it without any good reason).

Furthermore, you are getting two int function arguments from main call, which is not valid. (main function signature should be int main(void) or int main(int argc, char* argv[])). I am surprised that the compiler did not catch this error.

And the variables (int a, int b) are actually not used. When you find unused variables, it is usually a good practice to remove them for maintainability.

So int Tries = 0; is a global variable. It's set before main().

You basically have

int Tries = 0;

main() 
{
  while (true) {

    do
    {
       Tries++;
    } while(Tries < 3);

  }
}

Do you see that for each iteration in while, the value of Tries from the previous iteration is used? You would need to reset it before iterating again.

But there is no reason to have "Tries" as a global variable since you only need to know about it in the while(true)-loop. This is generally the case for a variable - put it to the closest scope possible:

main() 
{
  while (true) {
    int Tries = 0;
    do
    {
       Tries++;
    } while(Tries < 3);

  }
}

Now it's correctly reset between loops, and it is clear it is only needed for the loop logic.

Try to do the same for you other variables.

Try:

if (ans == 'N')
{
    cout << "Thanks for playing!";
    break;
}
else 
{
    Tries = 0;
}
Related