So, what do YOU think main() does?

First, you clear the terminal (whatever that does, I’m not sure; I suppose it’s just to clean up your view of the terminal for the text that comes after).

Then you instantiate two variables, “difficulty” and “maxDifficulty”. These are the start and end points of the game. While you are still not on the level designated as the “max”, your function for the actual game logic is run. I have no idea what a failbit or buffer is, but after you do whatever you just did with those, then you add a value of - I assume - 1 to the difficulty variable.

After that is done, if you have made it to the tenth level, you are rewarded with a bit of text that calls you a master hacker, and the code finishes.

Not super sure what’s going on here. the std is referring to the hash… dictionary… thing that would be above this. “clear” maybe… clears the first level’s text from the command terminal?

Then it names two integer variables.

int difficulty = 2;
int maxDifficulty = 10;

I think the difficulty must start at 2 because the player would have to get through difficulty 1 before this code is run. So this function is run after the first successful “unlocking,” then so long as the difficulty is below ten, it adds a new a level of difficulty here:

++difficulty;

So ++ is making the integer go up. No idea what failbit and buffer are – maybe it clears failed player inputs from the previous difficulty level?

Then the “WOW - You’re a master blah blah” would occur when difficulty is not <=10, so at level 10 the player wins.

As i see the main function centered about difficulty
so there are 9 levels of difficulty start at 2 & end at 10
Now once you clear the level there will be an increase in the difficulty level and then u will have the statement : wow…* and goes to the second level .
that’s all i can think of right now.

At the risk of sounding like an absolute waste of space here, main() is setting up some sort of difficulty for the user, and also telling the program to write to the terminal if the user hacks the code.

I think the main function:

int main()
{
    /* 
      Calls all other functions within the source file and is the last function to return before compiling
    */
    return 0
}

Main Function does the following


1) Sets difficulty and maxDifficulty
2) WHILE LOOP
	a) Condition: difficulty <= maxDifficulty
		i) Call PlayGameAtDifficulty(difficulty)
		ii) Does some 'built in' C++ input thing
		iii) Does some 'built in' C++ input thing
		iv) Increases difficulty by 1
3) Once the Condition in the while loop is no longer true
		i) It prints a 'You're 1337' type message 
		ii) It ends the program.

First line seems to use a function/method from imported code to clear the terminal. Next, two integer variables are declared. The first probably sets the initial level of difficulty, the second is the maximum level of difficulty. The while loop executes as long as difficulty is less than or equal to max difficulty. In the while loop, first the game is played at the current difficulty level. When a level is completed, looks like some house keeping functions are called, and then difficulty is incremented by 1. This loop repeats until difficullty is greater than max difficulty, then the while loop ends; an encouraging message is displayed in the console, and the program ends.

The main() makes the statements operational. You play the game until you reach the max difficulty and are presented with a “Wow - You’re a master hacker!” notification. And then based on this, the game is reset or simply ends.

{
  std::system("clear");  // clears the terminal

  int difficulty = 2;     // initial difficulty
  int maxDifficulty = 10;    // max difficulty
  while (difficulty <= maxDifficulty)    // loop following block while difficulty <= 10
  {
    PlayGameAtDifficulty(difficulty);   // run game function at current difficulty
    std::cin.clear();  // clears the failbit    (not entirely sure what this does here)
    std::cin.ignore();  // discards the buffer   (again not sure)
    ++difficulty;   // increment difficulty
  }

  std::cout << "WOW - You're a master hacker!\n";    // if while loop finishes, print this
  return 0;  // exit with no error code        
}

The main() function is basically where your program starts and ends, utilizing all the helper functions to run the program.

Now, I’m probably completely off-base but for the sake of trying, I’ll take a stab in the dark and say given the main function’s place or hierarchy at the very tippedy, top of the code, it provides the ground rules. Or even better, maybe it’s the framework and provides consistency to our program, irregardless of whatever changes we make beyond that function. It’s something the program will hearken back to continuously.

