So, what do YOU think main() does?

The main() function is the function through which all statements and other functions in the program are executed. There can be and must be only one main() function. It returns a data which is an integer.

Well, i think it serves to separate a code in blocks and then you can switch between different parts of block of codes in the program.

The main function is the start of a program.

I think that the main function is the one that describes how is the other code going to be.

Basically, u have two variables declared. The first variableis equal to the normal difficulty at which the game starts. The second variable is equal to the maximum levels the game can have. While the maximum value hasn’t been reached, the game will keep on playing. If you make it past the maximum difficulty you win.

main is the default entry point for your application. It is what gets called first and the start of everything.

I think main() is the driver of the entire program. main()'s while loop will continue to play the game starting from level 2 up until level 10. main() simply uses the above code and creates multiple levels of difficulty with the loop.

Main () is a function that initiates the start of the users custom code, but while that is it’s primary function it also is a sort of ‘safety’ because the system closes the environment for that code (by simulating a new environment) and if anything is wrong with the code the error will then return to state 0.

I could be totally wrong, this is all a guess :slight_smile:

I think main() is the main control of the file

  • The initial difficulty level is set to 2, and maximum set to 10
  • Whilst the difficulty is less than or equal to 10, play the game at the current difficulty
  • Once the round has completed, clear/ignore the unwanted or erroneous characters input by the user from the input buffer
  • Increase the difficulty by 1
  • Start all over again if difficulty is still less than or equal to the maximum
  • Or, print the “WOW” message and close the program

main() function is a collection of codes/statements that the computer reads and acts accordingly. The function could pull data or codes that other people wrote from a database that either we gathered and built, or from someone else’s.

It looks like it sets an initial difficulty level. Then it sets a maximum level for difficulty. Then it says while the difficulty level is not greater than the maximum, call our other function. Then a couple of things happen which I’m not sure what they are. Then the difficulty level incrementally increases. Then it displays a message.

I have no idea but i think main() runs a certain function ahead of all others and then looks at the variables. I’m sure i’m wrong.

The main() function is first code executed when the program starts and it returns something after completing, in this case it return 0.

The code above will set difficulty level to 2 and max difficulty level to 10. While difficulty level is lower or equal to the max difficulty it will play the game on current difficulty and increase the difficulty by one each round. When difficulty reaches the max level it will print out text and return 0, when the game ends.

The main()function is the most important part of code because it is where the main codes for the program are ran.

std::system("clear");

This first line clears the terminal so that the only information showed is the game itself.

int difficulty = 2;

This sets a variable that will be used later to determine how difficult the game will be.

int maxDifficulty = 10;

This sets a variable that will be used later to determine if you have won the game.

while (difficulty <= maxDifficulty)

This is a loop that will be run repeatedly until the condition is false. the condition is "is the difficulty less than or equal to the max difficulty. right now “difficulty” is 2 and “maxDifficulty” is 10, so the condition is true.

PlayGameAtDifficulty(difficulty);

The first thing in the loop is running a function called “PlayGameAtDifficulty” and it is taking in the variable “difficulty” as the argument. We don’t actually know what this does yet, just that it takes in a variable as an argument.

std::cin.clear();  // clears the failbit
std::cin.ignore();  // discards the buffer

I don’t know what these two lines do.

    ++difficulty;
 }

This adds one to the “difficulty” variable before ending the loop. because the “difficulty” variable is increased it will eventually exceed the “maxDifficulty” variable and the loop will stop running.

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

This outputs the words “WOW - You’re a master hacker!” before going down one lie in the console and then exiting the program with no errors from the return 0; portion of the code.

The main function sets the minimum difficulty and the maximum difficulty and increases the difficulty each round, but I don’t really know what the .clear and .ignore functions (is that even the right word?) does. The final part returns a statement “WOW…” after completion of one level or maybe only after all the levels are completed.

The main() function instantiates the code for the program and is the first function called when initiating the runtime system.

I think main() is the structure behind the code.

The main function is the beginning of the program and when the program is executed. the main() function is used in every C++ program.

Privacy & Terms