My code after finishing the if and else statements lesson

    //Three numbers that we should find
    int NumberA = 5;
    const int NumberB = 10;
    int NumberC = 15;

    NumberA = NumberB - NumberC;

    //Summation and Multiplication of these three numbers
    int SumOfNumbers = NumberA + NumberB + NumberC;
    int MulOfNumbers = NumberA * NumberB * NumberC;

    cout << "There are 3 numbers you have to find!" << endl;
    cout << "Summation of 3 numbers are: " << SumOfNumbers << endl;
    cout << "Multiplication of 3 numbers are: " << MulOfNumbers << endl;

    int PlayerGuessA, PlayerGuessB, PlayerGuessC;

    cout << "What are your guesses: ";
    cin >> PlayerGuessA >> PlayerGuessB >> PlayerGuessC;

    int GuessedSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;
    int GuessedMul = PlayerGuessA * PlayerGuessB * PlayerGuessC;

    if (SumOfNumbers == GuessedSum && MulOfNumbers == GuessedMul)
    {
        cout << "YOU WIN!!" << endl;
    }
    else
    {
        cout << "YOU LOOSE!" << endl;
    }

Here is my compiled game, with correct and then incorrect answers.

#include <iostream>

// Numbers game
int main()
{
	// Game story
	std::cout << "The treasure awaits...";
	std::cout << std::endl;
	std::cout << "You just have to crack the code to the safe...";

	// variables declarations
	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 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-up to: " << CodeSum << std::endl;
	std::cout <<"The codes multiply to give: " << CodeProduct << std::endl;
	
	// Player guesses
	int GuessA, GuessB, GuessC;
	std::cin >> GuessA;
	std::cin >> GuessB;
	std::cin >> GuessC;

	// Check if player correct
	int GuessSum = GuessA + GuessB + GuessC;
	int GuessProduct = GuessA * GuessB * GuessC;

	if (CodeSum == GuessSum && CodeProduct == GuessProduct)
	{
		std::cout << "You win!!!";
	}
	else
	{
		std::cout << "You lose ...";
	}

	return 0;
}
1 Like

#include

int main()

{

// Print welcome messages to the terminal

std::cout << "You are a ninja at the entrance of a sacret tample.";

std::cout << std::endl;

std::cout << "You need to solve a puzzle in order to open the gate... " << std::endl;    

// Decalare 3 number code / lever possitions

const int LeverA = 4;

const int LeverB = 3;

const int LeverC = 2;

const int LeverSum = LeverA + LeverB + LeverC;

const int LeverProduct = LeverA * LeverB * LeverC;

// Print LeverSum and LeverProduct to the terminal

std::cout << std::endl;

std::cout << "-> There are three levers with nine positions. Each position has a number from 1 to 9. " << std::endl;

std::cout << "-> The position numbers add-up to: " << LeverSum << std::endl;

std::cout << "-> The position numbers multiply to give: " << LeverProduct << 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 == LeverSum && GuessProduct == LeverProduct)

{

 std::cout << "Well done, Ninja! The gate is open!";

}

else

{

 std::cout << "Oh no, you got it wrong! Now the gate is shut forever!";

}

return 0;

}

1 Like

My code is base on TripleX but i wanted to go a different direction with my narrative. I call my game “selfdestruct”

// |**********************************************************************
// * Project           : Lesson 2 based on TripleX course.
// *
// * Program name      : selfdestruct
// *
// * Author            : morggin@gmail.com (Billy F. Jameson Jr.)
// *
// * Date created      : 5/4/2019
// *
// * Purpose           : Small C++ number guessing game.
// *
// * 
// *
// *  
// *  Revision 1.1 made on 5/4/2019 to fix spelling errors.
// *
// |**********************************************************************
#include <iostream>
#include <ctime>

