My code after finishing the if and else statements lesson

#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.

4 Likes

Here’s my code after adding an else

1 Like

Here’s what my code looks like.

2 Likes

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;

}

Here’s my code after this lesson 53%20(2)

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;

}

#include

int main()
{
//adds the story
std::cout << “You need to enter the vault…”;
std::cout << std:: endl;
std::cout << “enter the corect code to continue”;
std::cout << std:: endl;

//declare code
const int CodeA = 4;
const int CodeB = 2;
const int CodeC = 6;

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 code adds up to:” << CodeSum <<std::endl;
std::cout << “~the code multiplies to give:” << CodeProduct <<std::endl;
//std::cout << “~Your :” << 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 && Guess Product == CodeProduct)
{
std::cout << “You Win!” <<std::endl;
} else
{
std::cout << “you Lose!” << std::endl;
}

return 0;

In the end I also decided to make sure that the numbers are entered in the right order.

TripleX_Lecture27

Hey, Guys. These is my code. I’m using some stuff the creators of the course have not introduced so far (such as functions). I did that so my main() remains a little easier to read. I’m also using a loop (“do… while”) so that the game allows for multiple tries. The logic isn’t that different from the exercise, but there is some stuff that should be cover later on the course. Cheers!

#include<iostream>

void GameIntroStory();
void ChapterIStory();

int main()
{
    // Print the intro to the story
    GameIntroStory();
    
    // Declaration - First code
    const int CodeA = 0;
    const int CodeB = 0;
    const int CodeC = 6;
    const int MaxTries = 10;
    int GuessA, GuessB, GuessC, NumberTry = 0;    

    ChapterIStory();

    do
    {
        // Get player guess
        std::cin >> GuessA;
        std::cin >> GuessB;
        std::cin >> GuessC;
        
        NumberTry++;
        if (GuessA == CodeA && GuessB == CodeB && GuessC == CodeC)
        {
            std::cout << "You and 007 saved the world!";            
            break;    
        }
        else
        {
            std::cout << "Wrong guess. Try Again (You got "<<MaxTries-NumberTry<<" tries left)";
            std::cout << std::endl;
        }        
    } while ( NumberTry < MaxTries);
    
    if (NumberTry>= MaxTries) std::cout << "You failed. James Bond is no more...";
    std::cout << std::endl;

    // Return Function 
    return 0;
}

void GameIntroStory()
{
    std::cout << std::endl;
    std::cout << "M - Hello, 007.\nM - You got a new assignment.";
    std::cout << std::endl;
    std::cout << "M - A terrorist group invaded a russian base and stole the plans of a new ballistic missile.";
    std::cout << std::endl;
    std::cout << "M - As you can imagine, this is a crisis for the world. However, the truth is that we rather have this plans destroyed!";
    std::cout << std::endl;
    std::cout << "M - To ensure that the World don't see those plans again you will also take Dr. Petrova, who developed such weapon out of the equation.";
    std::cout << std::endl;
    std::cout << "M - She was kidnapped by the terrorist. I'm sure she won't mind coming back to England with you, James...";
    std::cout << std::endl << std::endl;
    std::cout << "M - Good luck...\nM - and leave Miss Moneypenny in peace on your way out!\nM - She has enought on her plate without you bugging her!\n ";
    std::cout << std::endl << std::endl;    
    return;
}

void ChapterIStory()
{
    std::cout << "Bond catch the first flight out to Russia, flirting with the flight attendand that brings his good old Vodka Martini ('Shaken... not stird').";
    std::cout << std::endl;
    std::cout << "A bomb explodes the back of the plane! Everyone is startle, but not Bond.\nHe thinks 'The terrorists can't be working alone. They couldn't know I'm coming!'";
    std::cout << std::endl << std::endl;
    std::cout << "Quickly, he grabs a parachute and jump away.\n\nRealizing he is close enought from the base, he ajust his course to get to the base bellow the radar detection.";
    std::cout << std::endl << std::endl;
    std::cout << "Bond now needs to figure out the code to open the door and enter the base. Help him out.";
    std::cout << std::endl << std::endl;
    std::cout << "TIP: What is the code name of the collegue of Bond that died in GoldenEye... twice!\nType the digits separated by spaces.";
    std::cout << std::endl;
    return;
}
2 Likes

After the if and else statements lesson, here is my code so far:

#include <iostream>

int main()
{
    //declare int variables to use for the three numbered code
    const int CodeA = 2;
    const int CodeB = 4;
    const int CodeC = 6;

    //declare int variables to hold sum, prod, div, and mod calculations
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;

    //print introduction/menu to the user
    std::cout << "You're a whistle-blower hacking into the CEO's computer, " << std::endl;
    std::cout << "hack the LEVEL 1 security code to retrieve the evidence..." << std::endl;
    std::cout << std::endl;
    std::cout << "Here's the information you know about the security codes..." << std::endl;
    std::cout << "  * There are three numbers in the codes" << std::endl;
    std::cout << "  * The codes multiply to give " << CodeProduct << std::endl;
    std::cout << "  * The codes add-up to " << CodeSum << std::endl;
    std::cout << std::endl;
    std::cout << "Enter the correct code to continue." << 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 << "Amazing job, you're in! Now download the files and get out quick! This guy is going down!";
    }
    else
    {
        std::cout << "Oh no, you failed! The system is shutting down and the place is going into lockdown! Good luck getting out of this.";
    }

    return 0;
}
1 Like

