What main() does

I havent worked with C++ much but I’ve been coding for a few years in other languages so here goes…

“int main()”
the “int” preceding main signifies the return type of this function is an integer.

“int difficulty = 2;”
“int” defines the variable type as integer
the variable is set to 2

“int maxDifficulty = 10;”
the name suggests this will be used as a comparison value later

“while (difficulty <= maxDifficulty)”
while is the built-in function that runs for an indeterminate amount of time, until a condition is met.
the condition in this case is that the difficulty must be less than or equal to maxDifficulty for the loop to continue.

“PlayGameAtDifficulty(difficulty);”
“PlayGameAtDifficulty” is a function which is passed the value of the difficulty variable as an argument.
the function does not appear to return a value

“std::cin.clear(); // clears the failbit”
I’m assuming from the comment that the fail bit that is being cleared is when the players input is wrong

“std::cin.ignore(); // discards the buffer”
from the comment it seems this drops memory allocated from the previous run of “PlayGameAtDifficulty”, possibly to prevent buffer overflow

“++difficulty;”
increments the variable “difficulty” by 1

“std::cout <<”
once the value of “difficulty” has met the condition to break out of the while loop, a string of text is printed to standard output indicating the player has won the game

“return 0;”
the function ends returning the integer value of 0 to indicate there was no error during the run of the process.

Privacy & Terms