So, what do YOU think main() does?

It seems like its starting off by clearing all inputs before, then it sets a difficulty range. It looks like it is setting the maximum difficulty, then telling the code to run the game as long as the selected difficulty is less than ten. I have no idea what all of the std cin stuff is doing. Perhaps testing inputs? That last section appears to be awarding a successful completion of a level with the statement in quotes.

main{} is the most important function, is the function the compiler will use when running the game. It has the main options and then it takes you to the other functions according to wich option the player choose.-

Main function purpose is to tell compiler which code you want to execute and in what order. Simple any other function that I/you have created will not be executed if proper reference is not made within main function. Also you can pass arguments through main functions so than you can create a terminal based app and do something like this to get result

eg. calculator 2 + 2

each argument can be read and further process by the function. Default arguments can be empty or void, it simple means we do not pass anything to main function. We expect main to return code 0 at the end otherwise it returns error. Where other functions can return values that program can actually further process.

What I think the main function in this example does is sets the difficulty at a defined rate, until it reaches the max difficulty where it stops incrementing by the value (default at 1), while inputting the user input against the created number combination, by using the two subfunctions labeled clear and ignore respectively for true and false if using a boolean statement.

Apologies for the details, kinda used to C# from unity development, but seeing C++ reminded me that its far more useful for higher end projects, while being able to better control memory flow and cpu usage.

The main() function is what the program uses to execute itself. You write out all your functions above it then neatly put them inside of main() to be used in your program.

I think the main() acts as some kind of overall reference point from other areas of the code that includes the overall concepts/fundamentals for it.

I am completely new to C++ so I don’t understand what the main function does. I am going to give it my best shot then look back to see how much my understanding of the subject has grown.
Line 1: Main() function is how we know that this is the main string of code for the game.
Line 2:Int difficulty = 2; I think that is the current difficulty of the level.
Line 3:Int maxDifficulty = 10; I am guessing that this is the max difficulty for the game.
Line 4: PlayGameAtDifficulty (difficulty); is the line of code used to set current game difficulty per level based on the data from mine 1 and two.
Line 5: Std::cin.clear(); we learned in the video that green text is a developer comment. So this line is used to clear the fail screen.
line 6: Std::cin.ignore;// I think this is used to increase the difficulty for the next level
the final line is an if-then statement. If the player inputs the correct value then display the message “WOW - You’re a master hacker”

1 Like

So first off it clears errors in the terminal through system.

  • Then you set the difficulty and therefor level to 2, and the max level to 10.
  • Then, as long as the difficulty does not exceed the max difficulty, you play the game at the currently set difficulty.
  • Then you clear anything that’s currently hanging out in the input field after each level.
  • Then you increase the difficulty if the in spite of the player passing or failing until the current difficulty matches the max difficulty.
  • Finally you say “WOW - You’re a master hacker!” and create a new line before exiting main with a return of 0.

it first clears the screen and then sets the starting difficulty to 2 and max to 10.

Then while the current difficult is less than or equal to the max difficult, it will play the game at that difficulty.

After you pass that level it clears the screen and memory (?) and then increments the difficulty by 1.

Once you pass all the levels it plays the final message.

The function “Main()” is the function that is automatically run when the program is executed.

What the main function does is run the previous function, PlayGameAtDifficulty, while changing its values depending on the difficulty which is set up to increase with every correct answer.

I think that the main() code seems to control the flow of levels in this Triple X game. The “int” and “while” keywords there pretty much explain that the levels range from 1-10 and after a level is completed it’ll gradually increase to the next level until it’s reached the max level of 10. Aside from that, I don’t know what the middle portion there really means, maybe that the game will clear a level and increase in difficulty after each level has been completed? The last bit seems to be the code that ends the game entirely after the player has successfully completed the final level.

base on the name its what run the main function in game and the out-put system

Well my vague understanding of main function is it’s a function where all other functions are called and executed.

Hello every one!
I Would say main() is the part of the code that is genuinely executed line by line during the runtime.
It can call external code parts (functions) and can use previously declared variables.
Let’s continue!

I believe the main() function is what is called at runtime… and I am using that term like I know what it means in the context of C++ but honestly I don’t.

I am just saying that whatever you put into main() will be called when you execute the program, whereas functions you write yourself will need to be put into main() in order for them to be used.

EDIT: And after playing the video I see that he’s talking about the actual code inside of the function, lol. I didn’t even look at the code :confused: oh well.

FYI, I am not a noob at programming but I’m new to C++.

The main() function first clears up the terminal window, for a fresh screen with just game data, sets default difficulty at 2 (Starting at Level 2), and ramps up difficulty until a maximum of Level 10, at which the game ends and you’re a ‘master hacker’! :slight_smile: For each level of difficulty, the function PlayGameAtDifficulty is called and once the level is played, it goes to the next level anyway…? Game does not end if user ends wrong combination…? In any case, it clears input buffer too before calling the function again.

Is the function that makes the game run, it starts by setting the difficulty to 2, and making the game run while the difficulty is lower than 10. which means that there will be 9 levels.

Is main() basically the code you use to initiate or end all the other processes within the program.

main() it’s the first function that system runs when a c++ application it’s executed.

In this code we see that the application runs as a cmd/terminal application. This is because the ‘std’ I/O library functionality it’s called multiple times in this function. The first line calls it and it clears the terminal, in other words clears all the cmd lines written, in order to let the application starts at the terminal’s first line in case you’d been using it before starts this application.

The main function then declares two ‘int’ variables that stores the value of the difficulty of this game. Difficulty starts at 2 and the max difficulty will be 10.

While difficulty it’s lower than 10 a code block is executed. At the first time this function is executed difficulty it’s 2 so the code inside it’s executed. This code executes a function named PlayGameAtDifficulty that needs the difficulty parameter. This function code isn’t shown at us so we are going to suppose that executes the core of the game at a given difficulty.

When PlayGameAtDifficulty function returns the control to main function there are two std library function executed that cleans the buffer of the user’s cmd/terminal entry (cin). This prevents that the previous usage of ‘cin’ stores stuff that we don’t want at the application buffer. In main we don’t see an usage of this user input so we can suppose that PlayGameAtDifficulty function is using it. This can be just a preventive technique. We can discuss if these ‘cin’ management functions should be executed here or at the end of PlayGameAtDifficulty because IMO a function mustn’t take responsibility of other functionality that it’s called inside. In case that this is a preventive technique then it’s all right.

No matter where the user input management function are executed difficulty increases and while block of code executes again until the difficulty arrives to max and the user wins the game.

At the end a message to the user is sent with the cout functionality explaining that he has win and the game closes with the return command that indicates that everything goes well.

Privacy & Terms