Well we all know what program everyone writes first dont we?
Yep, you got it. Hello world!
So first things first, you must choose your IDE and compiler. I am not going to go into depth about this at all, but 2 IDE's i suggest are:
Code blocks is a more user friendly IDE as it has good syntax highlighting and other nice features.
So now you have your IDE its time to setup your file.
First you need to include IOStream. IOStream stands for "Input/output stream" and will allow you to use commands like "Cout" for output and "Cin" for input, of which we will go into later.
To include IOStream simple write at the top of your file:
Now that you have iostream you can set the program to use "Std" (standard) as the namespace, this will allow you, for example, to write:
Instead of
Normally for small programs you would not use it as the namespace, but for the purpouse of this tutorial we will. To use "Std" as the name space simply write (underneath your includes)
So far our code should look like this:
Now you may be thinking, what are those "//" doing there. Well the double slashes are line comments, anything wrote after them on the same line will not be compiled as code, and therfore not run. They are there to help the programmer comment their code. Another form of comments is a block comment, which will comment out anything written between "/*" and "*/". For example:
Moving on, it is now time to add the "Main" function, which is the first bit of code the program looks at when run. There are many different ways of using "Main" but for this tutorial we will declare "Main" as an integer (int).
To do this type:
The "{" and "}" simply group chunks of code together, and it is inside these we will create our hello world program.
So far our code should look like this:
Now, at this poilt the program will compile and run. However if you do chose to compile and run, all that will happen is a console screen will flash for a matter of milliseconds.
So now we are actually going to actually make our program do something. Inside your main function, write:
Now let me explain this.
cout :Is an output command, which will output data. In our case it is the text "Hello world!"
<< :These are simply characters that link commands within a "cout" for example
Would NOT work.
endl :This tells the program to go to the next line, so the next output would happen on the next line.
; :This tells the program that the command has ended. If we did not include this then the program would not compile
Our code should now look like:
This code will compile, but if you run it, it will still flash a cmd screen really quick and nothing more.
To solve this we need to pause the program.
Now, there are countless ways to do this. I am going to explain the best way for windows only programs, and for multi platform programs.
Windows
For windows we can use
This will display "Press anykey to continue..." and then wait for a keypress before continuing.
Multi platform
For multiplatform you can use this code.
This will clear any characters cin may have stored, and then wait for a character input to continue.
The problem with this method is that you need to write your own text for it, and you have to press enter to continue
So for now we will use system("PAUSE"). Add it in after the cout command.
Now the code should be like this:
Finally we will add something for our code to do when it reaches the end.
This returns the value 0 to main, which makes sure the program cleanly exits.
So our final code will be:
Comile and run. Congrats, you just wrote your first c++ program!
Soon ill will write tuts on variables, loops and OOP using functions.
Yep, you got it. Hello world!
So first things first, you must choose your IDE and compiler. I am not going to go into depth about this at all, but 2 IDE's i suggest are:
- Code::Blocks
- DevCPP
Code blocks is a more user friendly IDE as it has good syntax highlighting and other nice features.
So now you have your IDE its time to setup your file.
First you need to include IOStream. IOStream stands for "Input/output stream" and will allow you to use commands like "Cout" for output and "Cin" for input, of which we will go into later.
To include IOStream simple write at the top of your file:
Code:
#include <iostream>
Now that you have iostream you can set the program to use "Std" (standard) as the namespace, this will allow you, for example, to write:
Code:
cout
Code:
std::cout
Normally for small programs you would not use it as the namespace, but for the purpouse of this tutorial we will. To use "Std" as the name space simply write (underneath your includes)
Code:
using namespace std;
So far our code should look like this:
Code:
//Includes
#include <iostream>
//Namespace
using namespace std;
Now you may be thinking, what are those "//" doing there. Well the double slashes are line comments, anything wrote after them on the same line will not be compiled as code, and therfore not run. They are there to help the programmer comment their code. Another form of comments is a block comment, which will comment out anything written between "/*" and "*/". For example:
Code:
//This is a line comment
/* This
is
a
block
comment*/
Moving on, it is now time to add the "Main" function, which is the first bit of code the program looks at when run. There are many different ways of using "Main" but for this tutorial we will declare "Main" as an integer (int).
To do this type:
Code:
int main()
{
}
The "{" and "}" simply group chunks of code together, and it is inside these we will create our hello world program.
So far our code should look like this:
Code:
//Includes
#include <iostream>
//Namespace
using namespace std;
//Main
int main()
{
}
Now, at this poilt the program will compile and run. However if you do chose to compile and run, all that will happen is a console screen will flash for a matter of milliseconds.
So now we are actually going to actually make our program do something. Inside your main function, write:
Code:
cout << "Hello World!" << endl;
Now let me explain this.
cout :Is an output command, which will output data. In our case it is the text "Hello world!"
<< :These are simply characters that link commands within a "cout" for example
Code:
cout << "Hello World!" endl;
Would NOT work.
endl :This tells the program to go to the next line, so the next output would happen on the next line.
; :This tells the program that the command has ended. If we did not include this then the program would not compile
Our code should now look like:
Code:
//Includes
#include <iostream>
//Namespace
using namespace std;
//Main
int main()
{
cout << "Hello World!" << endl; //Print hello world
}
This code will compile, but if you run it, it will still flash a cmd screen really quick and nothing more.
To solve this we need to pause the program.
Now, there are countless ways to do this. I am going to explain the best way for windows only programs, and for multi platform programs.
Windows
For windows we can use
Code:
system("PAUSE")
Multi platform
For multiplatform you can use this code.
Code:
cin.clear()
cin.get()
This will clear any characters cin may have stored, and then wait for a character input to continue.
The problem with this method is that you need to write your own text for it, and you have to press enter to continue
So for now we will use system("PAUSE"). Add it in after the cout command.
Now the code should be like this:
Code:
//Includes
#include <iostream>
//Namespace
using namespace std;
//Main
int main()
{
cout << "Hello World!" << endl; //Print hello world
system("PAUSE"); //Wait for a keypress
}
Finally we will add something for our code to do when it reaches the end.
Code:
return 0;
This returns the value 0 to main, which makes sure the program cleanly exits.
So our final code will be:
Code:
//Includes
#include <iostream>
//Namespace
using namespace std;
//Main
int main()
{
cout << "Hello World!" << endl; //Print hello world
system("PAUSE"); //Wait for a keypress
return 0;
}
Comile and run. Congrats, you just wrote your first c++ program!
Soon ill will write tuts on variables, loops and OOP using functions.