My code after finishing the if and else statements lesson

A look at my code after finishing If and ELSE statements. Added player name input by using std::string and then having the player input that, as well as typing OK to continue to the puzzle to avoid clutter of text all at once.


int main()
{
    //cout = characteroutput
    //cin = characterinput

    //printing out string information to the terminal for context.
    std::string PlayerName;
    std::string OK = "OK";

    //player enters any name of their chosing
    std::cout << "ENTER NAME BELOW" << std::endl;
    std::cin >> PlayerName;
    std::cout << std::endl;

    std::cout << "Hello " << PlayerName << ", I want to play a game. You've been cuffed to a radiator.";
    std::cout << std::endl; //line space between text.
    std::cout << "There's a safe here with a key inside it to let you out of the handcuffs...";
    std::cout << std::endl; //line space between text.
    std::cout << "You need to solve the riddle to gain the codes in order to unlock this safe and continue to the next step of the game. Don't fail, your life might just depend on it." << std::endl;
    
    //player needs to input "OK" to proceed to the riddle.
    std::cout << "Type OK to continue" << std::endl;
    std::cin >> OK;

    //declare 3 number code
    const int CodeA = 2;
    const int CodeB = 2;
    const int CodeC = 3;

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

    //print sum and product to the terminal
    std::cout << std::endl;
    std::cout << "* There are three numbers in the code" << std::endl;
    std::cout << "* The codes add upp to: " << CodeSum << std::endl;
    std::cout << "* The codes multiply to: "<< CodeProduct << std::endl;

    std::cout << std::endl;
    std::cout << "ENTER NUMBER BELOW" << std::endl;
    std::cout << 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 the Guess Sum and the Guess Product equal to the Code Sum and Code Product, players have beaten part one.
    //else, the player has lost.
   if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << std::endl << "Congratulations " << PlayerName << " you have completed the first part of the game.";
    }
    else
    {
        std::cout << std::endl << "Looks like you've lost the game at the very beginning, " << PlayerName <<"... GAME OVER";
    }
    

    return 0;
}

Here’s my funky coloured code!

Love the idea of using the player’s name, great job!

My code after if else lesson… My story is a bit weird I know :smiley:

#include <iostream>


int main() //{return 0;}
{  
    // user defined variables declarations 
    int CodeA = 2;
    int CodeB = 4;
    int CodeC = 6;
    

    int CodeSum = CodeA + CodeB + CodeC;
    int CodeProduct = CodeA * CodeB * CodeC;
        // player inputs and input manipulation delarations
    int PlayerGuess;
    int GuessA, GuessB, GuessC; //  for player input comparison 
    int GuessSum, GuessProduct;
    // Intro to game in console
    // Golum is has locked the ring in a box and the box gives 2 numbers and you have to figure out 3 numbers that make up the 2. 
    std::cout << "You have started a game with Golum..." << std::endl << "You will DIE if you lose!\n" << std::endl;
    std::cout << "Golum approaches you \n " << std::endl;
    std::cout << "Golum: Give me the correct numbers, I NNNEEEEEEDDD them to get My Presious"<< std::endl;
    std::cout << "Golum: It wants 3 numberss...\n" <<std::endl; 
    std::cout << "Golum: The sum of the numbers are: " << CodeSum << " ,"<< std::endl;
    std::cout << "Golum: and the product of the numbers are: " << CodeProduct <<".\n I NEED YOUR ANSWER!!! Hurry up!"<< std::endl;
    // Player input for guess
    std::cout << "Player: My first guess is:";
    std::cin >> GuessA;
    std::cout << "Player: The second one is:";
    std::cin >> GuessB;
    std::cout << "Player: The third one should be:";
    std::cin >> GuessC; 
    GuessSum = GuessA+GuessB+GuessC;
    GuessProduct = GuessA*GuessB*GuessC;
   // std::cout << "Your 3 guesses are: " << GuessA << ", " << GuessB << ", " << GuessC << std::endl;
   // std::cout << "The sum of your guesses are: " << GuessSum << std::endl;
   // std::cout << "The product of your guesses are: " << GuessProduct << std::endl;
    if ((CodeSum == GuessSum) && (CodeProduct == GuessProduct))
    {
        if ((CodeA == GuessA) || (CodeA == GuessB) || (CodeA == GuessC)) // a
        {
            if ( (CodeB == GuessA) || (CodeB == GuessB) || (CodeB == GuessC) ) // b
            {
                if ((CodeC == GuessA) || (CodeC == GuessB) || (CodeC == GuessC)) // c
                {
                    std::cout << "Golum: YES, YES, YES!!! It worked..." << std::endl;
                    std::cout << "While Golum is basking in the aura of The Ring you make your escape...";
                }
                
            }
        }
         
        
    }
    else
    {
        std::cout <<"Golum: YOU STUPID HUMAN!!!(Golum starts running at you) DIE!!!\n (He attacks you with his bare hands and bites out your throut) \n You died" ;
    }
    
    

    return 0;
}
2 Likes

