So, what do YOU think main() does?

I do not know what main() does pls help.I think it is saying the order of which the code will function?

I’m not sure on the specifics,
but I think the main word for function is a key word that the compiler recognizes and sets this as a starting point of the program that will be compiled.

It sets two integer values and then checks to see whether the value of “difficulty”(2) is still below “maxDifficulty” (10)
while that statement is true, it will run through a loop of code and recheck after it completes the loop until that statement is no longer true.
The loop:
It calls function “PlayGameAtDifficulty” and inputs the current value of “difficulty” variable as an input parameter for the function.
after running the PlayGameAtDifficulty function it clears two buffers
and then it increments the difficulty variable (adds 1 to the variable)

when that statement is no longer true
“difficulty <=maxDifficulty”
you have obviously beaten the max difficulty level and it prints out “WOW - You’re a master hacker!”

That’s what I think happens

I think that main() is like the power button of PC, without that you can’t run the code.

int difficulty = 2;

This code is the starting difficulty of the game

int maxDifficulty = 10;

This one as the code suggests it’s the max difficulty the game can reach

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

To be honest i’m not sure here, it probably changes if you pass or not the level.

I think the main function is the function which is the kind of “MAIN” function of a program, the program when executed, starts from the main function.

The main() function in general will be what the computer runs when the executable file is clicked. Anything outside the main function that isn’t used in some way inside the main function will be ignored. More specifically this main function declares and initiates some variables. Then goes into a while loop that if the condition is met will run the User made function PlayGameAtDifficulty(). Then after the while loop is done it will print out the string at the end.

So, with my basic programming knowledge (literally mainly BASIC, PASCAL, and some Actionscript 2.0) I think the main code does the following:

Sets two variables with integer types and sets a while loop to run as long as difficulty isless than or equal to maxDifficulty

The loop runs the function PlayGameDifficulty with the current value of the difficulty variable setting the level of difficulty

based on the response to the game, clear and/or ignore run then the difficulty variable is increased by one if the correct answer is given and the string is displayed on the screen.

I don’t see a main( ) function in the original code. Why is it not formatting for me? I keep thinking the OP is a trick question, but apparently not… :slight_smile:

I believe the main function creates two int variables, one named “difficulty” and the other named, “maxDifficulty” which gets computed in the “while” function to help set the difficulty of the game for the player depending if he or she has completed a level or failed a level. Having either cleared or failed a level, a certain message is set to display to the user and then the difficulty is therefore increased or in other words, the player moves on to the next level.

Hi Ben,

From the code given, the code does the following:

  • System clears the terminal screen

  • Sets the difficulty to 2 and Max Difficulty to 10.

  • A loop occurs after with the parameters if the difficulty is less than or equal to the max difficulty do the following:
    - Do a function of PlayGameAtDifficulty with a parameter of difficulty
    - Clear the Terminal
    - I am going to guess discard the memory buffer, but unsure.
    - Increment difficulty after looping back into while i believe
    - Keeps looping until difficulty is greater than max difficulty

  • Once done with loop, output that the user is a master hacker if they make it out of loop.

I think, as its name says, that is the “mother” function of the script, and its the first checked when running the code.

It defines che starting and maximum difficulty level for the game.

Then it keeps launching the function wich implements the game passing to it the difficulty level and incrementing the difficulty level at each iteration, until the maximum difficulty is reached.

The main function looks as if it starts out with clearing the terminal. It then assigns 2 as the base difficulty level, and 10 as the max difficulty level.

Afterwards, it carries out the tasks of starting the game with PlayGameAtDifficulty (likely another function located somewhere else. By pasting it here, I assume you are able to use the code written where the function is defined). It also changes the difficulty level by one after every successful attempt.

Lastly, if you are successful in clearing all difficulties between 1-10, it successfully prints out “WOW - You’re a master hacker!” on the line below.

The main, to my knowledge, is the bulk of the main code that tells other functions what to do. Almost like the rope holding together a bunch of logs, without the main it would fall apart. That is my best understanding but I have just started! Wish me luck!

The main function seems to be ran whenever the player submits their answer? It checks if the answer is correct, if it is, it then checks what the difficulty level is and raises it up with the while statement? It sets the max difficulty and checks what difficulty the player is on. If the player get it wrong, the main function throws an error and returns something other than 0? This is all speculation, I’ve written a lot of Javascript, but the syntax for c++ is a little different, so some of it I don’t know what it’s doing.

What I think that main() function does is

  1. Initiate the difficulty at 2, the max diff at 10.
  2. As long as (the while loop) difficulty is less than max diff. it will execute what is in the while loop
    call the PlayGameAtDifficulty() function, call clear(), ignore() and the increase the difficulty
  3. output the message “WOW - You’re a master hacker!\n”

Main Function is to call the statements , it includes what is happening in the Whole code - variables , statements etc.strong text

Sets current difficulty level and max difficulty level, then run the game until max difficulty level is reached.

It appears to me that a main() function is kindof a function that you use to execute everything else you’ve written, for example; the PlayGameAtDifficulty() function.

Really not sure if I am right though :slight_smile:

Overall, this will define a difficulty level that increases every time that the player successfully guesses the code. It is set to a simple loop that ends in the final difficulty level specified being completed and printing out a statement letting you know you won the game.

Privacy & Terms