Learn to Code via Tutorials on Repl.it

← Back to all posts
C++ An Introduction: Part 2
ipastrano (46)

Hi! Sorry, but I spaced out and forgot all about this thing on Saturday, so here it is.
EDIT: I am going to update on Sunday, that works better for me.
As before, if there are questions or comments or errors relating to this post, comment.
EDIT: I have the next tutorial up :D

The Main function

  • The main function is where most of your code will be(until we get to writing other functions, we will talk about that later.)
  • To declare your main function, use this statement:
int main() {

+ Let's break this down a bit:

  • int is the return type, an integer. Whenever you create a function, the return type tells you what type of data it will send back to the program.
  • main is the name of the function. Note that you can't just name any function main; is is special and is only used once.
  • The parenthesis () hold what data to send into a function. The main function doesn't need anything, so leave this blank.
  • The opening and closing curly braces {} signify where the program starts ({) and ends (}). all the things you want in the function go there.

Output to the screen

  • Ok, ok. You want to actually do something. Here I will tell you how to output text to the screen. Note that this gives you plain, uncustomized text to the screen. BORING.
  • To do this, use the following line.
cout << something;
  • cout will send output to the screen, to display something, put the content inside double quotes "" and BE SURE TO END YOUR LINE WITH A SEMICOLON. I can't stress that enough.
    Here's an example:
std::cout << "Hello World!";
//if you don't want to have to put std::, put using namespace std; outside of the main function.
//Also, make sure that the << are facing toward cout. If you don't, you get a lot of nasty errors.

Now, if you want to make a new line, use endl in this fashion:

cout << "Hello World!" << endl;
//Also, make sure that you use another set of <<.
  • Or, use the escape sequence \n inside the double quotes.

That's all folks!

Commentshotnewtop
ipastrano (46)

@Highwayman Yep! It's here! Sorry I didn't post on Saturday, but I'm glad it came together!