Basic Input Output in C++

Input Operations are supported by istream (input stream) class.
The istream input operation is referred as extraction which is performmed by extraction operator “>>” (“get from”) and is used to read a value from standard input.

Output Operations are supported by ostream (output stream) class.
The ostream output operation is referred as insertion which is performmed by insertion operator “<<” (“put to”) and is used to put the value to standard output.

Predefined Stream Objects – 

There are predefined stream objects which provides basic input output in c++ –

basic input output in c++

  1. Cin – means console input, and it is a istream class object tied to standard input. (For eg Keyboard)
  2. Cout – means Console output, and it is a ostream class object tied to standard output. (For eg Monitor)
  3. Cerr – means Console error, and it is a ostream class object tied to standard error.

A simple program to perform basic input output in C++ by using sum of two numbers – 

#include<iostream.h>
#include<conio.h>                             // Line 2
int main()
     {
     clrscr();                                // Line 5
     int a,b,sum;                             // Line 6
     cout<<"Enter value of a and b\n";
     cin>>a;
     cin>>b;
     sum=a+b;
     cout<<"sum of a and b is=";
     cout<<sum;
     return 0;
     }
Output of the above program is :
Enter value of a and b
5
6
sum of a and b is=11
Explanation of the above Program :

Line 2 and Line 5 : #include<conio.h>  → “conio.h” is a header file  named “Console Input Output” which is used here to insert clrscr () function. The  clrscr() function stands for ClearScreenand as the name suggests it clears the screen when we compile the progran and it moves the cursor to upper left hand corner of screen.

Line 6 : int a,b,sum → Line 6 defines three- integer variables a, b and sum. Next Section will describe what is a variable.

Cascading Of  I/O Operators

Cascading of IO operators (“<<”  “>>”) means concatenation of successive occurrences of these operators.

The above program can also be written with in a few lines as
#include<iostream.h>
#include<conio.h>                             
int main()
     {
     clrscr();                                
     int a,b,sum;                             
     cout<<"Enter value of a and b\n";
     cin>>a>>b;                       //cascading of Input Operator
     sum=a+b;
     cout<<"sum of a and b is="<<sum;  //cascading of Output Operator
     return 0;
     }
Output of the above program is :
Enter value of a and b
5
6
sum of a and b is=11

The output(with cascading) looks like similar to the output(without cascading). So, it means that there will be no effect on output on cascading of IO operators. It just concentrates the length of the source code written by user.


Previous Home Next
C Basic Introduction (Continued..) (Day2) Types of errors in C++ (Day4)

     

Leave a Reply