Friday, 17 August 2012

Output in C++

For this tutorial we will be using cout as the output function in C++

So first we need to include our input out put library like this
#include<iostream>

Next we need to specify what exactly we will be using in thing library
using std::cout;
or if you want to use everything in the library or libraries you could also use
using namespace std;

So now you will learn a bit about functions that is the main function of you c++ program a function is a set of instructions that you can call any place the main function in executed when a program runs but enough of that just know that you will need it for these examples for now later we will talk more about functions

Sorry guys forgot to mention the importance of your semicolons ; this is what tell the complier that you have finished a statement of you miss out you semi colons your program simple wont run

This is you main function

int main(){}
Within the { and } your instructions will go in there.

So a simple program displaying Hello World would look something like

 #include<iostream>//input out put library
using std::cout; // specfiy to use cout the library

int main() // main function of you c++
{
   cout << "Hello World"; // output Hello World in console 
   system("pause");   // pause program  
}
If you look at line 7 you notice cout this is the keyword we use when we want to display something from the istream library in c++ the << is telling the complier that there is something im going to be output next and "Hello World" is what is going to be output

if want run it go in dev C++ go to File > New > Source File


A blank page should pop up like below this is where you type you code


When your finish it should look like the this


You can run this by pressing F9 on your keyboard or going to Execute > Compile & Run on  the menu bar

Now when you run it will notice a problem the Hello World and the Press any key yo continue... is joined and is on the same line



But we would prefer to see this


Well this can be easily achieved  for us luckily there are 2 ways we can achieve this

1. is to use the endl this mean enter a new line and is use like shown below

cout << "Hello World "<<endl;
and you cout also do this </ br>
cout << "Hello World ";
cout << endl;
Or 2. You could use the end line character '\n' as show below
cout << "Hello World \n";
and you cout also do this </ br>
cout << "Hello World ";
cout << "\n";

If you decided to do it the first way remember to specify that you will be using endl this is in the code below on line 4



#include<iostream>//input out put library

using std::cout; // specfiy to use cout the library
using std::endl; // enter new line

int main() // main function of you c++
{
   cout << "Hello World"<<endl; // output Hello World in console 
   system("pause");   // pause program  
}

Here is the code using \n instead

#include<iostream>//input out put library

#include<iostream>//input out put library

using std::cout; // specfiy to use cout the library

int main() // main function of you c++
{
   cout << "Hello World \n"; // output Hello World in console 
   system("pause");   // pause program  
}
Here another thing you can do you can join text together with the << as shown below this is called concatenation
#include<iostream>//input out put library

using std::cout; // specfiy to use cout the library

int main() // main function of you c++
{
   cout << "Hello "<<"World \n"; // output Hello World in console 
   system("pause");   // pause program  
}

No comments:

Post a Comment