Jun 6, 2012

C++/Cpp Hello World application for beginners

I decided to make some c++ tutorials.
This is for beginners, an hello world application

Before we start make sure you have installed Dev Cpp (compiler).

In cpp we start with:
#include <iostream>
This include an c++ library.
using namespace std;
This must be included to use the print functions(i/o) cout and cin
int main(){
#code here
}
this is a function but not just an however function because it telle to the compiler that this is the main of our code, where the compiler should start.

To print something we must use cout just like this
cout << "Hello world!";
this print hello world on the cmd (command line).
But the cmd gets closed! yes it executes and the gets closed.
To stop that we must add:
system("pause");

Ok stop talking! Lets get into real coding:

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!";
system("pause");
}

And what it prints is:

Hope this tutorial helped you, and dont forget to share :)

7 comments:

  1. after system("PAUSE");
    you need to add return 0;
    in order to to tell the compiler what to return , in this case the code that you writed dont need it but , on other coding you need to add that line.
    Also as an alternative of system("pause");
    can include #include library than after main add
    int main()
    {
    string exit;
    .....
    ....
    .....
    cin>>exit;

    return 0;
    }

    ReplyDelete
  2. yeah your right :) and thanks for the last tip :D

    ReplyDelete
  3. Sorry , also as an alternative of system("pause");
    can include #include i have writed include#include o.O .

    ReplyDelete
  4. In some compilers or IDE's, system("pause") will not work if you will not #include on the header. For beginners who doesn't want to bother themselves on what to put on the header (the place where you typed #include , you can try this code:

    NOTE: "//" - stands for one line comment

    #include // header so that we can use "cout" function

    int main() // main function. every program have a function like this.
    {
    cout << "Hello World!"; // "cout" command prints the words enclosed in the "" sign.
    cin.get(); // this makes the program wait for a return keysroke before moving to the next line.

    return 0; // all is well command. think of it as a programming convention.
    }

    for a cleaner code (without comments):


    #include

    int main()
    {
    cout << "Hello World!";
    cin.get();

    return 0;
    }

    ReplyDelete
  5. Thanks for the tip liquid, i apprecate it :)

    ReplyDelete
  6. better used this c++ code
    #inlude
    #include
    using namespace std;
    int main ()
    {
    int i;
    for(i=0;i<10;i++) // :D
    {
    if(i<10)
    cout << "hello world"<<endl;
    if(i==10)
    cout <<"end hello world"<<endl;
    else
    cout <<"Error :("<<endl;
    }
    getch(); // system("pause");
    return 0;
    }
    // :D

    ReplyDelete
  7. none of them work on Visual C++ 2010

    ReplyDelete