Here’s mine :slight_smile:

	if (CodeSum == GuessSum && CodeProduct == GuessProduct) {
		std::cout << "Congratulations! You hacked the mainframe 😎" << std::endl;
	} else {
		std::cout << "You failed. The FBI are on their way." << std::endl;
	}
#include <iostream>

int main()
{
    std::cout << "You are a detective trying to figure out the details regarding the disappearance of the mayor's daughter.";
    std::cout << std::endl;
    std::cout << "Hack the computer that you suspect has information on here whereabouts..." << std::endl;

    const int CodeA = 4;
    const int CodeB = 8;
    const int CodeC = 12;

    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 codes add up to: " << CodeSum << std::endl;
    std::cout << "The codes multiply to 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 << "Great job, detective.";
    }
    else
    {
        std::cout << "Not quite. Think harder, detective.";
    }


    return 0;
}


Sharing my code from lecture 28.

#include <iostream>

int main()
{
    //Print welcome messages to the terminal
    std::cout << std::endl;
    std::cout << "You are a netrunner trying to break into a corporate database!" << std::endl;
    std::cout << "Enter the corrcet code to bypass their security..." << std::endl;

    // Declare 3 number code, sum and product of them
    const int CodeA = 2;
    const int CodeB = 4;
    const int CodeC = 5;

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

    // Print sum and product to the teminal
    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 << " - The codes multiply to give: " << CodeProduct << std::endl;
    std::cout << std::endl;

    // Declare a variable for player input
    int GuessA, GuessB, GuessC;

    // Getting player input
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

    // Declare a variable for sum and product of players input
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    // Checking if player guessed the code
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "You have successfully bypassed this security level!" << std::endl;
    }
    else
    {
        std::cout << "You have entered the wrong code! The screen flashes red and displays 'ACCESS DENIED' message!"  << std::endl;
    }
    

    return 0;
}

#include

int main()
{
//this is an intergalactic puzzle game, this is the first message that is printed
std::cout << “Princess Pluto needs your help infiltrating her enemy’s palace” << std::endl;
std::cout << “First you need to prove yourself to continue…” <<std::endl;

//declare the three variables for the code
const int CodeA = 2;
const int CodeB = 3;
const int CodeC = 4;

/* this is for
multiline 
comments 
cooooool */

//print the CodeSum and the CodeProduct for the code
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
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 To Give You:"<<CodeProduct << std::endl;

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

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

if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{ std::cout << “You are worthy!” <<std::endl; }

else { std :: cout << “Princess Pluto does not find you worthy, you will be sent home now.” << std::endl;}
return 0;
}

#include <iostream>

int main()
{
    //Opening Story 
    std::cout << "The year is 2089, You are an escaped prisoner from a Maximum Security Prison on Planet Terra.";
    std::cout << std::endl;
    std::cout << "You find a Turbo 2000 Hover Bike you can use for your escape but it is locked with a code. Figure out the correct codes to unlock the Bike." << std::endl;
    
    // PassCode numbers for Hover Bike
    const int CodeA = 4;
    const int CodeB = 3;
    const int CodeC = 2;

   
    //combined value of passcode numbers
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;
    
    //print sum and product of passcodes
    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 << "The codes 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 << "You hear the Engine of the hover bike come on. You guessed the code correct! You escape the prison successfully!" << std::endl;
    } else
    {
        std::cout << "You hear a loud alarm from the Hover Bike... You guessed the code incorrectly. The guards hear the alarm, find you and throw you back in prison." <<std::endl;
    }
    



    return 0;
}

2 Likes

Privacy & Terms