const int MaxLevel=5;
int FailedCode=0;
void SelfdestructSit()
{
     // Print situation to the terminal
    std::cout << "You are onboard a space ship.\n";
    std::cout << "You are jarred awake by a dreadful sounding alert.\n";
    std::cout << "Along with the blaring sirens, an automated message is broadcast through out the ship.\n";
    std::cout << "The self destruct sequence has been activated. Please proceed to an escape pod.\n";
    std::cout << "Desperately you seek out the nearest escape pod.\n";
    std::cout << "You find yourself suffering from apoplexy as a futile search has revealed all 24 escape pods have been jettisoned.\n";
    std::cout << "You realize you have to abort the self destruct seqence.\n";
    std::cout << "Frantically you scramble to the bridge of the ship and take a seat at the main terminal.\n";
    return;
}

void PrintTerminal(int Difficulty)
    {
        std::cout << "\n\nThe main terminal flickers to life\n";
        std::cout << "Please enter Security Sequence: " << Difficulty << " of " << MaxLevel << "\n";        
    }

bool PlayGame(int Difficulty)
{
    PrintTerminal(Difficulty);
    // Declare 3 number code
    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 CodeProd = CodeA * CodeB * CodeC;

    // Print CodeSum and CodeProd to the terminal
    std::cout << "\n\n";
    // std::cout << " - code is "<< CodeA <<" "<< CodeB <<" "<< CodeC <<" -\n";
    std::cout << "+There are 3 codes in the security sequence\n";
    std::cout << "+The security codes add up to: "<< CodeSum <<"\n";
    std::cout << "+The security codes multiply to give: " << CodeProd <<"\n"; 
    
    // Store player guess
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;

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

    // Check if the players guess is correct
    if (GuessSum == CodeSum  && GuessProd == CodeProd)
    {
        std::cout << "\n\n+You have deactivated the self destruct code for sequence: " << Difficulty << "\n";
        return true;
    }
    else
    {
        std::cout << "\n\n                            YOU FAILED TO ENTER THE CORRECT SECURITY CODE\n\n";
        ++FailedCode;
        return false;
    }
}

int main()
{
    srand(time(NULL)); // Create new random sequence based on time of day.

    int LevelDifficulty = 1;
    SelfdestructSit();

    while (LevelDifficulty <= MaxLevel)  // Loop the game till all codes are found or ship is destroyed.
     {
         bool bLevelComplete = PlayGame(LevelDifficulty);
         std::cin.clear(); // Clears any errors.
         std::cin.ignore(); // Discards the buffer.

         if (bLevelComplete)
         {
             ++LevelDifficulty;
         }
         if (FailedCode == 3)  // Game ends in self destruction.
         {
             std::cout << "    The terminal window shuts off and the keyboard becomes unresponsive as intense heat begins to build.\n\n";
             std::cout << "In a bright blinding flash           *** THE SHIP EXPLODES ***      spreading your remains all over the cosmos.\n\n";
             std::cout << "                                           GAME OVER MAN        \n\n";
             std::cout << "                                                 *                \n";
             std::cout << "                                                 *                \n";
             std::cout << "                                                 *                \n";
             std::cout << "                                  *              *              * \n";
             std::cout << "                                    *            *            *   \n";
             std::cout << "                                      *          *          *     \n";
             std::cout << "                                        *        *        *       \n";
             std::cout << "                                          *      *      *         \n";
             std::cout << "                                            *    *    *           \n";
             std::cout << "                                              *  *  *             \n";
             std::cout << "                             * * * *G*A*M*E* * * * * * *O*V*E*R* * * *\n";
             std::cout << "                                              *  *  *             \n";
             std::cout << "                                            *    *    *           \n"; 
             std::cout << "                                          *      *      *         \n";
             std::cout << "                                        *        *        *       \n";
             std::cout << "                                      *          *          *     \n";
             std::cout << "                                    *            *            *   \n";
             std::cout << "                                  *              *              * \n";
             std::cout << "                                                 *                \n";
             std::cout << "                                                 *                \n";
             std::cout << "                                                 *                \n";
             std::cin.get();
             exit(0);
         }

     }
    std::cout << "\n*** Great work! You saved the ship! ***\n"; 
    std::cout << "You dock at the next space station and stroll into the nearest seedy tavern \"The Boastful Blaster\" for a nerve calming drink!\n";
    std::cin.get();
    return 0;
}
1 Like

