So, what do YOU think main() does?

The compiler (g++) calls the main() function. Before calling main the C pre processor directives get copied in a separate file. If your code includes macros then they are copied in a file. Then the header files which include all the function calls. Compiler calls main() after that. After that the standard linkage of library is stdlib in C, but if we use functions we have to include right header file and proper linking to library.

from what i know main() is like the engine part of what will run , physics functions the we indent and variables we transform or any math we do into with it will also declare any and all variables we ''declare ‘’ into it . it acts as what the base of whatever we do will be

It is the main entry point into the application and runs the main loop (until maxDifficulty is reached).

The main() function is the initial access point for any program. Any caller to the application doesn’t know the structure of the program but, by default, it knows that there will be a main() function that can be call and start the execution of it.

The main() function is our entry point into the program. Between its curly brackets is the code, which is going to be executed, which in this case is:

  • The terminal gets cleared (as the comment suggests)
  • Then we have an integer difficulty which has the value 2 assigned
  • And an integer maxDifficulty with a value of 10
  • The next thing is a while-loop, which is executed as long as our integer difficulty has a lower value than the integer maxDifficulty
    • Inside the while-loop it passes the value of difficulty on to a function called PlayGameAtDifficulty, which we can't see in the program, so it has to be somewhere else
    • Then there is cin.clear, which should presumably clear the input method from any error flags, which would stop our input from working
    • cin.ignore should have a value of what it should ignore of the input, but if I remeber correctly leaving it empty should remove any resuming input
    • The expression ++difficulty increments our difficulty by 1
  • When our while-loop finishes, we leave it and move on the the code below it's second curly bracket
  • cout simply outputs the string value, which follows in the same line
  • return 0 simply returns the value 0 to our main-function, which stops the program

That should cover everything more or less exactly. I just happen to know some stuff from other programming languages.

execute all the program

The main() function in the following code clears the terminal,assigns values to variables, calls the PlayGameAtDifficulty() function and returns a value of 0. Generally the main() function is the heart of a code block which the compiler looks at before anything else in the code i.e. the main() function is executed before any other function.

I have absolutely zero experience with code so here it goes.

It looks to me like first part of the code std::system("clear"); // clears terminal is basically telling to start from a clean slate.

The second part looks like giving variables integer values. Specifically an initial or floor value for difficulty int difficulty = 2; and a max difficulty int maxDifficulty = 10; while also stating that the initial difficulty always has to be less than or equal to the max difficulty (difficulty <= maxDifficulty)?

I’m unsure about the other part but it looks as though it instructs to play the game at (difficulty) which was defined in the earlier section as 2.

I assume at this point the text for the question and answer would run (although I would have thought that text would have had to be told to run).

Maybe the // clears the failbit and // discards the buffer refers to getting the answer right or wrong and either essentially “opening the lock” and allowing you to progress (winning) or removing the difficulty buffer that would be added as you progressed and returning you to a difficulty = 2 to try and play again(losing).

The last bit looks like maybe a message displayed when you get the answer correct or “win” at the final difficulty.

In addition could some one comment and let know if the quoted texts in my reply was what what he meant by:

@ben?

thanks!

I am 100% new and have no clue. Learning how to think about it from the comments here, though!

1 Like

In my words I think the main function is the function in which C++ runs first because it is the main function. It is the start of the script and runs what is in the brackets and calls any methods that are in the function.

I have some basic experience with c++ so here it goes.
yeah i know that clear cmd that used to clear the terminal and there is another function clrscr() which used to clear screen if you are not using the ide that does not contain terminal.
I also know about the functions.but i dont know about those two functions that are used in later in the code and its all i have to say.

The beginning of main() removes existing text from the screen. The next 3 lines define a range of difficulties the player will progress through as they keep solving puzzles. The block of code within the while loop’s curly brackets start each new difficulty by calling a function that randomly chooses a number based on the current difficulty, then displays the puzzle to the user, waits for an answer, then tells you if you passed or failed. Afterwards, the input stream is cleared. I’m not sure if that’s necessary, but it’s been a while since I last wrote a console application that took multiple inputs in succession. Also, though I know it doesn’t matter in this case, at least stylistically, I prefer putting the increment operator at the end of a variable, rather than the beginning. Makes it more clear that it’s shorthand for difficulty = difficulty + 1, or difficulty += 1.