This is my code after the lesson. I tried something before but it would only make sense if we were trying to match the code in a specific order. Anyway, it was good practice. :slight_smile:

#include <iostream>

int main()
{
    // Print outs
    std::cout << "You're a secret agent breaking into LEVEL 2 of a server room" << std::endl;
    std::cout << "Your SuperHacker 2000 tells you the following information..." << std::endl;

    // Code numbers A, B and C
    const int CodeA = 4;
    const int CodeB = 6;
    const int CodeC = 1;
    
    // Sum and Mult variables
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProd = CodeA * CodeB * CodeC;

    // Print out sum and mult
    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: " << CodeProd << std::endl;
    std::cout << std::endl;
    std::cout << "Enter the code followed by an X (# # # X): " << std::endl;

    int GuessA, GuessB, GuessC;
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

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

    /* Trial number #1

    if (GuessA == CodeA || GuessA == CodeB || GuessA == CodeC) {
        if (GuessB == CodeA || GuessB == CodeB || GuessB == CodeC) {
            if (GuessC == CodeA || GuessC == CodeB || GuessC == CodeC) {
                std::cout << "The code is correct!";
            }
            else
            {
                std::cout << "Your code is incorrect! Initiating alarm sequence...";
            }
        }
        else
        {
            std::cout << "Your code is incorrect! Initiating alarm sequence...";
        }
    }
    else
    {
        std::cout << "Your code is incorrect! Initiating alarm sequence...";
    }
    */

   // Trial number #2
   if (GuessSum == CodeSum && GuessProd == CodeProd)
   {
       std::cout << "The code is correct!! Opening vault...";
   } else {
       std::cout << "Your code is incorrect! Initiating alarm sequence...";
   }
   

    return 0;
}

#include

int main()
{
// below is my story for the triple x game
std::cout << “Oh, no! You got busted by the Lucky Sevens Casino guards counting cards in Blackjack!”;
std::cout << std::endl;
std::cout << “The Casino owner is going to send you to sleep with the fishes unless you can guess HIS lucky numbers!”;
std::cout << std::endl;
std::cout << “Roll the dice to try your luck and you might just get out in one piece!”;

// declare 3 number code
 const int DieA = 5;
 const int DieB = 6;
 const int DieC = 3;

 const int DieSum = DieA + DieB + DieC;
 const int DieProduct = DieA * DieB * DieC;

// print sum and product to the terminal
std::cout << std::endl;
std::cout << "There are three dice used to make up the lucky numbers" <<std::endl;
std::cout <<  "The dice add up to: " << DieSum << std::endl;
std::cout << "The dice multiply to: " << DieProduct << std::endl;
std::cout << 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 == DieSum && GuessProduct == DieProduct)
{
    std::cout << "Winner! No sleeping with the fishes for you!!!";
}

