Code after if/else statements

#include <iostream>
using namespace std;

int main()
{
	// Variables
	int FirstCode = 4;
	int SecondCode = 3;
	int ThirdCode = 2;
	// Constant Variables
	const int TotalAddition = FirstCode + SecondCode + ThirdCode;
	const int TotalMultiplication = FirstCode * SecondCode * ThirdCode;

	
	// Intro text
	cout << "Welcome to the wonderful world of hardcore amature code breaking!\n";
	cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
	cout << "You are an apprentice code breaker on your first live fire test\n";
	cout << "You need to enter the correct codes to continue...\n\n";

	// Default output for codes
	// + There are three numbers in the code
	// + The codes multiply to give 18
	// + The codes add-up to 8
	// 
	// Enter the three code numbers followed by x

	

	cout << "+ There are three numbers in the code\n";
	cout << "+ The codes multiply to give " << TotalMultiplication << "\n";
	cout << "+ The codes add-up to " << TotalAddition << "\n\n";
	cout << "  Enter the three code numbers followed by x\n";
	cout << "  Example: 0 0 7 x \n\n";
	
	// Player guess variable

	int FirstGuess;
	int SecondGuess;
	int ThirdGuess;
	cin >> FirstGuess;
	cin >> SecondGuess;
	cin >> ThirdGuess;
	int GuessSum = FirstGuess + SecondGuess + ThirdGuess;
	int GuessProduct = FirstGuess * SecondGuess * ThirdGuess;

	if (GuessSum == TotalAddition && GuessProduct == TotalMultiplication)
	{
		cout << "You win!";
	}
	else if (GuessSum != TotalAddition && GuessProduct != TotalMultiplication)
	{
		cout << "Keep trying!";
	}
	return 0;
}

Are you looking for a game loop? Also in your if/else statement the else if is a bit redundant.

 if (GuessSum == TotalAddition && GuessProduct == TotalMultiplication)
	{
		cout << "You win!";
	}
	else
	{
		cout << "Keep trying!";
	}

This is my TripleX code:

#include <iostream>
#include <ctime>

void PrintIntroduction(int difficulty)
{
    std::cout << "You're a secret agent breaking into LEVEL " << difficulty << std::endl;
    std::cout << "You need to enter the correct codes to continue..." << std::endl;   
    std::cout << std::endl;
}

bool PlayGameAtDifficulty(int difficulty)
{
    PrintIntroduction(difficulty);

    #pragma region  variables for game
    const int CodeA = rand() % difficulty + difficulty;
    const int CodeB = rand() % difficulty + difficulty;
    const int CodeC = rand() % difficulty + difficulty;

    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;
    #pragma endregion

    #pragma region printing the variales
    std::cout << "The sum of the codes is " << CodeSum << std::endl;
    std::cout << "The product of the codes is " << CodeProduct << std::endl;
    #pragma endregion

    #pragma region asking for user input
    std::cout << "Enter CodeA, CodeB, CodeC: " << std::endl;
    #pragma endregion

    #pragma region get user input 
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;
    #pragma endregion

    // checking userinput vs generated code
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "You Win!" << std::endl;
        return true;
    }
    else
    {
        std::cout << "Try Again..." << std::endl;
        return false;
    }
    
    
}

int main()
{
    int difficulty = 1;
    const int MaxDifficulty = 5;

    while (difficulty <= MaxDifficulty)
    {
        bool bLevelComplete = PlayGameAtDifficulty(difficulty);

        #pragma region error checking
        std::cin.clear();
        std::cin.ignore();
        #pragma endregion

        if (bLevelComplete)
        {
            ++difficulty;
        }
    }
    

    return 0;
}

Here’s what I got so far:

#include <iostream>

int main()
{
    //introduces premise of game and main objective
    std::cout << "You are a thief breaking into a heavily guarded treasure room.\n";
    std::cout << "You must crack the lock codes for each door to reach the treasure...\n";

    //sets up our three numbers
    const int CodeA = 4;
    const int CodeB = 5;
    const int CodeC = 6;

    //stores the sum and product of our three numbers
    int CodeSum = CodeA + CodeB + CodeC;
    int CodeProduct = CodeA * CodeB * CodeC;

    //displays sum and product to terminal
    std::cout << std::endl;
    std::cout << "There are three numbers in the code." << std::endl;
    std::cout << "The codes add up to " << CodeSum << std::endl; 
    std::cout << "The codes multiply together to give " << CodeProduct << std::endl;

    int GuessA, GuessB, GuessC;
    std::cout << std::endl;
    std::cout << "Enter your guess here (3 numbers separated by spaces): ";
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;
    std::cout << std::endl;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "You opened the door and got the treasure! Run!" << std::endl;
    }
    else
    {
        std::cout << "You failed to open the lock and tripped an alarm. Run!" << std::endl;
    }
    
    return 0;
}

Privacy & Terms