Have to win the game 2 times to level up 1 time!

Well, I have to win the game two times to level up one time. I’m not able to find out the mistake.

I’m attaching my code below and the compiler video below.

https://imgur.com/a/IRgUuAB

#include<iostream>

void PrintIntro(int Difficulty)
{
    std::cout << "\n\nYou're breaking into a level " << Difficulty; 
    std::cout << " game server. But weirdly, you're asked to add some codes. \nFortunately, you manage to get some hints to calculate the codes.";
    std::cout << std::endl; //New Line Code
    std::cout << "Now it's upto you. Whether you bypass the firewall or you give up!" << std::endl;
}

bool PlayGame(int Difficulty)
{
    PrintIntro(Difficulty);

    //Declaring the Variables
    const int CodeA = 4, CodeB = 3, CodeC = 2, CodeSum = CodeA + CodeB + CodeC, CodeProd = CodeA * CodeB * CodeC;

    //Printing CodeSum and CodeProduct in the terminal
    std::cout << std::endl;
    std::cout << "+ There're 3 numbers in the code" << std::endl;
    std::cout << "+ The codes add-up to: " << CodeSum << std::endl;
    std::cout << "+ The codes multiply to give: " << CodeProd << std::endl << std::endl;

    //Store Player's Guess
    int GuessA, GuessB, GuessC, Confirmation;

    std::cout << "Enter your First Number: ";
    std::cin >> GuessA;
    //std::cout << std::endl;

    std::cout << "Enter your Second Number: ";
    std::cin >> GuessB;
    //std::cout << std::endl;

    std::cout << "Enter your Third Number: ";
    std::cin >> GuessC;
    std::cout << std::endl;

    std::cout << "You entered: " << GuessA << GuessB << GuessC;
    std::cout << std::endl;
    std::cout << std::endl;

    int GuessSum = GuessA + GuessB + GuessC, GuessProd = GuessA * GuessB * GuessC;

    //Check if the player's guess is correct
    if(GuessSum == CodeSum && GuessProd == CodeProd)
    {
        std::cout << "Congratulations! You broke in the Game Server Successfully!";
        return true;
    }
    else
    {
        std::cout << "Oh snap! You got caught. You Lose.";
        return false;
    }
}

int main()
{
    int LevelDifficulty = 1;
    while(true)
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        PlayGame(LevelDifficulty);
        std::cin.clear(); // Clears any errors
        std::cin.ignore(); // Discards the buffer which stored the false value, to prevent future errors

        if (bLevelComplete)
        {
             ++LevelDifficulty;
        }
        
    }
    return 0;
}

I found out the mistake and troubleshot it. It’s working fine now.

The mistake was in the while loop, below the bLevelComplete variable. I called the function PlayGame again and hence it’s running the game again without increasing the level. But as the while loop is not completed yet, after the game is run again, it goes to if expression and increases the level on the 2nd try.

Edit: Typos

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms