C++ Basic Input/Output

By | September 29, 2021

Every program we use must have some Input/output operations which make it more interactive for the user. In C++ we also have some standards libraries which are used to perform basic Input/Output operations.

All the input/output (I/O) operations in C++ performed in a sequence of bytes which is known as stream.

There are two basic streams we use in programming:

  • Input Stream
  • Output Stream.

Input Stream: The input stream fetches the data from devices such as the keyboard and secondary memory and loads it into the main memory.

Output Stream: In this data flow from main memory to the display or secondary memory.

Vamware

In C++ we have 3 libraries for the I/O operations and to include these libraries in our C++ program we need to include their header files.

  • <iostream.h> or <iostream>
  • <iomanip.h>
  • <fstream.h> 
iostream:

iostream stands for Input Output Stream and it is the basic header file for Input/Output operations. It defines the objects like cout, cin, cerr and clog.

Vamware
iomanip:

It stands for Input/Output Manipulators, and it defines the statements like setw, setprecision etc.

fstream:

This header file stands for file stream and it is used to read and write between the text file.

iostream objects:

This article is about Basic Input and Output in C++, so here we have only covered the object of <iostream> header files such as cout, cin, cerr and clog.

cout (Standard Output Stream):

cout is a predefined object of <iostream> header file and it is used to print the values and data on the standard output device such as display screen. It works along the insertion operation << (two consecutive less than signs).

Example:
#include <iostream>  //header file for cout

using namespace std;

int main()
   {
          char string[]="TechGeekBuzz";                 
          cout<<string << " :-This is a statement from cout";
   }
Output:
TechGeekBuzz :-This is a statement from cout
Behind the code:

In this example first, we include the header file #include<iostream> and using this header file we were able to use the cout<< statement.

cin (Standard input Stream):

To get input from the user we use the cin object. The cin statement works along with the extraction operator >> (two consecutive greater than symbol), and it helps to get data from the user with the help of keyboard.

The cin statement accept the input from a user and store it into variables.

Example:
#include <iostream>  //header file for cout and cin
using namespace std;

int main()
    {
          int age;
          cout<<"Enter your age: ";

          cin>> age;            // the cin object store teh user input in age variable
          cout<<"The age you entered is:- "<<age;
     }
Output:
Enter your age: 20
The age you entered is:- 20
Behind the code:

In this example the cin>>age; statement is used to accept the input from the user and store the data into age variable.

cerr(Un-Buffered standard error stream)

The cerr statement is similar to  cout  but it is used o print the error on the screen. It is also an object of iostream header file and it is un-buffered which mean it is used when one needs to display an error message immediately, and do not need buffer(temporary storage) to store the message.

 Example:
#include <iostream>  //header file for cerr
using namespace std;

int main()

       {                             
          cerr<<"An Error has been Found";
       }
Output
An Error has been Found

clog(buffered standard error stream)

It is an instance of iostream header file, and it is also used to display error messages. Unlike cerr, the clog is buffered which mean that insertion to the clog could cause its output to be stored in the buffer until the buffer is filled our flushed.

Example:
#include <iostream>  //header file for clog
using namespace std;

int main()
   {
      int a =10;            
      clog<<"An Error has been Found";
   }
Output
An Error has been Found
Behind the Code:

With these small examples, we can not show the difference between cout, clog and cerr statements, but in big programs, the difference could be seen easily.

Quick Summary

  • The input/output operations are used to interact with the user.
  • There are three header files used for I/O operations <iostream>, <iomanip> and <fstream>
  • cout and cin are the most common statements we use in C++.
  • Both cout and cin are the objects of <iostream>
  • cout<< is used to display output.
  • cin>> is used to take input from the user.
  • clog and cerr are used for buffered and unbuffered error messages

People are also reading:

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.