So, what do YOU think main() does?

I think we write the code in main the we want to prioritize and the code that we want to complete first even if it’s after the void functions.

I’ve started the UE4 C++ Developer course on Udemy!

Zero knowledge here. So, let’s see what we got. Yeah, yes, I get it I think. The main() is…
I don’t know. Without a knowledge base I have no reference to an answer or guess. It looks like gibberish. Breaking it down, I sense what it’s doing. Main is the main. Main main. So from main() is it? It’s the main. From zero knowledge I say the Main() is the first? Being main. What goes into the main() drives the game forward?

std::system(“clear”) so the main() is (“clear”) here. Anything in main() is the main().

I look forward to looking back on this reply with a larger knowledge base.

i think that main() function does the following:

firstly, it clears the systems console.

secondly, it states the minimum and maximum difficulty.

thirdly, its saying while the difficulty level is below the max level, it runs the playgameatdifficulty function.

fourthly, once youve completed the challenge, it states that you are a master, and returns with no error.

I dont know a lick of c++ so i hope i did well haha

Starts by clearing variables?
sets a current difficulty and max difficulty. while less than or equal to max difficulty, play the game at the selected difficulty.
Don’t understand what failbit is.
Never used a buffer in code.
++ maybe adds one (or two) to the difficulty as a step.
player wins?

The main() method creates two integer variables, difficulty and maxDifficulty, which indicate the starting level and the maximum level. The while loop checks if the difficulty is less than or equal to the maximum difficulty, and if so, call the other method with the value of the difficulty. After the method is called, the difficulty variable is added by one. The for loop goes back again, and repeats the same code until the difficulty variable is greater than the maximum difficulty.

honestly i am completely new to this and i do not know what the main () does.

I think main() is where the compiler begins to interpret the code.

Main() is the first function called within your a program. In this program’s Main() function, we first declare two variables that are probably being used to set the difficulty of the current/next level. Then, as long as we are not at the maximum difficulty, we used the PlayGameAtDifficulty() function and pass in the difficulty parameter to start a new level. I don’t really know what std::cin represents, but I think it’s something about the terminal inputs. So, I’d say we clear the terminal using the clear() function and ignore all following user inputs using the ignore() function until the next level is started. Finally, when the user has completed all the difficulties, in other words, when the difficulty variable is greater than the maxDifficulty variable, we using std::cout to tell the user he succeeded.

To be honest i don’t know any programming languages i’m a total beginner.

Main() is probably the head code of the game that you play, i haven’t read other peoples comment because i want to see if i’m correct without any knowledge and how far i can come with this course.

It seems the main function is in charge of preparing the terminal/program and determining the integers (ints) that correspond to values, it also seems to fetch the other ( I want to say objects) scripts such as the “playGameAtDifficulty” code.
Finally it looks like it is the place where you want to the end program.
To me this looks most like a game engine, something that fetches other pieces of vital information and compiles them into one place in proper order.

Probably talking a bunch of faff but hey oh.

{ Start of the main() function
std::system(“clear”); clears the terminal

int difficulty = 2; Sets the lowest difficulty to 2
int maxDifficulty = 10; Sets the highest difficulty to 10
while (difficulty <= maxDifficulty) A while loop when the difficulty is between 2 and 10
{
PlayGameAtDifficulty(difficulty); Starting the PlayGame function at the difficulty of int difficulty
std::cin.clear(); clears the failbit
std::cin.ignore(); discards the buffer
++difficulty; Continuing to add one extra number to int difficulty for each cycle of the while loop
}

std::cout << “WOW - You’re a master hacker!\n”; If he complete all of the round’s he get this message
return 0; // exit with no error code
}

Personally i would have put a constexpr before the int maxDifficulty

Hello everyone,

The code makes sure that your terminal is empty of clutter when the code is run. It creates two variables of starting difficulty and max difficulty. While the difficulty is less than the max difficulty, the PlayGameAtDifficulty function will keep loop.

I don’t know what cin.clear() and cin.ignore() do. However, the difficulty increments after a game. Once you’ve succeeded in max difficulty, the computer will say you’re a master hacker.

Then the main returns 0.

Its a code function that can compile the code to watch for errors and if they’re no errors the value returns

main is the function in charge to execute the code of the program, in this place is executed locally and it uses external functions to perform calls to external code. Thats what i think but i´m starting with the course and i´m not pretty sure about it.

I’m a total beginner in terms of programming language, but based on the code shown and the function being called “main”, I think, this is probably the first function that happens when we compile the code and if there is no error only then rest of the code will work (maybe?).

The main functions does the following

  1. Clears any text currently in the terminal from previous code.
  2. Creates two separate variables, difficulty and maxDifficulty and then initialises them as 2 and 10 respectively.
  3. Creates a while loop in which the stopping point is the difficulty being equal to the maxDifficulty.
  4. In this loop it runs the PlayGameAtDifficulty function, taking the current value stored in difficulty as a parameter.
  5. It clears the failbit (Don’t know what this means yet).
  6. Discards the buffer (I’m assuming this is the memory buffer most likely for the users inputs).
  7. Increments the difficulty by one.
  8. Once the stopping condition is met, it outputs the victory message then ends the code.

Most of this is a guesstimate based on what I have learned in Java so far, as there are a couple similarities as well as core concepts such as while loops which appear in both languages.

The main function here creates and initializes two variables difficulty, maxDifficulty. It then initiates a while loop the continues the following processes when difficulty is less than maxDifficulty: execute a function called PlayGameAtDifficulty passing it the difficulty variables current value as an argument, then a clear function is ran, an ignore function is ran, and then difficulty is incremented.

When the loop is exited, the function prints the master hacker statement and then returns the standard no error return value for the main function.

Hey all! Just started the video series on Udemy and wanted to do the first challenge.
So to start of with as the name suggests
int main()
is the primary function of every program. It is where all your functions will be pulled together to create the final product, in this instance I should say since this is a single file project that doesn’t include any outside files. The
PlayGameAtDifficulty()
function is the area that contains most of the meat of the project. In here you can find where the actual game mode is being established and then the predefined “rules” of the game can be exported to the main function where a few additional parameters are brought up like that the system needs to accept user input, and the success message is predefined.

P.S.Also @ben I wanted to ask you because I’ve noticed this in alot of the different dev content I’ve viewed but is there a reason that you don’t use
using namespace std;
Ive noticed with a few seperate developers that they prefer to always have
std::
as a precursor to all their code that has to do with
<iostream>
I was wondering if its an efficacy thing that makes developers do this instead of utilizing using statements.

The second line of code - std::system("clear"); clears the screen much like the linux clear command.

The fourth and fifth line of code define the current difficulty and the overall max difficulty variables. After that there is a while loop that runs while the difficulty does not exceed the max difficulty variable. After that there are two commands which i do not know what they do. At the end of the while loop the difficulty is incremented by one.

If you win the game I suppose (since in order for the while loop to finish the difficulty needs to be larger than it’s max value), the program prints out the given text.

There is one thing I do not understand - if a player loses, I’m pretty sure it won’t print out the “WOW” text and since there’s nothing in the main function, there must be something in the PlayGameAtDifficulty() function that exits the program before the stated cout line is executed.

What I think, main() does: (I look forward revisiting my reply after I finished the course :grin: )

The ints at the start are stored variables for the difficulty and the maximum difficulty defined by the programmer. The while statement executes the “clear” and the “ignore” functions and adds 1 to the int “difficulty” as long as the “difficulty” is smaller or equal to the int of “maxDifficulty”.
when the while statement is finished (because maxDifficulty is reached) the program prints the “master hacker” text.
return 0 closes the program in the end.

Privacy & Terms