So, what do YOU think main() does?

The main function is gonna be the “skeleton” of our game, where all the settings of the game will be writted.

Jack here. I am totally new to this and know zero about coding. I work full time at an art institution doing digital media/videography. I am so excited to be doing this course! Making (or at least understanding) games has always been a desire of mine. I am not sure what main() does, but if I were to guess: This somehow sets the parameters of what our string of code is going to be? Like whatever is within that main() is what the system will read as one chain of events.

Disclaimer: I studied c++ on my own several years ago. Haven’t done anything with it for a while now though.

“int main() {}” - denotes that the function should return an integer type. the word ‘main’ is reserved by cpp and is always the point where the program execution will begin. The () gives a place for you to insert any arguments that you want main to have when it starts. The {} denotes that all the commands within should be executed as a complete unit whenever the identifier preceding is called, main() in this case.

int difficulty and int maxDifficulty - declares a variable with the names difficulty and maxDifficulty, and the type ‘int’. Because you didn’t specify ‘long’, ‘short’, ‘unsigned’, etc, it will have a size of 4 bytes ranging from either 2^-32 to 2^32-1, or 2^-64 to 2^64-1, depending on your machines architecture. the variables are immediately initialized with the values 2 and 10 respectively.

while() - tells the program to repeat the set of instructions int the brackets for as long as the condition within () remains true.

PlayGameAtDifficulty(difficulty) - calls the function with that same name, passing the argument ‘difficulty’ to be used by that function.

std::cin.clear() I know that this calls the clear() function from the “console in” class of the standard library, but I’m not entirely sure what that would do. Probably clear the buffer.

std::cin.ignore() same idea as above but calls the ‘ignore()’ function instead. No idea what that would do.

++difficulty is an iterator. It adds exactly 1 to the variable named ‘difficulty’ Because the ++ is before the variable, the program knows to raise the value by one before the variable is called. However, because of its location in the function, I don’t think this would have an effect.

std::cout calls the ‘console out’ class of the standard library, telling passing it the string “Wow … hacker!\n” cout then passes the string to whatever is set as the standard console, (usually a monitor), along with a newline character as denoted by \n

return 0 tells whatever parent process called the function that everything was successful and the process is ready to stop.

Hello! As an overall, I think it clears old info on the screen at the start and sets the difficulty starting point and how it progress to be harder. It also limits the difficulty so it can’t go past a certain number. It also gives the player a message if they succeed. That’s my best guess at this point lol.

This main function is the entry point into this program and is controlling the flow of this program from beginning to end.

The main code sets the numbers that will determine the difficulty of the game, and bumps that number higher as you progress

Hello, this is my first time with Cpp i have done some C# and python before so i think that the main() function is where our other functions gets called

Main function is a type of function where it has all the inbuilt libraries that you need to access them later in your code.

The gist is as follows:

First off, I think the main() function tells the compiler to start the execution here. Also, the include statements and the functions we define earlier are what the compiler keeps in memory so as to call them later on when need be.
In this code ,we are defining the limits of difficulty levels while we clear the input buffer to give an illusion of a new terminal screen, every turn we call the PlayGameAtDifficulty(iterative difficulty) .

I have no clue. But if I had to guess, I’d say it looks like the main() code looks like it determines the difficulty of the game being played.

Main function appears to define a couple integers (difficulty and maxDifficulty), then within the while loop it calls the PlayGameAtDifficulty function, clears something, ignores something (input stream?) and increments the difficulty. Finally it outputs a text string and and exits.

I think the main() function is used as the function that gathers all the code one uses through out the project whether the code is done exclusively inside the main function itself or is files outside the function. Its the function VS or any other coding software would use to run the code one writes

Soo, per my understanding the main() function is the entry point in the program, i.e. the program’s run will start from it. If there are other functions they need to be declared before it, so that the program can call them.

I think the Main() function is the center of any C++ program. This is where you will run your code (variables, functions etc.). The compiler will translate everything inside it to binary and tell the computer to start there.

main() would be the entry point of our program once it is compiled down to machine language and run. Once the OS does what it has to do to launch the program the first instruction in main() will be executed and the logic of the program will begin.

Okay, so I think Main sets up a blank terminal for the user. It establishes the difficulty and a maximum difficulty. As long as the base difficulty is less than or equal to the maxDifficulty then it will continue to play.

Then PlayGameAtDifficulty(difficulty) is clearing the failed attempt at the game with std:cin.clear();

I’m not entirely sure about this next part what discarding the buffer exactly means.

The last part turns the results out to the player if the attempt is successful.

Hmm, I think it’s the container of the code structure.

I think the main() would be a conatiner for all of the code inside it that is indented.

First it clears the console, then it sets the two variables “difficulty” and “maxDifficulty” at 2 and 10 respectively. Then it runs a while loop that checks if difficulty is less than or equal to maxDifficulty and as long as that is true it will call the function “PlayGameAtDifficulty(difficulty)” (while the difficulty in parenthesis is the return value that is set when you either win or lose). Then it clears again, and adds whatever number to difficulty that was returned and then does the loop again until the conditions are no longer met.

The main() function has a starting difficulty and a max difficulty that the game can go to. Once the code runs it starts the game at int difficulty and adds difficulty if the answer is correct. Then a message runs if you get the question correct.

Privacy & Terms