Triple X Section 13: My take on the main function

Hey all! I just got started on the Unreal C++ course, and I wanted to take on the first challenge to explain the main function. I am familiar with coding, and I have a bit of C++ experience so it all makes sense to me except for the cin.clear and cin.ignore method calls - I’ve never seen those before, but I’m guessing it’s to clear some sort of memory buffer?
The rest is pretty straight forward for me - declaring difficulty and maxDifficulty variables, and looping through, incrementing difficulty until difficulty <= maxDifficulty. In each iteration, we call the PlayGame function, passing the current difficulty level.
The two things that are unclear to me at the moment is a) the cin.clear and ignore, as mentioned above, and b) why Ben is opting to use the pre-increment on difficulty: ++difficulty. As I understand the increment operator, pre-increment basically increments the variable, and then evaluates the current expression. Post (difficulty++) first evaluates the expression, and then increments the variable. But since it’s a single line, it seems like either way the result will be the same. Just curious choice to use pre-increment. I hope he covers that. :smiley:

-Carlos

int main() 
{
    int difficulty = 2;
    int maxDifficulty = 10;
    while (difficulty <= maxDifficulty)
    {
        PlayGameAtDifficulty(difficulty);
        std::cin.clear();
        std::cin.ignore();
        ++difficulty;
    }
    std::cout << "WOW - You're a master hacker!\n";
}

1 Like

Great job going into detail with everything. You really articulate well about the stuff you understand in C++. That is a great question why they use ++difficulty and not difficulty++. Most of the time I use post-increment.

1 Like

Privacy & Terms