else
{
    std::cout << "You did not guess the Casino Boss's lucky numbers...You're off to sleep with the fishes, too bad";
}

std::cout << std::endl;

std::cout << "Lucky Numbers is a production of VolTam games";


return 0;

}

1 Like

This is what my code looks like now that there are if and else statements. `
int main() {

//Print welcome messages to the terminal and introduce the game.
std:: cout << "Your rival has hidden away all of your belongings and travel plans in a foolproof safe! ";
std:: cout << "But is it truly foolproof? Not for a super-hacker legendary spy such as yourself!\n";
std:: cout << "It appears that the perpetrator of this crime has left some clues for you to solve...";
std:: cout << "Enter the code now!\n";

//Declare 3 Number Code
const int Code_A = 3;
const int Code_B = 5;
const int Code_C = 2; 

const int Code_Sum = Code_A + Code_B + Code_C;
const int Code_Product = Code_A * Code_B * Code_C;

//Print sum and product to the terminals.
std:: cout << std:: endl;
std:: cout << "There are three numbers in the code." << std::endl;
std:: cout << "The codes add up to: " << Code_Sum << std:: endl;
std:: cout << "The codes multiply to give: " << Code_Product <<std:: endl;

int Guess_A, Guess_B, Guess_C;

std:: cin >> Guess_A;
std:: cin >> Guess_B;
std:: cin >> Guess_C;

int Guess_Sum = Guess_A + Guess_B + Guess_C;
int Guess_Product = Guess_A * Guess_B * Guess_C;

  if (Guess_Sum == Code_Sum && Guess_Product == Code_Product) {
    std:: cout << "Kli-click! The code was correct and the safe is unlocked! Hurrah! Your travel plans are safe! You've certainly earned your vacation now!";
}
else {
    std:: cout << "Wait, why is something smouldering inside the safe... OH NOOOO!! You've failed and set off a trap that burned up your travel plans! GAME OVER";
}

return 0;

}

#include
using namespace std;

int main()
{
// Display a message to the user setting the senario of the game
cout << “You just entered the lab and see the safe where the unbtainium files are located…”;
cout << endl;
cout << “Use your mad scientist knowledge to input the correct codes to open the safe” << endl;

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

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

// Print CodeSum and product to the terminal
cout << endl;
cout << " + There are 3 numbers in the code" << endl;
cout << " + The codes add up to: " << CodeSum << endl;
cout << " + The codes multiply to give: " << CodeProduct << endl;

int GuessA, GuessB, GuessC;

cin >> GuessA;
cin >> GuessB;
cin >> GuessC;

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

if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
    cout << "You opened the safe!";
}
else
{
    cout << "You failed to open the safe and hear noises behind you...";
}

return 0;

}

This is my game so far.

Really enjoying the variety this simple game is producing :slight_smile:

See my code below

#include <iostream>

int main ()
{
    // Print welcome messages to the terminal
    std::cout << "Right recruit it is your turn to plot the star jumps for our ships simulation...";
    std::cout << std::endl;
    std::cout << "This may only be a simulation but you will need to get this right if you want to progress onto the real thing!";

    // Declare 3 number code
    const int JumpCodeA = 4;
    const int JumpCodeB = 3;
    const int JumpCodeC = 2;

    const int JumpSum = JumpCodeA + JumpCodeB + JumpCodeC;
    const int JumpProduct = JumpCodeA * JumpCodeB * JumpCodeC;

    // Print JumpSum and JumpProduct to the terminal
    std::cout << std::endl << std::endl;
    std::cout << "* There are 3 numbers for each jump"<< std::endl;
    std::cout << "* The numbers add-up to your start location coordinate:" << JumpSum << std::endl;
    std::cout << "* The numbers multiply to be your jump location coordinates:" << JumpProduct << 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 == JumpSum && GuessProduct == JumpProduct)
    {
        std::cout << "Well done you successfully completed your first Jump!";
    }
    else
    {
        std::cout << "You have jumped us into the middle of a star Cadet!\n";
        std::cout << "You have a long way to go before getting your wings!\n";
    }
    
    return 0;
}

Here is my code :slight_smile:

#include <iostream>

int main()
{
    std::cout << "You are the pokemon trainer who is gonna catch'em all!!"; std::cout << std::endl;
    std::cout << "But first you must enter the correct numbers on to your special pokeball...";
    std::cout << std::endl; std::cout << std::endl;
    
    int KodA = 2;
    int KodB = 8;
    int KodC = 9;
    int toplam = KodA + KodB + KodC;
    int carpim = KodA * KodB * KodC;
    int TahminA;
    int TahminB;
    int TahminC;

    std::cout << "+ There is three digit on your pokeball for three numbers!" << std::endl;
    std::cout << "+ Sum of these numbers is " << toplam << std::endl;
    std::cout << "+ Multiplication of these numbers is " << carpim << std::endl;
    std::cout << std::endl;
    std::cin >> TahminA;
    std::cin >> TahminB;
    std::cin >> TahminC;
    std::cout << std::endl;
    
    int TahminToplam = TahminA + TahminB + TahminC;
    int TahminCarpim = TahminA * TahminB * TahminC;
    
    if (toplam==TahminToplam , carpim==TahminCarpim)
    {
        std::cout << "Congratz, you caught that Pokemon !!"; 
    }
    else
    {
        std::cout << "Oh no, Pokemon fled !! ";
    }

    return 0;
}
#include<iostream>
#include<chrono>

int main ()
{
     //Use a double dash to begin a comment statement.
     //Print out welcome statements to the terminal.
    std::cout << "You have found a bomb in your school";
    std::cout << std::endl << "There is only 1 minute left before it starts to explode\n";
    std::cout << "You have no time to tell other people...\n";  
    _sleep(7000);
    std::cout << "But, you can shut it off by yourself if you enter a correct combination...\n";
    std::cout <<std::endl;

    //Declare 3 variables.
    const int CodeA = 3;
    const int CodeB = 2;
    const int CodeC = 3;

    int CodeSum = CodeA + CodeB + CodeC;
    int CodeProduct = CodeA * CodeB * CodeC;
    
    //Print out CodeSum and CodeProduct to the terminal.
    _sleep(5000);
    std::cout << "+ There are 3 numbers in the combination\n";
    std::cout << "+ They add-up to: " << CodeSum << std::endl;
    std::cout << "+ Their product is: " << 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 << "Congratulation! You've shut the bomb off";
    }
    else 
    {
        std::cout << "Boom! You died";
    };

    return 0;
}

Hello one and all! Just a snippet of my code from today’s lesson!

Here is my short code after finishing the lesson.

Haha, I played it – and won without looking at the answer too! 8D
Good job!

Mah code :stuck_out_tongue: :

#include <iostream>

using namespace std;

int main()
{
    int GuessA, GuessB, GuessC;
    const int CodeA = 4;
    const int CodeB = 5;
    const int CodeC = 6;
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;

    // text output
    cout << endl;
    cout << "a puppy is trapped by animal kidnappers" << endl;
    cout << "you can chose the save the puppy or leave." << endl;
    cout << "to save puppy, enter the right code to the lock." << endl;
    cout << "- there are three numbers in the code" << endl;
    cout << "- the code add up to: " << CodeSum << endl;
    cout << "- the code multiply up to: " << CodeProduct << endl;

    //player Guess
    cout << "Numbers only, press SPACE or ENTER for next number:\n";
    cin >> GuessA;
    cin >> GuessB;
    cin >> GuessC;

    // Math for Guess
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;
    
    // text output
    cout << "Sum of Your Guess: " << GuessSum << endl;
    cout << "Product of Your Guess: " << GuessProduct << endl;

    // player Guess condition
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        cout << "You took the puppy and ran away";
    }
    else
    {
        cout << "Wrong code";
    }

    return 0;
}



lol

Privacy & Terms