My code after finishing the if and else statements lesson

Here’s my code after finishing this lesson.

My if else code :slight_smile:

My complete code of if & else statements lesson:

#include <iostream>

int main()
{
    std::cout << "Your in interstellar space and you have found the black hole!";
    std::cout << std :: endl;
    std::cout << "Enter the code to launch the thrusters into the black hole..." << std :: endl;

    //variable declaration
    const int CodeA = 4;
    const int CodeB = 3;
    const int CodeC = 2;

    //expression statement
    std::cout << std :: endl;
    std :: cout << "There are 3 numbers in the code." << std :: endl;

    //Sum of code
    const int CodeSum = CodeA+CodeB+CodeC;
    std :: cout << "The codes add up to: " << CodeSum << std :: endl;
    //product of code
    const int CodeProduct = CodeA*CodeB*CodeC;
    std :: cout << "The codes product is: " << CodeProduct << std :: endl;

    //Guess declaration
    int GuessA, GuessB , GuessC, GuessSum, GuessProduct;

    //taking input for Guesses
    std :: cin >> GuessA;
    std :: cin >> GuessB;
    std :: cin >> GuessC;

    std::cout << std :: endl;

    std :: cout <<"Player Entered Guess is: " << GuessA << GuessB << GuessC << std::endl;

    //Sum of guesses
    GuessSum = GuessA + GuessB + GuessC;    
    std::cout << "Guess sum is: " << GuessSum << std::endl;

    //product of guesses
    GuessProduct = GuessA * GuessB * GuessC;    
    std::cout << "Guess product is: " << GuessProduct << std::endl;

    std::cout << std :: endl;

    //if condition
    if(GuessSum==CodeSum && GuessProduct==CodeProduct)
    {
        std::cout << "GREAT!, your in black hole...";
    }
    else
    {
        std::cout << "You Failed!";
    }
    
 
    return 0; //Return Statment
}
#include <iostream>

int main() //used for starting up the game
{
    //prints out intro
    std::cout << "You are a death row prisoner working with the government for freedom...";
    std::cout << std::endl;
    std::cout << "Your goal is to crack the secret codes from the most wanted criminal's computer..." << std::endl;

    //values
    const int CodeA = 4;
    const int CodeB = 3;
    const int CodeC = 2;

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

    //prints out numbers from variables above
    std::cout << std::endl;
    std::cout << "! There are 3 numbers used as the password on the computer" << std::endl;
    std::cout << "! The password adds up to " << CodeSum << std::endl;
    std::cout << "! The password multiply to give " << CodeProduct << std::endl;

    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 << "Congratulations, you cracked the password!";
    }

    else
    {
        std::cout << "INCORRECT PASSWORD. The computer is now locked.";
    }

    return 0;
}
#include <iostream>

int main()
{
    //print introduction to the game
    std::cout << "It is 1944. 1.75 million Jews have all ready been exterminated in Auschwitz. The SS in conjunction with the Arrow Cross now want to remove all 800,000 Jews living in Hungary. The Glass House in Budapest and other safe houses house thousands of Jews escaping from the Nazis. However, the ruse of the ""Schutz-Passes"" is at stake. An Arrow Cross officer has a paper locked away in his house that will reveal the secret.";
    std::cout << std::endl;
    std::cout << "Your job is to crack through all the locks and retrieve the document.";
    std::cout << std::endl;
    //declare three number code
    int Code1 = 1;
    int Code2 = 5;
    int Code3 = 6;

    int CodeSum = Code1 + Code2 + Code3;
    int CodeProduct = Code1 * Code2 * Code3;

    int GuessA;
    int GuessB;
    int GuessC;

    //print sum and product to the terminal

    std::cout << std::endl;
    std::cout << "+ There are 3 numbers in the code" << std::endl;
    std::cout << "+ The codes add to: " << CodeSum << std::endl;

    std::cout << "+ The codes multiply to: " << CodeProduct << std::endl;
    
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

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

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

    if (GuessSum == CodeSum && GuessProduct == CodeProduct) 
        {
            std::cout << "The lock clicks open.";
        }
        else 
            {
            std::cout << "The lock doesn't budge";
            }
        
    return 0;
}Preformatted text

What my code looks like so far:

#include <iostream>

int main()
{
    //print welcome messages to the terminal
    std::cout << std::endl;
    std::cout << "I lost my password for GameDev.tv";
    std::cout << std::endl;
    std::cout << "Can you hack it so that I can finish my course?" << std::endl;

//declare 3 number code
const int CodeA = 4;
const int CodeB = 5;
const int CodeC = 6;

/*
multi line comment
print sum & product to the terminal
*/
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;

    std::cout << std::endl;
    std::cout << "There are 3 numbers in the code" << std::endl; 
    std::cout << "The numbers add up to: " << CodeSum << std::endl;
    std::cout << "The numbers multiplied by each other add up to: " << CodeProduct << std::endl;

    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 << "Code Correct, you can continue your lesson" << std::endl;
    }

    else
    {
    std::cout << "Code Incorrect, The Police have been called!" << std::endl;
    std::cout << "If I were you I'd start running..." << std::endl;
    }
    
    return 0;
}

