So, what do YOU think main() does?

I know it’s an odd question at this stage, but put yourself out there and give it a go. Here’s the code…

{
  std::system("clear");  // clears the terminal

  int difficulty = 2;
  int maxDifficulty = 10;
  while (difficulty <= maxDifficulty)
  {
    PlayGameAtDifficulty(difficulty);
    std::cin.clear();  // clears the failbit
    std::cin.ignore();  // discards the buffer
    ++difficulty;
  }

  std::cout << "WOW - You're a master hacker!\n";
  return 0;  // exit with no error code
}

By the way you block format code with three ` at the start and end

14 Likes

main () (Which is a function)is going to run all the statements within the { } also everything with ; is considered a statement. When compiling the program it will look for errors. if there are no errors the value will return 0. hmm looking at while statement I notice a ; is missing. Is that an error?

6 Likes

The main function is the control center of the code. Everything has to get sent back to main and then returned to 0 to compile. The main function controls what gets presented and how it gets it is presented when compiled.

Reading the code above it looks like we will clear the terminal to leave us a blank slate then we will start with a base difficulty of 2 that has a max of 10. If you haven’t reached 10 the game will locate what difficulty you are on and then clear your answer and ignore the buffer??

After that the difficulty will increment by one.

If you make it past the max difficulty you win the game and exit.

5 Likes

The game will play until the difficulty level equals the max difficulty . Until that happens, the playGameAtDifficulty function will run.

I don’t know what failbit is, but once difficulty passes maxDifficulty the “winners” message appears then the main function exits.

3 Likes

It calls people who beat the max difficulty master hackers and doesn’t allow people to stop (non-forcefully) until they are.

2 Likes

The main() function is the starting part for all c++ scripts, without it your code will simply not work.

It then cascades out into your own functions.

2 Likes

Specifically? Here’s my guess:

  1. Clears all text from the screen
  2. Sets the starting difficulty to 2
  3. Defines the max difficulty of 10
  4. Play’s the game, increasing the difficulty after each level
  5. Prints that you are a master hacker after level 10
  6. exits

Generally; main() is the first ‘function’ the program will look for and then calls out to other functions as it reads through main.

2 Likes

I think main is the main area of code that the computer reads. Anything outside of main is not run unless called on. In this case main is just setting some integers then running the game loop and telling the computer to run the function which holds the game itself.

1 Like

I think “main” is the centralized apparatus that sets and organizes the rest of the code into a cohesive executable formation that manages how it gets actualized.

1 Like

I studied a bit the programming language C, in C the function main () is the main function of the program, this function is the one that runs the terminal, the program can not run without the function main ().

1 Like

I’m learning C++ in this course, while having a strong foundation of the C# language.
I think that main() is the equivalent of Start() in C#, which will run at the start of the application.
1- It clears every text in the terminal in the beginning in case there were some text in the terminal from previous testing
2- The game will progress on levels of difficulty until you clear the max difficulty level
3- Finally the terminal will output the end game message and not run anything else

1 Like

Main() is there, so the program knows where the code starts.

What I suppose the mentioned code does :

  • As comment says, it begins by clearing logs from the console (or terminal).

  • The two following lines are variables. Their use is to set up the actual and maximum difficulty.

  • Then it starts a loop. As long as the max_difficulty isn’t exceeded :

    • It launches the function that allows you to play at the current difficulty.
    • As we don’t have the information of the (PlayGameAtDifficulty) results, I’m supposing you can only go further if you write the correct input.
    • The next two lines are a bit difficult to me to understand but I guess it is there to prevent errors from the upper function?
    • It then increments (+1) the actual difficulty, and then the loop starts again until maxDifficulty is exceeded.
  • When you pass the max difficulty, then console writes "“WOW - You’re a master hacker!” and the game exits.

Sorry if answer is too long

1 Like

In the main () function there are two variables at startup, the first variable is to determine the level in which you start, the second variable is used to determine which is the last level. Then there is a cycle while (), this cycle what it does is to repeat the game while the current level is less than or equal to the last level to play, if one is wrong in the code to be introduced then you remain at the same level, if the code is correct then the difficulty increases by one. std:: cin.clear (); This line what it does is to clean the buffer what has been written before to avoid errors, std:: cin.ignore () This line what it does is ignore what is in the buffer to avoid entering previously written information.

1 Like

The main method is the method that is actually called by the program that then calls the other defined functions or methods.

1 Like

I honestly don’t really know what the main() function does but I think it basically runs all the statements between the { and } statements. Other than that i don’t know

1 Like

not sure at this stage, I think there are 10 levels of difficulty, I think this part is if you are successful in getting the code and it makes a means of going to the next level, there’s a return at the end I think to go to the next level

1 Like

At pretty a much assuming that I’m ‘educated guessing’ level:

  1. It clears the terminal allowing a clean space for the game to run and user inputs to be made.
  2. Chooses the range of difficulties available for the game, the total number of ‘levels’
  3. Not really any idea what the indented bit does, It looks to me like it is what happens if you fail (maybe even if you win), eg it clears what you typed in as an answer and later on in the code it spits out an in story reply to you as the player
  4. This looks like what happens if you ‘win’ and so the numbers you selected are the correct numbers the code is looking for and so it gives you a “WOW - You’re…” message and then return 0; either finishes the game or maybe starts a new one
1 Like

As a whole, the function of the main() is to be the function that actually executes the game so you can play it, whereas everything written before it is defining what happens when the game is executed. It also increases the difficulty level once a level has been cleared, determines the win condition, and cleans up the terminal so the text doesn’t clutter the screen.

1 Like

Main function is the backbone of the code, it runs the whole program by executing functions called inside it.

1 Like

It starts by declaring the two margins of difficulty possible: you start at a minimun difficulty of 2 and every time you succed it increases that value until it reaches 10.
Then you exit the while loop and you get printed “u’re a master hacker”.
The function PlayGame… require the parameter difficulty and on top of that it shapes the numbers that you are going to guess.
cin clear and ignore wipe out and clean the input window

1 Like

Privacy & Terms