Basic C++ Input Output

Basic C++ Input  Output: cin, cout, endl are used in the streams.  A stream is nothing but a sequence of bytes (or) flow of data. Let us know in detail regarding input and output operations/ streams.

Basic C++ Input Output (cin, cout, endl)

 Standard Input stream (cin)

It is connected with the standard input device which is usually known as “keyboard”. The cin is the predefined object for istream class. And this cin is used with conjunction with stream extraction operation (>>) to read input from the console.

cin Example:



#include <iostream>  
using namespace std;  
int main( ) {  
  int age;  
   cout<<"Enter your age: ";  
   cin>>age;  
   cout<<"Your age is: "<<age<<endl;  
}  

Output:

Enter your age: 15

Your age is: 15

Standard Output stream (cout)

It is connected with the standard output devices which are usually used to display the screen.  The cout is the preferred object for ostream class. And the cout is used in the conjunction with the stream insertion operator (<<) to display output on the console.

cout Example:

#include <iostream>  
using namespace std;  
int main( ) {  
   char xyz[]="Welcome to freshersnow tutorial";  
   cout<<"Value of xyz is:"<<xyz<<endl;  
}  

Output:

Value of abc is: Welcome to freshersnow tutorial

Standard end line (endl)

The standard end line (or) the endl is a predefined object of the ostream class. It is used for inserting the new line characters.

endl Example:

#include <iostream>  
using namespace std;  
int main( ) {  
cout << "freshersnow Tutorial";     
cout << " C++ tutorial"<<endl;   
cout << "End of line"<<endl;   
}

Output:

freshersnow Tutorial  C++ tutorial
End of line

I/O Library Header Files

In C++ we are having different header files. They are as follows:

  • <iostream>
  • <iomanip>
  • <fstream>

1) <iostream>

<iostream> stands for “standard input-output stream“. It contains definitions to objects like cin, cout, cerr, etc;

2) <iomanip>

<iomanip> stands for “input output manipulators“. This method declares the files which are used in manipulating streams. i.e.setw, setprecision etc;

3) <fstream>

<fstream> describes the header file. This header file is used for handling the data i.e. being read from a file as input (or) the data being written into a file as output.

Features of Input & Output in C++

  • C++ IO operations are based on the streams. And are also device independent.
  • And C++ IO type is safe.

fstream Example:

#include<iostream> 
using namespace std;   
int main() 
{ 
    int age;      
    cout<<"Enter your age:"; 
    cin >> age; 
    cout<<"\nYour age is:"<<age;  
    return 0; 
}

Output:

Enter your age: 25
Your age is: 25