My code after finishing the if and else statements lesson

10_tx_uc2
#1
#include <iostream>

int main()
{
    //Starting message
    std::cout << "You are an adventurer diving dep int othe goblin caves.\n";    
    std::cout << "Before you lies a chest with a combination lock.\n";
    std::cout << "Unlock the chest to gather the loot and unlock the next dungeon level.\n";
    std::cout << "Good luck adventurer.\n" << std::endl;

    //The digits
    const int CodeA = 5;
    const int CodeB = 5;
    const int CodeC = 3;

    //Multiplying and adding the numbers
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;


    //Out puting the product and sum
    std::cout << "  # There are three numbers you must figure out:\n";
    std::cout << "  # The code add up to: " << CodeSum << std::endl; //<< product << std::endl; (This works as well if you want to do it on one line.)
    std::cout << "  # The code has a product of: " << CodeProduct << std::endl;

    int GuessA, GuessB, GuessC;
    std::cout << std::endl;
    std::cout << "First Number: "; std::cin >> GuessA;
    std::cout << "Second Number: "; std::cin >> GuessB;
    std::cout << "Third Number: "; std::cin >> GuessC;
    std::cout << std::endl << "You have entered the code: " << GuessA << " " << GuessB << " " << GuessC;
    std::cout << std::endl;

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

    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "You correctly guess the code and open the chest!\n";
    }
    else
    {
        std::cout << std::endl;
        std::cout << "You have put in the incorrect code and the goblins have captured you.\n";
        std::cout << "Better luck next time adventurer.\n";
    }


    return 0;
}

A look at my code after finishing this lesson.

1 Like
#2

Here’s my code after adding an else

1 Like
#3

Here’s what my code looks like.

1 Like
#4

#5

Here’s mine.

#include

int main() // The control function
{
std::cout <<“You are a secret agent breaking into a secure server room\n”;// Story of game
std::cout <<“You need to enter the correct codes to continue…\n\n”;//What to do to pass the level
std::cout << std::endl;

//Declare the 3 number code
const int CodeA = 4;
const int CodeB = 3;
const int CodeC = 2;

const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;

//Print to the terminal
std::cout << "+ There are three numbers in the code.\n";
std::cout << "+ The code add-up to: " << CodeSum << std::endl;
std::cout << "+ The product of code's digits is: "<< CodeProduct << std::endl;

int GuessA , GuessB , GuessC;

std::cout << "\n+Enter the code: ";
std::cin >> GuessA;
std::cin >> GuessB;
std::cin >> GuessC;

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

if ( GuessSum == CodeSum && GuessProduct == CodeProduct )
    std::cout << "You win" << std::endl;

else
    std::cout << "You are dead" << std::endl;

return 0;

}

#6

Here’s my code after this lesson

#7

Heres my code!!

#include

int main()
{
// Welcome messages
std::cout << “Welcome to your first steps toward freedom Prisoner 227.”;
std::cout << std::endl;
std::cout << “We have dispatched the guards from your prison block for 2 hours.”;
std::cout << std::endl;
std::cout << “All you have to do is enter the correct codes into to the terminal.”;
std::cout << std::endl;
std::cout << “Signed, TrippleX”;
std::cout << std::endl;

// 3 number code
const int CodeA = 5;
const int CodeB = 7;
const int CodeC = 3;

const int CodeProduct = CodeA * CodeB * CodeC;
const int CodeSum = CodeA + CodeB + CodeC;


// CodeProduct and CodeSum under welcome
std::cout << std::endl;
std::cout << "+ There are 3 numbers in the code" << std::endl;
std::cout << "+ The numbers multiply to get: " << CodeProduct << std::endl;
std::cout << "+ The numbers add-up to: " << CodeSum << std::endl << std::endl;

// Player input 
int GuessA, GuessB, GuessC;
std::cin >> GuessA;
std::cin >> GuessB;
std::cin >> GuessC;

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

if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
    std::cout << "GateSecurity101: (Bypassed) [OPENING] ";
}

else
{
    std::cout << "GateSecurity101: (!Breach!) [LOCKDOWN]";
}


return 0;

}