C++ Tutorial #1 — Hello World


Topics covered: During this video I introduce #include (used to include a header file into a program) using namespace std (this will be discussed at length later on) and cout.

Source code: Unavailable for the first 3 lessons (if someone would like to transcribe it for me I’ll post it)


Hello World
“Hello World” is the quintessential “starter program” in most programming languages, we simply include enough files to write this message to the screen, and then try to explain what each part of the program does. In this case the program will look something like this (I don’t have the source code transcribed for this video right now):


#include <iostream>

using namespace std;

int main(){

cout << "Hello World" << endl;

return 0;
} //end main

Now let’s break the above code down part by part and explain what each part of it does.

#include <iostream>
This line tells our compiler to “include” the library “iostream”, which is also known as input/output stream. This library contains all the code required to use the functions cout / cin (covered later) in our programs. For quite some time cin / cout will be all that we’re really using iostream for, as many of the more advanced uses of it just aren’t sensible at this point.


Using namespace std;
Namespaces can be fairly confusing to some people, because until you see what the above program looks like without this statement it’s hard to understand what exactly it does, so I’ll show you that, and then try to explain it a bit better:


#include <iostream>

int main(){

std::cout<<"Hello World" << endl;

return 0;
}

In the above code you’ll notice that I left out the using namespace std; but had to change cout<< to std::cout<<. This is what the using namespace std; line does. It allows us to use items from the ‘standard namespace’ without mentioning the namespace that they’re in. We’ll talk about namespaces MUCH later on in the series when we get out of using just STD, and maybe get into Boost and other libraries.


int main(){
This line is fairly simple, it shows where the start of the “main” part of our program is. I want you guys to take special notice to the { though. That’s known as a Scope Bracket, these can make your life easier or harder based on the habits you develop with them now. You guys should use this as something of a rule when it comes to programming: “Every line I write should end with either a ; or an { / }. This will make your life much easier, especially in the early stages of programming.


cout <<”Hello World”<<endl;
This line calls the function “cout” from the iostream library, and passes in the text string “Hello World”. The part after that (endl) is just to make a new line after that. We’ll get more into how this works in subsequent lessons


return 0;
This line tells the compiler that it can “return” (to the command prompt) if everything went well. The return command will make more sense later. The reason we’re using return 0 and not some other number will be covered way later as well.


} //end main
This is really just a closing bracket } , the //end main after it is what’s known as a comment. Comments are used by programmers to keep track of what’s what in a program. In this case when you have a longer program with many brackets, you might want to know that this bracket is closing your int main() opening bracket.


13 comments

  1. Ben says:

    Hi, great tutorials!

    My build keeps failing when i “Run Main Project” even though my script is identical to yours

    it says:

    BUILD FAILED (exit value 2, total time: 5s)

    Any ideas?

    Thanks

    • Nick says:

      Same thing happens to me. I’ve restarted the project numerous times and made sure it’s identical. Quite frustrating.
      “BUILD FAILED (exit value 2, total time: 1s)”

      I’m running NetBeans 7.1.2

      • Nick says:

        So, I left, came back, and reran the code. I didn’t so much as add a space, but it decided to work for me randomly. Best guess, “rerun” the program instead of trying to “run” it several times.

        • Damien says:

          Yep, sometimes netbeans can be VERY tempermental when it comes to getting started up right. When I use it at work I’ll get jvm’s failing for 10 minutes and 2-3 reboots… it happens. Netbeans isn’t perfect for C++, but it’s free and accessible to everyone and it has my personal favorite debugger.

    • Jesper says:

      Hi! I got the same problem..

  2. Matt says:

    I’m getting the same problem as Ben. I have identical script, but it comes up as BUILD FAILED at the bottom.

  3. Bryant says:

    I think I have a solution to all of your problems. After trouble shooting for about 2 hours I found that when you are creating a new file it places that new file under “Important Files”. You actually need the new C++ file to be in the “Source Files” Section. There is usually already a templete c++ file in there that you can use titled “main.cpp”. Hope this helps!

    • Damien says:

      Yep, I actually mention this in one of my first videos when showing you how to make a project, I say specifically “Be sure to untick the “make main file” button here. It seems as though I should have just kept it and shown people it was there as this is a common problem.

  4. Michael says:

    I also ran into a BUILD FAILED error. I tried restarting NetBeans and making a new project, and then doing that again, unchecking the “Create Main File” box as some previous posts have seemed to suggest.

    It seems that whenever I try to type the code exactly how you did, it fails, but if I were to copy and paste your code from the description, it runs fine. The only difference that occurs is that after you enter

    int main()(

    you enter two line breaks, and then a “nesting bracket” automatically appears on the left by the line numbers, but it doesn’t happen for me. Then, every line from then on becomes flagged as an error.

    Screenshots of my program and “log” (sorry, that’d be my SAS background showing through) can be seen here: http://imgur.com/a/1wNE0

    I’d appreciate your help, and I really appreciate everything you’re doing here.

  5. Tommy says:

    If you ever run across the “Build Failed” problem like other people have, make sure you’re using these squiggly brackets { } when you type in

    ‘int main(){ Squiggly.

    blagh

    }’

    I had a derpy moment with those. Heh.

    • Damien says:

      This was unfortunately due to a bug that used to exist in netbeans, it was a HUGE pain in the ass. It’s been fixed since 7.0

Leave a Reply

Your email address will not be published. Required fields are marked *

*