Good job! :fire:

This is my code:

#include<iostream>

int main()
{
	//introduction
	std::cout << "An unidentified alien intelligence is trying to invade the earth...";
	std::cout << std::endl;
	std::cout << "The only way to defeat them is to blow up their control room, the problem is that it is highly secured...";
	std::cout << std::endl;
	std::cout << "There will be several codes to crack. As earth's best, you have been chosen...";
	std::cout << std::endl;
	std::cout << "Good luck!";

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

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

	//print to screen
	std::cout << std::endl << std::endl;
	std::cout << "There are three numbers for this code:" << std::endl;
	std::cout << "+ The sum of the codes are: " << CodeSum << std::endl;
	std::cout << "+ The product of the codes are: " << 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 << "3..." << std::endl;
		std::cout << "2..." << std::endl;
		std::cout << "1..." << std::endl;
		std::cout << "0" << std::endl;
		std::cout << "Boom!!!!!!!" << std::endl;
		std::cout << "You won! You have saved humanity and have pierced the hearts of any alien race trying to invade!";
	}
	else
	{
		std::cout << "Red Alert! Red Alert! Securing Control Room Hatch and personal quarters! Self destruct ship in 3... 2... 1... Boom!" << std::endl;
		std::cout << "You managed  to escape but couldn't destroy control room or any aliens. Humanity must meet its doom!";
		std::cout << std::endl << "Game Over!";
	}

	return 0;
}

Output when correct:

Annotation 2020-06-14 110124

Output when incorrect:

Here is my code

#include <iostream>

int main()
{
    //Declare welcome messages to the terminal
    std::cout << "You are a secret agent breaking into a secure server room";
    std::cout << std::endl;
    std::cout << "You need to enter the correct codes to continue...";
    std::cout << std::endl;
    //Declare a 3 number code
    const int CodeA = 4;
    const int CodeB = 2;
    const int CodeC = 3;

    /* This is a multiline
    comment*/

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

    //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 numbers add up to: ";
    std::cout << CodeSum << std::endl;    
    std::cout << "The numbers multiply to: ";
    std::cout << 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 << "Welcome";
    }
    else
    {
        std::cout << "Prepare for termination";
    }
    

    //format space
    std::cout << std::endl;
    return 0;
}

Here’s mine!

My code terminal results as it stands right now.

1 Like

Great work everyone! :clap:

#include <iostream>

int main()
{
    // Print welcome message to the terminal
    std::cout << "You are an archeologist that've just uncovered a secret tomb in ancient Babylon.";
    std::cout << std::endl;
    std::cout << "To open it you should pass an exam of ancient mathemathitians:" << std::endl;

    // Declaration of 3 number code
    const int CodeA = 4;
    const int CodeB = 7;
    const int CodeC = -5;

    /* Just some multiline comment
    */

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

    //Printing the result
    std::cout << std::endl;
    std::cout << "The are three numbers in a code" << std::endl;
    std::cout << "And they add-up to: " << CodeSum << std::endl;
    std::cout << "While thir product is: " << CodeProduct << std::endl;


    std::cout << std::endl << "Please, enter your guess of three numbers, each esparated by at least one space: ";
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessA + GuessB + GuessC ; 
    int GuessProduct = GuessA * GuessB * GuessC ;

    if (GuessSum==CodeSum && GuessProduct==CodeProduct)
    {
      std::cout<<"You've won!";
    }
    else
    {
      std::cout<<"Oh no!...The code is wrong and the puzzle is reset. All the progress is lost...";
    }
    

    return 0;
}

late post

With if and else statements

here is my code after i finish my game

image

Privacy & Terms