Monday, 6 January 2014

Filled Under:

Basic input and output instruction in c++

Basic input and output instruction:

               Two major function is use for basic input cin and getline.
                and for output we use cout

Output(cout)

Library /header file:

 #include<iostream>

Explanation:

                   Cout is used for basic output in c++. Cout is used with two less than signs which is written as “cout<<”.  The << operator insert the data cout  function  which is shows as output. The “<<” operator can be use more than one time in a statement.

Example:

 #include<iostream>
Using namespace std;
main()
{
Cout<<” this is programminglogic.net example”<<endl;
Int a=10;
Cout<<a<<endl;
Cout<<””the value of a is=”<<a;
}

Output screen of above example:

                  this is programminglogic.net example
                  10
                  the value of a is= 10


so the above example shows that if we write the string with cout it display it as it is. And if we write the variable with cout it display its value. As show is in output screen.
Note:
         String always written in double quotes . if we write the variable name in double quotes then compiler will take it as string not a variable.

Input(cin)

Library

          #input<iostream>
 

Explanation:

                 Cin is use for basic input in c++. User use the cin for taking input from keyboard. Two greater than signs use with c++. Which is written as “cin>>” . The operator “>>” also called overload operator.

Example:

 Int a;
  Cin>>a;     //  any integer value that user enter by keyboard it will store in variable a.

getline() function: 

            we normally use cin for taking input from user. but when user enter a string for input which including blank spaces. for example we have a string variable.
string info;
cin>>info;
let user enter "my name is umer". 
when user  display it my cout so then output display on screen "my" expect "my name is umer" it mean when cin find the blank space it stop the extraction. so the solution of this problem for string input we use the getline() function . which is more efficient for string input. and it take the string input with spaces.

syntax:

      getline(cin,variable name);

header file/library:

       #include<iostream>

Example(cin vs getline):

   #include "stdafx.h"
    #include "iostream"
   #include<string>           //for string data type
    #include"conio.h"         //for getch()
   using namespace std;
   void main()
{
string info1,info2;
cout<<"enter your first information==";
cin>>info1;
cout<<"enter your second information==";
getline(cin,info2);
cout<<"your first information is=="<<info1<<endl;
cout<<"your second information is=="<<info2<<endl;
_getch();
}
Note:
         every time user enter the input "my name is umer".

output:

enter your first information==my name is umer        
enter your second information==my name is umer  
your first information is==my                                    
your second information is==my name is umer        
you
you clearly seen in output when you use cin for taking input. only display "my" when you use getline then display all "my name is umer". because our input include blank spaces.

Combine example of cin and cout function:

     #include "stdafx.h"
    #include "iostream"
    #include "stdio.h"
   #include"conio.h"
using namespace std;
 void main()
{
            int a,b,c;
            cout<<"enter the value of a"<<endl;
            cin>>a;                                                      //input the value of a
            cout<<"enter the value of b"<<endl;
            cin>>b;                                                      //input the value of b
            cout<<"eter the value of c"<<endl;
            cin>>c;                                                        //input the value of c
            cout<<"the value of a=="<<a<<endl;         //output the value of a
            cout<<"the value of b=="<<b<<endl;        //output the value of b
            cout<<"the value of c=="<<c<<endl;        //output the value of c
            _getch();       //function use for display the all output until we press a char

}

output of above program:

enter the value of a
10
enter the value of b
20
enter the value of c
30
the value of a==10
the value of b==20
the value of c==30





0 comments:

Post a Comment