What’s a bit odd is that the code appears structured to progress in difficulty even if you “fail miserably and die horribly.” You can fail every puzzle and get called a master hacker. I suppose it would be sarcasm, in that case. It returns 0 because to return otherwise is to indicate a failure state (e.g. “Did this program run correctly?”).

I should probably skip section 2. Judging from the names of the subsections, I already know everything being taught. Assuming there’s homework for making the game better reflect a failure state (i.e. stopping when you die), I might just modify the PlayGameAtDifficulty() function to return false if you fail, setting that result to a bool within main and add an additional check to the while loop of && alive. If that bool is false, instead of being complimented, you’d get a game over message.

Btw, section 2, quiz 1, question 4 (not sure if these questions are randomized), it says “it’s declaration;” it should be “its.” Quiz 2, I learned camelCase for variable naming, and generally m_camelCase in the industry. :stuck_out_tongue: The real answer is, “whatever the codebase already uses.”

  1. Clears the terminal of any previous data
  2. Creates two int variables, one for starting difficulty and the other for highest difficulty
  3. start a while loop to performs steps while the difficulty variable is less than or equal to the maxdifficulty variable
  4. During while loop, execute the game function and pass in the difficulty variable
  5. Not sure on the failbit
  6. Not sure on the buffer
  7. Increment the difficulty variable by 1 and return to the beginning of the loop
  8. End the while when the difficulty variable is greater than the maxDifficulty variable
  9. Write string content to the console
  10. Return int 0 to indicate no errors were encountered

I think main() is the…umm general frame of the game, so sort of a summary of the entire thing. For example it contains the lines
int difficulty = 2;
int maxDifficulty = 10;
What each difficulty setting does at each level I assume will be coded elsewhere to ‘difficulty’
or some other placeholder and a given value up to 10. The ‘main()’ code then just contains all the variables of the game in a single function.

Main() will check the progress of the user, if the user haven’t reached the maxDifficulty then the difficulty variable will be used in “PlayGameAtDifficulty(difficulty)”. Then PlayGameAtDifficulty() will clear the user input at current difficulty and ignores any other input from the user and increment. If the user reach difficulty 10 then a message will be printed " WOW - You’re a master hacker!" then the program will indent to new line and exit.

I’ve used a bit of Java before in a freshman level CS course I took, so most of this looks pretty familiar.

That being said, I’m not clear on what std means, but I’m certain it has something to do with sexually transmitted diseases. I’m joking, that’s probably one of those “wow, I’ve never heard that one before” things, sorry. If I had to guess, I’d probably say it had something to do with system, or standard, or… desktop? I don’t know.

I’m not certain what the double colon :: is doing either. I’ve seen it before somewhere, but I don’t know. Once again guessing, I’d go with std performing the equivalent function as a package in Java. Instead of using a package.class.method format like in Java, you’re using package::class.method to make it clearer what’s happening on the screen. Idk.

Variable initialization, loops, methods, all the same as Java. Main is creating some variables, running a loop while one variable is less than or equal to the other std::cin is probably talking about console in, but maybe not, and once difficulty is greater than maxDifficulty it ends the loop and tells the user (don’t know what the << are about. In Java you’d hit them with System.out.println("WOW - You're a master hacker!");) that they’re a master hacker. Returning 0 instead of void like Java because of reasons unknown to me.

I’d just like to apologize for the formatting, this is my first post and I’m trying out the three ` thing that was mentioned.

Every c++ program needs to have main function.That function includes commands and other functions which need to be executed.Hope that roughly right.

First and foremost ,there must be a reason why its called the “main” function. As far as i know any C++ code is a block of code that performs a set of or even a single function. The “main” function must be a super function,ie it controls the flow of order of other functions (if any) in the whole of the code and its execution precedes all other functions.
The Master Caller and controller of other functions.

I’ve taken a python 3 course before and also know some javascript, so I have some familiarity with coding languages in general. I believe that it is the main function which the game plays off of. It looks like it’s clearing the terminal for a fresh start, setting the difficulty, using a while statement to mange the difficulty level while playing, once you get to the max difficulty it will break it’s self out of the while loop and return a winning message or string. Then finally it ends the main() function.

main() funcion is often called as “dummy function” because it does nothing , its sole value is to tell computer where the instructions are i.e. inside it.

Privacy & Terms