So, what do YOU think main() does?

I would guess Main () function is the command under which you have to include a task to run in a game, if that makes sense. This is just a guess as I don’t really know what purpose does Main() serve.
I am quite sure that it is not the correct answer, but like you said I am recording my starting point, so I can look back later and see how much more I improved and know by the time I finish the course.

Note to future self: Well done if you completed the course and can now do all the things taught in the course on your own! :smiley:

@ben main() is the starting point of the code. The operating system executing the code starts from the main function, while the other functions are ordered by how they are called by the main() function or a function that was called by main or so on.

main means the head of the code or something. seriously i have no idea

So i’ve been using c++ for some time now, but never really asked myself the question of what does main do, for me it’s the “main” function, it’s the function that is executed at first, it will involve some commands for closure of the program like cout commands and other function are being called from there!

I have no idea, but i think main gives instructions to the rest of the code on how to behave.

This function sets the beginning difficulty of the game to 2, and the max difficulty to 10. Then it sets up a loop that runs the PlayGameAtDifficulty function with the current difficulty setting. If the player passes the level it clears the terminal input/output and increases the difficulty level by one. It repeats this until the player clears the max difficulty level then it gives a congratulations message and exits the program.

Hi, I’m Daniel, my English may be rusty, I apologize for any spelling errors, but these are my thoughts on the main() function based in the code.

Well, as I have a basic knowledge of C ++, The main() just is the function that will execute all the work, if everything is written correctly. I think the first line is performing a screen cleaning, as it is the first line to be executed and is a function, I think that code will execute every time the code starts, again and again.

The code sets 2 variables that will be used by the while condition, both are of the int type, that will store numbers that are integers in the code. The While will compare the variables and will execute three things that i don’t know what they do, i have a idea that they delete and discards something, and add 1 more difficulty.

And at the end of the code I see a text to be shown on the screen with a line break, if I’m right, this will be shown at the end of the level.

Main function…main()

The main function is a line of code that the compiler looks for to start the execution of the code/program. It basically tells the compiler the program starts executing at this line, Main(){}

int main() is the point of entry to the program. You can pass parameters to the program inside the brackets of main() and it returns either a 0 signifying successful execution or an error code

main() is the place where lines of code is executed one by one from top to bottom during a frame.

Having no prior knowledge of c++ I believe that main() is perhaps the controller of the code above. The difficulty appears to rise a level every time you complete the previous level and at level 10 the game is complete.

in c++ main is the main funtion of program where from program is start and exucute other fuction and statement what is in it

What I think main does is declare the integers for difficulty and max difficulty and makes sure that the difficultly cant exceed the max one or makes sure the game ends when the difficulty exceeds or equals the max number. And at the bit bellow which is indented says to start the game and clear the answer if it is incorrect and don’t proceed to do anything. And then the final bit says what to say when the answer entered is correct and possibly says to restart to a higher difficulty.

Coming from a C# Unity background, i asume main() is the first function that will execute inside this main class where you can initialize variables and execute other functions. It is also a void function, that’s why it returns 0.

These functions seem to be related to the terminal, either to write on it or to clear it

std::system(“clear”); //clears terminal
std::cin.clear(); //don’t know, maybe clears current line
std::cin.ignore(); //don’t know
std::cout //writes new line

This is a basic loop to generate the levels based on difficulty, and each time after PlayGameAtDifficulty() is called, the difficulty increases by 1 and continues with the loop until maxDifficulty is reached.

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

If the condition is reached and you exit the loop, the game will congratulate you with a message, then it will exit.

std::cout << “WOW - You’re a master hacker!\n”;
return 0; // exit with no error code

The function “main” will be the one that will be executed, in this specific one there are some variables made to give an idea of where the player arrived and when he wins and it uses another function which is “PlayGameAtDifficulty” which takes the current game’s difficulty as an argument and uses it to do specific operations. the “main” function also does some other operations such as printing when the player wins.

I think the main block of code sets up the program to run. Like instructing the game what to do depending on your response.

I think main() indicates where the program execution starts, and I guess that the code shared by Ben does the following: It starts by clearing the terminal, then it put the starting and the highest difficulty level. It continues with a statement that executes what’s inside the curly brackets (PlayGameAtDifficulty function and then the difficulty is increased to check the condition again) while the current difficulty is lower or equal to the max difficulty. When the difficulty is higher than the max established, it ends.

I think it includes foundation code the initial setup of something a game or app as an example.

From reading it its setting up difficulty then the cap then loads a function called PlayGameAtDifficulty which has some instructions of the game then depending on your answer sends a you win message.

I think main() brings together the other functions (if thats what they are called) together, kind of like the index to a book listing all the chapters?

The main() is the algorithm of the levels that the games has. This algorithm shows the minimum level and maximum levels. the game goes on until the difficulty reaches the maximum difficulty/

Privacy & Terms