So, what do YOU think main() does?

So my take on this is -

Difficulty starts at level 2 and increases to a maximum difficulty level of 10. There is a condition that states "while difficulty is less than or equal to the maxDifficulty, play the game.

When the game is played, the code resets to its empty value and moves forward to the next part. The difficulty increases if the level is beaten and the script runs again until the maxDifficulty level is exceeded.

If the difficulty exceeds the maxDifficulty, it will run to a script that the player can view saying they completed the game and then exits.

as long as the maximum difficulty of 10 has not been reach, the game will run and update the difficulty by one. When maximum difficulty exceeded 10, the game will exit without an error where the player has won.

I’m not gonna lie to anyone here, I have looked at this code and I have no idea what the main does but I really hope I am going to learn.

Accidentally made my own post before I realized there are actually premade posts for the particular course. I’ll delete that one and move everything to the correct forum posts.

{
  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
}

So I think it defines the starting difficulty (don’t know why that’s 2 instead of 1).
Then it declares what the maximum difficulty will be.

Then it will let you play the game as long as the current difficulty is lower than or equal to the maximum difficulty. Not really sure what’s happening about the failbit and buffer part. It will increase the difficulty after every round though.

Once the current difficulty exceeds the maximum difficulty, the game will end and you’ll be told that you’re a master hacker.

I believe that the main-function is the program itself. It will run anything that is placed (indented) inside the curly brackets. I think that the example code will set the difficulty to 2 and the max to 10. The game runs inside the while-thing and when completed it will return 0.

I’m actually a web developer. So, I’m not completely new to the world of programming however, since, game development is something I have decided to kind of explore as a new interest of mine, I have chosen to take this course. And yes C++ is a language I’m fairly new in.

So here’s my current understanding of the code:

  • The main() is the the entry point of a c++ program if I am not wrong.
  • std::system("clear"); - I’m not sure what std is here. Probably a package or something that comes with some functions and system is one of those functions that takes a string argument that tells the function what to do and in this case it is clearing the terminal.
  • int difficulty = 2; and int maxDifficulty = 10; are just two integer variables initialized with the given values.
  • while (difficulty <= maxDifficulty) - beginning of a while loop with a condition that says the the code in the while block will execute in a loop as long as the condition is “true”, i.e., difficulty is less than or equal to maxDifficulty.
  • PlayGameAtDifficulty(difficulty); - a function that does something and judging by the name it just executes code that plays the game at the given or passed difficulty value.
  • std::cin.clear(); and std::cin.ignore(); probably does what the comments beside them say although I don’t know how to explain that properly.
  • ++difficulty; basically just increments the difficulty value by 1 and then the control of the program goes back to the start of the while loop and the condition is checked to see if the diffculty is less than or equal to maxDifficulty. If yes then the code in the loop will run again. If not then the loop ends and the control comes out of the loop to execute the code after the loop.
  • std::cout << "WOW - You're a master hacker!\n"; - probably just outputs the string inside the double quotes in the terminal. Although the syntax for doing this is something I’m not familiar with. (is << an operator? Not sure what that is)
  • return 0; - just returns the integer value - 0 when the function - main finishes execution. After reading the comment beside it, I think if the value is something other than 0 then it probably means there were some errors

Well, I took C classes before. Now, I want to learn Unreal Engine and C++. I am taking another course on C++ to get comfortable with it. So, maybe all this may help me a little bit more to answer the question, but for sure I still have more than a lot to learn and I am wrong at least a few times in my answer.

First, the main function is essential to any program. It is where all C/C++ program start. In this case, I see the main function follows the algorithm:

  1. Clear the terminal, so if there is any text before you start the game, it will all be cleared. Should it be std::system("CLS"); for Windows or it works anyway?
  2. Sets the variables the program will use (difficulty and maxDifficulty both as integers).
  3. Starts a loop and keep running as long as the current difficulty is equal or less than the maxDifficulty.
    a. Run the PlayGameAtDifficulty function passing the variable difficulty as an argument.
    b. When control returns to the main function, it will clear and flush the buffer (I am still confused of what this means, but I think it refers to the input stream) and add one to difficulty.
  4. When the loop stops, print to the console the message “WOW - You’re a master hacker!”.
  5. Finally, return 0. This means that the program ran without any errors.

Main() is a function that contains all the C++ code that compiles on the main screen of our game.

This includes ramping up the difficulty as they solve levels. It also presents the “Win” condition when the player completes the last level.

I am more familiar with C# and not very familiar with C++, so if I had to guess, I would think that the main() function is similar to the start and update functions of C#.

Best guess - it is the starting / main entry point of every program you write in C++.
In order for our program to run it must have a main function, without a main fuction our program will not run. (OK, I admit I have done a bit of programming in VB and C# - but I am no expert).

Main() function allows us to create statements or write statements which later on help us create another for creating a program.

Okay, first time outside of C# or Python, but here goes. I’m pretty sure the main method is what will run if it’s the main function in the code file and it runs what you tell it to in the body which includes functions/methods you define below as well as ones you use from ones you have in the header (where the #include lines are). I think I’m right but, again, this is my first time branching out of Python and C# (both of which are fairly similar in many aspects.)

main() is giving the starting point of the primary function. however it needs to be defined in order to have a purpose and knows what to do.

It does the following things, in order:-
1)Clears the log screen, in case there is any previous outputs.
2)Executes PlayGameAtDifficulty() function
3)Increments the difficulty by 1
4)Repeats step 2,3 if difficulty is not greater than maxDifficulty
5) If difficulty is greater than maxDifficulty print “WOW - You’re a master hacker!”
6)Exit the main function without returning anything.

It seems like int main() begins a new section of code, that is a group of functions that operate inside what int main() defines. So anything in that section goes back to main(), but other sections, possibly also begun with more main(), function separately??

To be honest this is my first time learning about C++ at this very moment I’m learning javascript and React and I do not have an idea about what main() means!

The main() function, I think, is literally the main part of the code. For each level that you complete, the while statement in the main() function gains one value, and you proceed to the next level. When you finish the game, the while loop breaks, and you get the final message.

Hi Ben,

I would guess that main() is a function sets, checks and progresses the difficulty of the game.

It is also updating the player with lines of text on how they are doing.

Looking forward to digging into the rest of the course!

Chris

main function is the first function the program search to run a script, it is here where all the program starts and ends, in this case, this main function instantiate the variables for difficulty and max difficulty and then it creates a while loop where the function “PlayGameAtDifficulty” is called and that function I assume is the game we are gonna play, this while runs until the player reaches the max difficulty, and when the player finish the max difficulty it prints “WOW - You’re a master hacker!” and finish the program

Hello there!
I think main() function is the point where our program will launch. So looks like a very beginning place where all staff from our program will start. 'Cause the first lines of includes will be transform to .cpp code during preprocessing

Privacy & Terms