So, what do YOU think main() does?

The main function in every program (and most, if all not programming languages), is the start and finish of our program. It is in other words, the body. But the body, needs arms, legs, brain, organs to function properly. So all the other functions, all the other code we write, that is the juicy stuff, that is the interesting part of the program. They tell the body how to function, what to do and how to do something.

The main function, just gives us a map to follow. Which function will be called first, what is the next instruction, etc. until it sometime ends and the program finishes.

main() probably is the main source of code for the whole difficult of the game. I think maybe it is the code that increases the difficulty as you pass the levels. It has the starter difficulty & the max difficulty. I think maybe it revolves mainly around the difficulty of the game, as you progress. I think? haha

main() is, just as its name say, the main function for a program, it’s mainly used to tell what scripts to start but in this case, for a terminal game, it’s used to ramp up the difficulty and print text when the answer is correct.

I think the main() function provides an access point, or ‘doorway’ into the code; it additionally allows optional parameters to be passed into the program. I think the main() function provides a necessary adherence to a programming protocol whereby the main() function is always called by default irrespective of who has produced the code, thereby establishing a preferred convention to a limitless toolset.

The main function initiates all of the functions that a program/ application uses, working as an overarching driver at the top of the code hierarchy. Main is in all applications that runs within the .Net framework the function that is called first, before the rest of the code is executed and said code has to be directly or indirectly called by the main function, since the main function is the absolute starting point for all code that needs to be run by the application.

//Hortensea

so I have studied the basics of c++… a few months ago and Im trying to get back to it after a huge amount of work, so I think the main function is the primary function in the whole section of the code, and , there could only be one of them , and… I don’t remember much more than that at the moment.

The main() function execute the function above.

I believe the main function is the starting point for the code. All code starts right after the main function, and must stay within the main function to run properly.

1 Like

may be it controls the rules of the game… :roll_eyes:

In response to section 2.14 of the C++/Unreal engine course, my guess is that the main() function is where you put most of the code for the things you want the game to be. Judging by the Triple X example and how the modifier to the game (the difficulty setting) was not in the main function but instead was just adding to it, my thinking is that main() is where all the action happens and the other funtions just add conditions to whatever is in main().

Hopefully this is close? Either way I’m looking forward to returning to this and seeing how much I’ve progressed! Happy to be a part of this community and excited to be getting into this crazy new world!

It look to me like the top of the code determines the difficulty. the middle removes the failure message then increases difficulty. once the difficulty is >=10 the game gives you the win message. (in this case “WOW - Youre a master hacker”) after then win message is shown you can then press enter to close the game.

The main function gives the game a base difficulty of two, which can increase to a maximum of ten. This function allows the player to level up if they get the correct answer and then gives them the win message and lets them exit once they have beaten the game at maximum difficulty level. (I think, not sure though)

First it sets basic variables for the while function which determines the duration of the game. Than it iterates all states of the game. If you win the game you get a message about being a master hacker.

My guess is the main() function execute all the different code that we have written above

I think what main does is it sets up the conditions of the game. Anotherwards, the difficulty that is going to be used and what is presented on the screen to the player: Clearing previous answers that were given on previous levels.

I understand the main function as the only function recognized by the system as being the collection of code instructions necessary to successfully run the program and return an integer.

The main() function processes all the statements written before. It executes the main statements located in the difficulty functions written above. I believe? Very new to all this.

As of C++ execution structure, main function is the first function to be called (pushed into the execution stack) and continue with the execution according to what's on top of the stack

According to what’s in the main() explains that (it is already self explaining)…
Initially the system terminal where this program executes is cleared using the std::system("clear"). Then two integer variables were created with default values 2 and 10. After that a while loop is initiated, means it loops the instructions written inside its curly braces “{}” until it’s condition is satisfied. That is difficulty <= maxDifficulty .

Inside the loop
An another function call takes place, means PlayGameAtDifficulty(difficulty); is pushed into the top of the execution stack which takes in an argument. Now the execution control is onto what’s inside that function and hopefully if no other function calls happens inside PlayGameAtDifficulty(difficulty) and upon it’s completion its is popped from execution stack and execution continues from what’s on top of it, in high a level saying, the instruction will be std::cin.clear();.

The cin represents the input stream object (consider it as a one way buffer for now) and is used to take input from streams like files, consoles. As the comment says clearing the fail bit ensures proper future I/O operations and also discarding the buffer is also a good practice to
avoid unnecessary data along with your inputs. This implies that PlayGameAtDifficulty(difficulty) will have a cin operation. Lastly the ++difficulty increases the variable’s value by 1 on each iteration.

The loop will continue to execute until the condition satisfies

After the loop
A string is outputted onto your terminal using cout (similar to cin except that data is flowing to the terminal ) which represents the output stream object .
Lastly the execution terminates by returning 0, states that the program executes successfully.

I last programmed in college (last century), so here goes. The main function executes a group of statements, then returns a value when completed. In this instance, it looks like the statements initiates some variables, sets up a loop structure, increments a counter of some sort, then defines an exit code that gets returned to the main() function. Not sure if this all makes sense. :slight_smile: Thanks!

This main function will;

  1. First execute a system command to clear the existing terminal prompts from the screen.

  2. Then initialise an integer difficulty variable and assign the value of 2 to this variable.

  3. Next it will do similar things to step 2, this time for maxDifficulty variable.

  4. Then it will check whether the value of difficulty is less then or equal to the value of maxDifficulty. As long as this condition is true, it will keep;

    • Calling PlayGameAtDifficulty() function with the current value of difficulty variable

    • Clear the failbit

    • Discard the buffer

    • And increase the level of difficulty by 1 at the end of each iteration

  5. Once the value of the difficulty variable exceeds the maxDifficulty variable, it will break out from the loop and prompt the message.

  6. After all these instructions the main function will lastly return an exit value of 0 to end the program.

Privacy & Terms