So, what do YOU think main() does?

From what I understand, it is the portion of code that will call and play the previous code that we created above it. In essence it is the engine to the program.

1 Like

The main() function is probably used to actually execute everything we’ve coded. We’ll set up all of our variables, strings, inputs, outputs, etc. before we get to the main() function. Afterwards main() will be used to give the player the information relevant to the game in an easy to understand way and will let them interact with what we’ve programmed beforehand.

1 Like

I think that the main function runs all the “main” code. You write the side-code outside of the main function and call it within the main function when you need it to run.

1 Like

I believe it may be the core of your code where everything else lives.

1 Like

main is the function which you’ll be calling later to increase the difficulty after each correct guess made . the while loop will loop through each passed lever slowly increasing the difficulty until its equal with maxDifficulty so as soon as you reach level 11 (by guessing correctly) the system will then exit the loop and congratulate you on your code cracking skills

1 Like

I think it is the control block or cpu of the program, Code compilation starts with main function, it controls the sequence in which code is to be executed.

1 Like

The main function is function which is the primary function that’s looked by the computer first so as to do particular tasks, it’s like the control center for our program which gives direction to the computer what to do.

1 Like

My understanding of the main Function is, that you could see the main Function as a boss of all other Functions or Methods. Every Method or Function that we declare is just a “blueprint” that only runs when call it. And we call them in the main Function for Example. Methods or Functions are usefull Tools that helps us to save up to write the same code everytime. For example we need the exact Codeblock 10 times in our project so we do create a Method/Function and that save us Time and Codewriting.

1 Like

main func in C++ and in other language is the Entrypoint of our Programm when the Linker tried to link the c++ files together in one executable file

1 Like

Since we put everything inside main, I assume it is the function that runs everything we write inside it.
My guess from the code provided:

  • We start off with clearing all the terminals from … ?
  • Then, we play the game from difficulty level 2 until 10 (inside the “while” function)
  • Not sure what are the clear and ignore function are for
  • The ++difficulty means increase 1 level once we finish with the current level?
  • When level 10 is completed, we get out of the while function and get the message “WOW - You’re a master hacker!”
  • And that ends the game
    Is it possible to have two mains in a single file? Sorry, I’m a complete noob >_<
1 Like

Here is my take of what the main() does in this game:

First, the main method clears the terminal. Then, it sets the initial difficulty and the maximum difficulty level of the game. After resetting those two integer variables, the main game loop is ran. Within that loop, a while loop in fact, the game is played, and after the execution of the game, the failbit portion of the terminal is cleared, the buffered is discarded, and the difficulty is incremented, given that the player has beated the level. The game loop runs until the player has solved the puzzle at the maximum difficulty. Once the loop completes, a victory phrase is displayed, and an exit integer code is returned back to the main method.

1 Like

It is basically the core of the program. It calls in other groups of codes or whatever else we have declared in a specific order so that the program works how we want it to.

1 Like

Hey together :slight_smile:I wanna give my best to explain what the main function of this game is doing:
At first it sets a difficulty to 2. After this it defines a maximum difficulty of 10. The while loop checks if the difficulty is less than or equal to the maxDifficulty. If this is actually true a function is called which takes the difficulty as a parameter and probably will load a level based on the given difficulty. What the next two functions do is a little unclear to me but I will find it out while following this course. Lastly the difficulty gets incremented in the loop.
After the loop the user gets a congratulations message. And at the end the main function returns.

I hope I could explain it well.

Best regards
Daniel

2 Likes

From what I can imagine, the main function tells the computer how to begin processing the information.
Opening a window, closing other programs ect…

Once Program execution starts, the code will load then c++ will look at main for the start point, once it arrives here it will first clear then terminal for us then create two sections of memory to hold our value of difficulty and max difficulty then place the values of 2 and 10 in those for us.

Then it will construct a loop that will execute continuously until the difficulty becomes equal to or greater than our maximum difficulty. In this loop it will call our PlayGameAtDifficulty function and pass by reference our value for difficulty.

Once that function has finished doing whatever it does and returns main will call to both the clear then ignore functions from the cin class from the standard namespace, it will then increment the difficulty variable by one. Once difficulty becomes equal to or greater then our max difficulty the loop will break and finally using cout print to the terminal “WOW - You’re a master hacker!” Give it a new line and return 0 for the whole program and close down.

1 Like

I think in order to run the C++ class it everything should call inside main method.each c++ class should have one main method.It is the point that execution of each program starts.

1 Like

The main function first sets two variables.
There is the Level Difficulty which is set to 2 and the Max Difficulty set to the 10.

Next is a while statement. The while statement will continue to run as long as the Difficulty is less than the Max Difficulty. Inside the while statement is runs the PlayGameAtDifficulty which has a difficulty parameter. The std::cin.clear() will clear the code and then the std::cin.ignore() will prevent any errors. Then it increments another difficulty by one. This is the end of the while loop.
Now it prints out a message saying “Wow - You are a master hacker!” to the terminal!

After all of that it returns 0 and exits with no error.

Main () is a series of library-ed functions that calls certain things to run depending on what you are doing?

The code above looks like it wants to start difficulty at 2 and max it at 10 while calling current Difficulty to be outputted?

I think Main() is what you put everything into it?

I think it’s like the principal executions the code must do, and with the other command change it a little , maybe…

Privacy & Terms