xx2

1 Like

Hi :hugs: Here’s my code:

#include <iostream> /* load iostream header file into code before moving forward */

int main() /* () pass code into function */
{ /* {} is the function "body" */

//Print welcome messages to terminal
    std::cout << "You are tasked with solving a mathematical puzzle box." ;
    std::cout << std::endl;
    std::cout << "You must open the box to retrieve the key required to enter the next room." << std::endl;

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

    /* This is a multi
    line comment
    ! */

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

    // Print sum and product to terminal
    std::cout << std::endl;
    std::cout << "There are 3 numbers required to solve the puzzle..." << std::endl;
    std::cout << "The sum of the numbers is: " << CodeSum << std::endl;
    std::cout << "The product of the numbers is: " << CodeProduct << std::endl;

    int GuessA, GuessB, GuessC;
    
    std::cout << std::endl;
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;
    std::cout << std::endl;

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

    
    std::cout << "The sum of your guess is: " << GuessSum << std::endl;
    std::cout << "The product of your guess is : " << GuessProduct << std::endl;

    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    
    {
        std::cout << std::endl << "Congratulations, you attain the key and enter the next room..." << std::endl;
    }
    
    else
    {
        std::cout << std::endl << "The puzzle box locks and you fail the challenge. Out with you!" << std::endl;
    }
    

    return 0;
}

HOWEVER, I’m finding that if I input three numbers that satisfy the product but NOT the sum, I still attain the win-state message. Anyone else care to try it in their code and see if I did something wrong?

*EDIT: Got it! I think the code didn’t auto-save properly or something, no longer giving me the false win-state.

#include <iostream>

int main()
{ 
    // This is the main story line printed to terminal
    std::cout << "You are an astronaut in the space station and the main booster has failed...";
    std::cout << std::endl;
    std::cout << "You need to enter the correct codes to manually restart the booster..." << std::endl;

    // Declare 3 mumber code
    const int CodeA = 4;
    const int CodeB = 8;
    const int CodeC = 7;

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

    //Print CodeSum and CodeProduct to the terminal
    std::cout << std::endl;
    std::cout << "+ There are 3 numbers in the code" << std::endl;
    std::cout << "+ The codes add-up to: " << CodeSum << std::endl;
    std::cout << "+ If you multiply them you get: " << CodeProduct << std::endl;

    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 << "The booster has restarted you get to live!";
    }
    else
    {
        std::cout << "The booster has permanently failed RIP";
    }

Hey folks! I made it!

#include <iostream>

int main()
{
    //Print welcome message to the terminal
    std::cout << "Your are a secret agent. You have to enter the secret code!";
    std::cout << std::endl;
    std::cout << "Write the secret code: ";

    const int CodeA = 4;    // declarate variables
    const int CodeB = 7;
    const int CodeC = 1;

    const int CodeSum = CodeA + CodeB + CodeC; // adding up a,b,c
    const int CodeMultiply = CodeA * CodeB * CodeC; // multiply a, b, c

    //Print CodeSum and CodeMultiply
    std::cout << std::endl;
    std::cout << "+ There are 3 numbers in the code" << std::endl;
    std::cout <<"+ The code add-up to: " << CodeSum << std::endl;
    std::cout << "+ The codes CodeMultiply to give: " << CodeMultiply << std::endl;
    
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;
    
    int GuessSum = GuessSum = GuessA + GuessB + GuessC;
    int GuessMultiply = GuessMultiply = GuessA * GuessB * GuessC;
    
    if (GuessSum == CodeSum && GuessMultiply == CodeMultiply)
    {
        std::cout << "Congratulations. You won!" << std::endl;
    }
    else {
        std::cout << "You lose! That's bad man..." << std::endl;
    }
    


    return 0;
}

Here’s my code after the if and else lesson. Does this code look alright? I’m finding I’m adding a good deal of “std::cout << std::endl;” to skip lines in the terminal so it looks clean once the code is run. Is there a simpler way to do this?

Hi, here’s my code! :slight_smile:

#include <iostream>