The code provided above seems to be concerned with what exactly happens when players clear the challenge presented.

If someone could explain anything a bit more thoroughly to me that’d be much appreciated.
Edit: I was wrong, main is not at the tippedy top and boppedy bottom.

I don’t know much about C++ yet, but I think the main function is the start and the end of your program. It’s like the roots of a tree, without roots you have no tree and without the main function you have no (working) program. I think it’s where the code comes back to all the time.

But that's just what I think, because I have no clue in what it could do.:face_with_monocle:

1 Like

I believe the main function first sets a difficulty level, establishes a max difficulty, then checks to see if the difficulty has reached the max level. If the game has yet to reach max difficulty it runs, nad increases the difficulty for next time.

I have no idea what it does lol.

So as far as i know from python what main does is that it runs the code in itself if that file is running directly.So as we use #include to import other codes to our file the statements in the main function of that code does not run because we are not running that file directly we are just using pieces of it in our code.

main is the first function that the compiler runs. all other functions are called from main and return value to main which in turn returns a value on successful completion.
by dividing our tasks into other functions we can separate the code into them and control the flow of the code by using main. Sort of like an index to a book.

First, you clear the terminal to erase previous iterations of the game.

Then you set the difficulty. First, the difficulty the first level has, which is 2, and the max Difficulty, which is 10 and actually marks the last level.

Next, you iterate through 2 to 10 in the difficulty level. So, you’ll do the first level at difficulty 2, the second level at difficulty 3 and so on until you reach difficulty 10.

I’m not sure what those next line after the method PlayGameAtDifficulty(difficulty) do, but I’d say that it’s something to close resources maybe?

If you manage to go through 2 to 10, you’ll show a message ("Wow- you’re a master hacker!). I wonder what happens if you fail, I’m not sure if it’s implemented in this code, maybe in the PlayGameAtDifficulty method?

it defines the main function from which all the integers will be called from

In my opinion, the main function does just like its name, main. It’s the main part of a program, and only the statements and codes in the main are run or compiled. Anything that is not included or mentioned in the main function won’t be run when the program is activated. There is only one main function in a program, I think.

Well, i think the first sentence comunicates to the terminal, ordering it to clear out everything which was previously written in there. The second and third one give a certain value to the variables difficulty and maxDifficulty(both integers). the forth sentence checks a certain condition(between brackets), if said condition is true, it will go through the block code that follows(sentences 5-8). Once all the block code was executed, it will check to see if the condition is still true, given it is true, it will continue through this cycle until the condition is false(a.k.a. difficulty is greater than maxDifficulty). Once the condition is finally false, it will print to the cmd the text in the ninth sentence. Finally, it will return the integer value 0 to the command line to notify it that everything went as it was suposed to.

The main function in this code is the first thing that will be executed by default.
Note: In the code whenever the author is typing in std:: we are accessing the iostream standard library functions that are predefined in the header file, another alternative for this would be to write the statement using namespace std; right after the inclusion if the header files, but clearly the author has a different reason not to do so (Which I’m not aware of yet).

Looking at the code further we can see that the author is clearing out the terminal and then proceeding to set the default starting values of the max difficulty possible and the starting level of the difficulty.

We execute a while loop with the condition that the current difficulty level never exceeds the maximum level of difficulty possible.
When we enter the loop beforehand the control is transferred to the function PlayGameAtDifficulty which basically sets problem presented to the user according to the current difficulty level for the player, it is also passing the current difficulty level as a parameter.

I personally have never used the clear and ignore functions of the standard input library but from the commented code I gather that the author is clearing the input stream of any data, after this the difficulty level is incremented.

The user will have to keep on playing unless he crosses all the difficulty levels and on winning he shall be presented with an appropriate message. After that 0 is returned to the system and the code has finished the execution of the given code successfully!

Privacy & Terms