As of C++ execution structure, main function is the first function to be called (pushed into the execution stack) and continue with the execution according to what's on top of the stack
According to what’s in the main() explains that (it is already self explaining)…
Initially the system terminal where this program executes is cleared using the std::system("clear")
. Then two integer variables were created with default values 2 and 10. After that a while
loop is initiated, means it loops the instructions written inside its curly braces “{}” until it’s condition is satisfied. That is difficulty <= maxDifficulty
.
Inside the loop
An another function call takes place, means PlayGameAtDifficulty(difficulty);
is pushed into the top of the execution stack which takes in an argument. Now the execution control is onto what’s inside that function and hopefully if no other function calls happens inside PlayGameAtDifficulty(difficulty)
and upon it’s completion its is popped from execution stack and execution continues from what’s on top of it, in high a level saying, the instruction will be std::cin.clear();
.
The cin
represents the input stream object (consider it as a one way buffer for now) and is used to take input from streams like files, consoles. As the comment says clearing the fail bit ensures proper future I/O operations and also discarding the buffer is also a good practice to
avoid unnecessary data along with your inputs. This implies that PlayGameAtDifficulty(difficulty)
will have a cin
operation. Lastly the ++difficulty
increases the variable’s value by 1 on each iteration.
The loop will continue to execute until the condition satisfies
After the loop
A string is outputted onto your terminal using cout
(similar to cin except that data is flowing to the terminal ) which represents the output stream object .
Lastly the execution terminates by returning 0, states that the program executes successfully.