So, what do YOU think main() does?

The main() function is your executable program that gets compiled into machine code which can run on any machine regardless of the operating system. This is done using a linker which resolves dependencies and zip together object files with those dependencies.

The main function has to return an integer, so the operating system can determine if the program terminated successfully (i.e exits with code 0).

  1. In this case, this main function sets a variable difficulty (of type of integer) which has initial value of 2.

int difficulty = 2;

  1. Then, a variable maxDifficulty (also of type integer) is set with an initial value of 10.

int maxDifficulty = 10;

  1. Next, using those integer variables a while loop is used to play the game with increasing difficulty. While loop are also known as pre-test loops, where the exit condition is evaluated before iteration takes place. If the condition evaluates to true, the loop continues. The loop exits when the exit condition evaluates to false.
while (difficulty <= maxDifficulty)
{ 
      PlayGameAtDifficulty(difficulty);
      std::cin.clear(); // clears the failbit
      std::cin.ignore(); // discards the buffer
      ++difficulty;
}

The exit condition in this loop is whether the difficulty is less than or equal to the maxDifficulty.

If true, the following statements are run

a. > PlayGameWithDifficulty(difficulty);

This is a function that accepts an integer parameter that handles the game logic. No value is returned since the function is declared as void.

b. > std::cin.clear();

This std::cin object uses the clear method to clear the fail bit.

c. > std::cin.ignore();

This std::cin object uses the ignore method to discard the buffer.

d. > ++difficulty;

This statement uses a pre-increment operator to increasing the value stored in difficulty by one. This ensures that the loop doesn’t continue infinitely.

if false the loop exits and the program continues.

  1. cout << “WOW - you’re a master programmer\n”;

This line prints a congratulatory message to the screen.

  1. return 0;

This value is returned to the operating system, if the programmed terminated successfully.

Hello, I’ve just started the UE4 C++ Developer course on Udemy.

It looks like the function main() is setting two integer values, one is “difficulty” and the other is “maxDifficulty”. The while statement is saying if the difficulty is less than or equal to the max difficulty (set earlier with int maxDifficulty = 10; ), do whatever is inside the brackets. Which I don’t quite understand.

I don’t know what std::cin.clear(); is doing… other than what’s commented “clears the failbit”
Or the std::cin.ignore(); which discards the buffer.
++difficulty; (no idea here either)
std::cout << “WOW - You’re a master hacker!\n”; - I’m guessing this measures something, then if it meets some certain criteria, it prints the message and returns a new line.

That’s about as much as I know :slight_smile:

It is where the program begins. You can write all code in main and it’ll execute but splitting it up into functions and files is recommended

I think the main() function is just the starting point and base for most of the code in a script. By the looks of it, it clears the terminal of anything that was previously in it. It looks like it will start with a difficulty of 2 and keep increasing until 10. But I’m not sure what the clear and ignore lines are clearing and ignoring

Hi there. I just joined today and thought I would take a stab at this. I have been coding for a while in different languages and am looking to add C++ to my belt.

Main is the primary function that is called when the program is started up. By returning the 0 at the end we tell the system that our program ran successfully. As for what the function does, here is the breakdown.

  1. Clears the terminal so we can start with a fresh clean terminal
  2. Initializes two difficulty variables. One for current and one for max.
  3. The While loop serves as the game loop. It calls the PlayGameAtDifficulty with the current difficulty. The if statement will evaluate if the current difficult is less than or equal to the max difficulty, (that way the game does not go on forever.)
  4. With each iteration after calling the Play game function it clears the console of any user input and increments the difficulty
    5.Lastly, once the current difficulty is more than the max difficulty it writes out the game win message and ends the game.

I think main() is telling the computer where to pull functions from. :thinking:

Ok, so i think main() controlls more important functions in our program. In this segment, main() controlls difficulty and raises it if password was correct

if i am understanding this correctly. I am still new to coding. Main() runs all the code inside of it. While the difficulty is less then maxDifficulty the game will continue to run.

The “main()” defines where the program starts, so the compiler can know what will be the first things to run the moment the proggram is executed.

