Hello!
I know there are similar topics, and I tried to find the answer, but I’m not managed to do it.
Here is my question related to “27. Function Parameters” video:
Below is the code which I have as on lecture №27 and I totally unclear with how the program knows that “Difficulty” should be “1” then after increasing the level - “2” etc.
I was confused because we just put this word as a function parameter, but didn’t initialize it somehow.
However, we initialize the parameter “Difficulty” and incremented it. It was clear to me. But I still don’t understand, how my program knows that there should be “1”, “2” etc after the words "You are secreting agent breaking into a level "
Please help to understand what makes the parameter “Difficulty” appears in Terminal as a number and what increments this parameter?
#include <iostream>
void PrintIntroduction(int Difficulty)
{
// Print welcome messages to the terminal
std::cout << "\n\nYou are a secrete agent breaking into a level " << Difficulty;
std::cout << " secure server room...\nEnter the correct codes to continue...\n\n";
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
// Declare 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 CodeSum and product to the terminal
std::cout << "+ There are 3 numbers in the code";
std::cout << "\n+ The codes add-up to: " << CodeSum;
std::cout << "\n+ The codes multiply to give: " << CodeProduct;
std::cout << std::endl;
// Store player guess
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
// Check if the player's guess is correct
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\nYou win!";
return true;
}
else
{
std::cout << "\nYou lose!";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
while (true)
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); //Clears any errors
std::cin.ignore(); //Discrards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
return 0;
}