So, what do YOU think main() does?

My guess for what the Main() function does is first clearing the terminal then setting the difficulty level, when the difficulty level is less than the max difficulty level it will clear the failbit, and discard the buffer. Then you get the notification of “WOW - You’re a master hacker!”

The main method is essentially the starting place for the program to begin running. With out it the computer would not have a real idea of where to start working.

First, the main function clears the terminal and declare a variable for the first difficulty and a variable to set a max difficulty. Then, what is going to do is execute the code in the PlayGameAtDifficulty with the value of the variable “difficulty” as a parameter, clean the input buffer and increase the value of the variable “difficulty” in a loop that ends at the maxDifficulty value. Finally, when exit the loop the terminal shows the last message and makes “main” return 0.

OK, let me give this a try :slight_smile:

int main()
{
    int difficulty = 2;
    int maxDifficulty = 10;
    while (difficulty <= maxDifficulty)

When the main function is called two variables are initiated, they set the difficulty or, what I suppose is level in our game. Then a loop is run until the difficulty variable is bigger than maxDifficulty.

{
    PlayGameAtDifficulty(difficulty);
    std::cin.clear(); // clears the failbit
    std::cin.ignore(); // discards the buffer
    ++difficulty;
}

Inside the loop, PlayGameAtDifficulty function is called with difficulty as an output. This function is our game. I don’t know how the next two lines work, but from the comments, I suppose they clear something in the memory. The next line adds one to our difficulty variable and the loop repeats.

    std::cout << "WOW - You're a master hacker!\n";
    return 0; // exit with no error code
}

After the loop ends the next line outputs “WOW - You’re a master hacker!” to our screen and if there are no errors the program ends.

The main() function is a function which does not accept any arguments in C++ and contains all functions, variables, constants and loops that are to be implemented in the program. The execution is done in the main function while the functions inside the main may be defined outside the main().
I hope I’m a little accurate at least.

If I recall correctly. It is a compiler defined function that acts as the entry point for execution of the code.

Basically it says to the compiler: “start here when the code is run”.

What I think Main() does,

I think that the main function contains just that the main function of the programme that needs to be executed for the game/programme/whatever to work. That’s all I have as I am brand new to programming in general. :slight_smile: :+1:

Hi,

So I do know a bit of C++ took 2 classes on OOP and Algorithms in my College but I can’t properly write the code just yet.

So I believe first line “std::system(“clear”);” clears out the console just like he commented.
You are also declaring the min and the max Difficulty in 2nd and the 3rd line of code.

The while loop basically checks as long as the difficulty is under max Difficulty which is 10 only then it runs the loop. PlayGameAtDifficulty() is a function that takes in the difficulty as a parameter and it would be the main part of this code and after you type in the answer it increases the difficulty by +1;

std::cout basically prints the string “WOW - You’re…”
return 0; exits the program like its commented.

The main function is the code which will be run first. It is written last in the file as previous functoins have to be read first. Firstly, main defines the starting and max difficulty, then says whilst the difficulty is less than or equal the previously defined max difficulty, keep running the function PlayGameAtDifficulty. Every time PlayGameAtDifficulty is completed, difficulty is increased by one.

The syntax of running PlayGameAtDifficulty(difficulty) has difficulty in the bracket so that integer can be used cross-function i think.

Once difficulty > maxDifficulty, it outputs a nice message then returns to 0.

I believe Main is the main set of instructions the program will use to run. It will detail what to display in the terminal.

I think the main thing is a function that symbol of which codes work inside. our reason for use this command just for use that few lines inside of function in one word :slight_smile:
tell me if im wrong pls, thanks

er…I have never learned a code language before this so…my guess, after reading through a couple comments and such, that the main is the equivalent of the “start menu” and its the function that calls on other things and without it doesn’t run the program? I feel like such a noob… >_<

i think the main function is the start of a program and also the end of it. other functions follow thereafter.

It starts the game, controls the game loop and ends the game.

The main function is where everything is “called”, or where everything is used. In other languages, you might have a create an instance of a class in order to run any functions but in C++, you can run code right away using the main function.

I’m still a little bit new to coding but I believe that “main()” is the main function to power the entire code that we write. Without it we won’t be able to run our code.

Its been a while since I’ve worked in C++ however here is my best recollection:
the main function is the starting point of the program during run time and all programs will start from this function no matter where it is positioned in the code.

Imagine that you want to water the plants, but you need some sort of watering can, before you can do it. The same is with main () - it’s a main command to code in C++. That means if we define sort of arguments we are able to expect particular results, when specified conditions have met.

The main function first clears the terminal - I know that from the comment :wink:. Next, it declares two integers, difficulty and maxDifficulty. A while loop follows. While difficulty is <= maxDifficulty, the function PlayGameAtDifficulty is called. According to the comments in the 2nd and 3rd line of the while loop, these lines clear the failbit and discard the buffer (reset for the next execution of the loop?). Next, difficulty is incremented. The loop will terminate when difficulty = 10. When the player guesses the combination, the program outputs “WOW …” text.

I the the main() function is where all of the non-main or secondary(?) code is housed.

Privacy & Terms