int main()
{
    std::cout << "You are a student of a Wizard who was given the task of entertaining the Sphinx";
    std::cout << std::endl;
    std::cout << "Answer my question correctly ... Sphinx smiled and her eyes stare deeply at you";

    const int FirstTal = 1;
    const int SecondTal = 7;
    const int ThirdTal = 9;

    const int SumTal = FirstTal + SecondTal + ThirdTal;
    const int MultiTal = FirstTal * SecondTal * ThirdTal;

    std::cout << std::endl;
    std::cout << std::endl;
    
    std::cout << " 1) I will tell you a story about the Three Lords and their sorcery talismans" << std::endl;
    std::cout << " 2) One of them defeated the others, gaining " << SumTal  << " power level from the combined talismans" << std::endl;
    std::cout << " 3) However, with the help of the ritual he could get a multiplied high power, level " << MultiTal << std::endl;
    
    std::cout << std::endl;
    std::cout << std::endl;
    
    int Answer_FirstTal, Answer_SecondTal, Answer_ThirdTal; 
    
    std::cout << "How much power level did First Talisman have: ";
    std::cin >> Answer_FirstTal; 
    
    std::cout << "How much power level did Second Talisman have: ";
    std::cin >> Answer_SecondTal;
    
    std::cout << "How much power level did Third Talisman have: ";
    std::cin >> Answer_ThirdTal;
    
    std::cout << std::endl;
    std::cout << std::endl;

    std::cout << "Your answer is: " <<  Answer_FirstTal << ", "<< Answer_SecondTal << " and " << Answer_ThirdTal << "." << std::endl;
    
    std::cout << std::endl;
    int Answer_SumTal = Answer_FirstTal + Answer_SecondTal + Answer_ThirdTal;
    int Answer_MultiTal = Answer_FirstTal * Answer_SecondTal * Answer_ThirdTal;

    if (Answer_SumTal == SumTal && Answer_MultiTal == MultiTal)
    {
        std::cout << "Congratulations, you passed the test.";
    }
    else
    {
        std::cout << "It's shame, but your journey ends here";
    }
    

    return 0;
}


1 Like

It’s not very deep my story, but I like it :poop:

This is where I’m at right now:

Had my first issue that I had to debug while doing this lesson. Was silly enough to write a semicolon after the if condition. :cold_sweat: Resulted in getting the same message whether I gave the program the correct answer or not. :face_with_hand_over_mouth:

My progresso in making game here.

#include <iostream>

int main() 
{
    // Wyswietlenie tekstu wprowadzajacego do swiata gry
    std::cout << "You are a terrorist aiming to infect whole world and be the only person who has the remedium." << std::endl;
    std::cout << "Selling this remedium to all infected people would be very profitable." << std::endl;
    std::cout << "Just broke in to a secret military labolatory." << std::endl;
    std::cout << "There is a probe with dangerous new unknown virus called COVID 19 and the remedium." << std::endl;
    std::cout << "Both hidden in very strong and heavy safebox." << std::endl;
    std::cout << "Unfortunately, the safebox requies several codes to open." << std::endl;
    std::cout << "Need to overcome some code security levels." << std::endl;
    std::cout << "You notice a some notes on the table left by one of the employee." << std::endl;
    std::cout << "This may be helpful..." << std::endl;
    std::cout << std::endl;

    // Deklaracja zmiennych kodu do sejfu
    const int CodeA = 4; 
    const int CodeB = 3;
    const int CodeC = 2;

    // suma i iloczyn zmiennych
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;
    
    // Wyswietlenie sumy i iloczynu zmiennych
    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 to give: " << CodeProduct << "." << std::endl;

    // zmienne przechowujace podane cyfry przez gracza
    int GuessA, GuessB, GuessC;
 
    // gracz wprowadza cyfry kodu i zostaja one wyswietlone
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

    // zmienne sumy i iloczynu cyfr kodu podanych przez gracza
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "You opened the safe! Enjoy your brand new disease." << std::endl;
    }
    else
    {
        std::cout << "Wrong numbers. The system reset." << std::endl;
    }
    
    return 0;
}

Do you like it?

This is how my code looks so far after the if and else lesson:

#include <iostream>

int main() 
{
    // These are the instructions of the game.
    std::cout << std::endl << "You're a scientist trying to find a cure for the novel COVID-19-Gamma virus strain..."; 
    std::cout << std::endl;
    std::cout <<"Your VirusDestroyer 2000 tells you the following information..." << std::endl;


// These are the numbers the player will have to guess.
    const int AntigenQtyA = 4;
    const int AntigenQtyB = 3;
    const int AntigenQtyC = 7; 


// These are the artihmetic operators - used for adding and multiplying the variables.
    const int AntigenQtySum = AntigenQtyA + AntigenQtyB + AntigenQtyC;
    const int AntigenQtyProduct = AntigenQtyA * AntigenQtyB * AntigenQtyC; 


// These will output the value of AntigenQtySum and AntigenQtyProduct.
    std::cout << std::endl;
    std::cout << "+ You need to add three quantities of antigen to the cure" << std::endl;
    std::cout << "+ The antigen quantities add-up to: " << AntigenQtySum << std::endl;
    std::cout << "+ The antigen quantities multiply to give: " << AntigenQtyProduct << std::endl; 

    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 == AntigenQtySum && GuessProduct == AntigenQtyProduct)
    {
        std::cout <<"You saved the world!!" << std::endl;
    }
    else
    {
        std::cout <<"Humanity is doomed!!" << std::endl;
    }
    
    return 0;  
}

:beers:

Privacy & Terms