Hey,
I thought for all those beginners in C++ it would help to put up the most basic tutorial, every programming site needs its own hello world tutorial after all

and i have not seen one.
Ok i am using Dev-C++, if you are not using the same compiler then somethings maybe different such as your includes names etc...
First i want you to make the program before i explain it, so open up your compiler and start a new console application project, if you have not already got a .cpp file added to your project you will need to add one, normally file -> new ->file then select .cpp file.
I have called my .cpp file main as this is a name that best describes it for me, you may call it something else.
Now add the following code:
Code:
//My first program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
system("PAUSE");
return 0;
} Ok compile and run your program, your output should now be displayed on a console window
Now to break down the code so i can explain each bit.
This line does not do anything to change the program, this is simply a comment for you, i would recommend you comment each line of you code so if you come back to it in say a year you will know what it is.
So clearly to start a comment you need to start you line with "//" if you run onto the next line you will again need to put another "//" at the start of the line. Alternatively if you have a big comment that will run onto more than one line you can use "/*" at the start of your comment (without the double quotes) this will then let you go onto many lines with no problems as long as you end you comment with "*/" examples:
Code:
//comment line one
//comment line two
/* comment that can
run onto many different
lines without problems */
Code:
#include <iostream>
This line tells the compiler to include the iosteam.h file, the iosteam contains part of the standard library (input-output part in this case) that is needed to run some of the code, such as the cout and endl in this program.
Code:
using namespace std;
This is the line that can save us a lot of typing, without this line we would have to type std::cout or std::endl, all the files in the C++
standard library declare all of its entities within the std namespace. That is why we have included this statement, normally any program where iostream is declared we declare this namespace std.
Main is the function, every program you make in C++ must contain a main() function. We use int so we can return a number at the end of the function, that explained later...
The curly braces must be used as this is where your statements go within, "{" to open and "}" to close.
Code:
cout << "Hello, World!" << endl;
This is the code that actually produces the output of Hello, World!, the first part "cout << "Hello, World!" is sending the text to the console, this text will then displayed, make sure you use the arrows facing the correct way as they are commonly mixed up with cin's arrows which face the opposite way.
The next part of the code is "<< endl;", again the arrows are sending it to the output but this will not display a message this will make a newline.
You could try replacing the code in this part with:
Code:
cout << "Hello, World!";
cout << endl;
This will still do the same job, if you would like to try removing the endl line all together you will see that the message you want to display and the system pause message is on the same line, now you can see what endl actually does
This line will stop the console from closing, if you want to try taking this line out and recompiling it you may, you will see that without this line it will flash your console with your text and close immediately so you will not be able to see your output, with this line in it will stop it from closing and only close when you press a key or close it.
This is where we return the number we mentioned before, if the main() returns 0 then that indicates the program has run successfully, if it returns another number then the program did not run as it should.
----------
Notice how each of the statements end with a semi-colon, this is important, without these your program will not work.
----------
I hope this tutorial has helped some of the people new to C++. Any Questions just post them and i will do my best to help out