int main () is a function that contains the instructions laid out in the code snapshot. These instructions first clear the terminal, they then lay out a range of possible difficulties
int difficulty = 2;
int maxDifficulty = 10;
These are integer value variables which must resolve into whole numbers. We then are given a while loop that expresses while the value of difficulty is between 2 and 10.

PlayGameAtDifficulty(difficulty) is a function that passes the integer difficulty as its argument. Then clears a fail state if the player fails.
std::cin.ignore(); is called to ignore the buffer introduced in the std namespace function cin.
++difficulty iterates the difficulty by a value of 1 greater than the int value.

In the case of a win state, std:cout since namespace std isn’t declared prints to the terminal that the player is a master hacker, and then ends the program without an error message.

The main() Function, is the starting point/portal that we run our game/program.

The main() method is where we will be running the functions that we created.

okay so I don’t know anything about C++ and this is my first challenge so I’m going to give it a try.

I think the main section is the code that determines how hard the game is and how many levels there are.

Whenever you run a c++ program, the first thing that happen is the everything that is Inside the main, so after #includes, Main() is what called automatically by the computer we dont even need to tell the computer to run this part of code.

The Main() function appears to begin a new game by clearing the terminal, sets difficulty at a predetermined magic number of 2, and the highest difficulty level as an arbitrary 10.
It then calls a second function PlayGameAtDifficulty, passing the difficulty level as an argument. This second function performs its tasks at the designated difficulty level before returning to the point in Main() where we called it.
I am not yet sure what the next two lines are for(clearing the failbit and discarding the buffer) but it then proceeds to increment the difficulty level by 1 and check if we have exceeded the highest (max) difficulty setting. If not, it will call the PlayGameAtDifficulty function again, passing the new difficulty value. If it has exceeded the max difficulty, then it will proceed to tell the player they are a master hacker via a console message and end the game, presumably either closing the terminal window or returning to a command prompt.

main() is the Alpha and Omega of staring a program and then ending it at some point.

All code from file will be compiled and excuted. Compilation is not “main() does” issue, it’s compiler’s duty completely. After compiling(and some other black magic things), our code is ready for execution. Generally, what code will be executed and how it’ll be executed - these two things-to-know which the main() function gives to operation system.

from what i know from other programming languages the main function is the one that is executed when the compiled program is run. so it defines the current difficulty to 2 and the maximum diffictulty to 10 and then consequentially runs PlayGameAtDifficulty() function as long as the current difficulty doesn’t surpass the maximum one - adding 1 to the difficulty after each time the PlayGameAtDifficulty() function is called.
i guess the terminal is cleared after each round as well, but I am not really sure about that failbit/buffer buisness… I also don’t know why the main function needs an int return value. I guess it’s part of error handling, but so far i have never used a return value in a main method…

I believe the Main function is the controller of the game, that runs and decides what to do next.

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

int difficulty = 2; //initial difficulty
int maxDifficulty = 10; //maximum difficulty it can reach
while (difficulty <= maxDifficulty) //if difficulty is less than or equal to maxDifficulty
{
PlayGameAtDifficulty(difficulty); //sets difficulty
std::cin.clear(); // clears the failbit //notsure
std::cin.ignore(); // discards the buffer //notsure
++difficulty; //increases difficulty
}

std::cout << “WOW - You’re a master hacker!\n”; //if you complete it say this
return 0; // exit with no error code
}

I went over and had a quick go at adding comments to what I believe they do, previous comments have been left.

First, the main function is the entry point of the program. It is where the execution starts.

First, the terminal window is cleared so that we will only see game-related text during execution.

The starting difficulty is set to 2 and the maximum difficulty is set to 10. Then, the game will be played. Before playing again, the difficulty is incremented by 1. Once the level 10 difficulty iteration is complete, the while loop will terminate, causing the “WOW - You’re a master hacker!” message to be displayed.

Interestingly enough, it seems you could speed run this game by just typing in random inputs each time. Even if you fail, the while loop appears to keep you progressing to the next level.

Privacy & Terms