Basic input/output in C++

In this tutorial, we will learn about the basic input/output in C++. There are many inbuilt libraries to perform input and output operations in C++. In the input operation, we read the data from the input device. And in output operation, we display the data/information onto the screen or through other output devices. If you are enthusiastic to learn about how to perform input and output operation in C++ you are in the right place.

C++ streams

In C++ we can do the input and output operations through a sequence of bytes called streams.

  • Input stream – When the data flows from the input device to the main memory then it is called input.
  • Output stream – When the data flows from the main memory to the output device then it is called output.

Header files for basic C++ input/output operations

Some header files are used to perform input/output operations in C++. The header files are as follows –

  • iostream – This is a standard input/output streams library. It is an object-oriented library that provides basic I/O operations in C++. The objects of iostream are –
    cout – Standard output stream object
    cin – Standard input stream object
    cerr – Standard output stream object for error display
    clog – Standard output stream object for logging
  • fstream – This header file provides functions for files. We can read/write in a file by using the functions provided by this header file.
    The ofstream object provides functions to write data to a file.
    The ifstream object provides functions to read data from a file.
  • sstream – This header file provides a string object and method to read a string just like the cin object. This is very useful in parsing input. For input and output to a string stringstream, istringstream, ostringstream objects are useful.

Basic C++ input/output operations

Now, we will learn about performing basic I/O operation in C++ using cout, cin, cerr, and clog objects. So let’s understand the use of these objects through small programs in C++.

Standard output stream object – cout

The cout is an object of an ostream class. We can use this object to display output onto the screen. To use this object you need to include the iostream header file in your program. We can use it to display any data types such as int, char, float, double, string.
In cout, ‘c’ means character and ‘out’ means output. We need to use an insertion operator(<<) with the cout object to display a stream of characters. Let’s see a simple C++ program illustrating the use of cout object.

#include<iostream>
using namespace std;
int main()
{
  int a=10;
  cout<<"\nTHE INTEGER IS : ";
  cout<<a;

  float b=5.5;
  cout<<"\nTHE FLOATING POINT NUMBER IS : ";
  cout<<b;

  char str[]="BASIC CPP";
  cout<<"\nWE ARE LEARNING "<<str;
  return 0;
}

So, this program uses the cout object to display an integer, real number, and a string. The output of this program is –

[email protected]:~/basic$ g++ basic1.cpp
[email protected]:~/basic$ ./a.out

THE INTEGER IS : 10
THE FLOATING POINT NUMBER IS : 5.5
WE ARE LEARNING BASIC CPP
[email protected]:~/basic$

So, in the output, we see the stream of characters which we displayed using the cout object.

Standard input stream object – cin

The cin is an object of istream class. We can use the cin object to take input from the user through input devices like a keyboard. To use it you must include the iostream header file in your program. In cin, ‘c’ means character and ‘in’ means input. It is used along with the extraction operator(>>) to store the input data from the user via input devices into the main memory of the system. Let’s see how a cin statement works through a simple C++ program.

#include<iostream>
using namespace std;
int main()
{
  int a;
  cout<<"\nENTER A INTEGER NUMBER : ";
  cin>>a;

  float b;
  cout<<"ENTER A REAL NUMBER : ";
  cin>>b;

  cout<<"\n\nTHE INTEGER NUMBER IS "<<a;
  cout<<"\nTHE REAL NUMBER IS "<<b;
  return 0;
}

So, this is the program code that illustrates the cin object. It takes an integer and real number as input. It stores it in main memory at some location referred to as ‘a’ and ‘b’ in the program. Then it displays the data using the cout object. The output of this code is –

[email protected]:~/basic$ g++ basic2.cpp
[email protected]:~/basic$ ./a.out

ENTER A INTEGER NUMBER : 5
ENTER A REAL NUMBER : 7.42

THE INTEGER NUMBER IS 5
THE REAL NUMBER IS 7.42
[email protected]:~/basic$

So, this is the output of the program. We take input using cin and display it using the cout object successfully.

Unbuffered standard error stream object – cerr

The cerr object represents a standard error stream. It is an object of ostream class and we can use it to display data onto the screen. To use the cerr object use the iostream header file. It provides an unbuffered output. The insertion operator(<<) is used along with the cerr object. Let’s see a simple program that illustrates the use of the cerr object.

#include<iostream>
using namespace std;
int main()
{
  int divisor,dividend,res;
  cout<<"ENTER DIVISOR : ";
  cin>>divisor;
  cout<<"ENTER DIVIDEND : ";
  cin>>dividend;
  if(divisor==0)
    cerr<<"ERROR : DIVISOR CANNOT BE ZERO";
  else
  {
    res=dividend/divisor;
    cout<<"QUOTIENT IS : "<<res;
  }
  return 0;
}

So, this is a simple program to calculate the division of two numbers. As we know the divisor must not be zero. So, if the user enters zero it produces an error message. The output of this code is –

[email protected]:~/basic$ g++ basic3.cpp
[email protected]:~/basic$ ./a.out
ENTER DIVISOR : 0
ENTER DIVIDEND : 10
ERROR : DIVISOR CANNOT BE ZERO
[email protected]:~/basic$ 

The output displays an error message that the divisor cannot be zero. The objects cout and cerr seem to be similar. But there is a huge difference between the two. Because cout is an object of stdout stream whereas cerr is an object of stderr stream.

Buffered standard error stream object – clog

A clog object is an object of an ostream class. This object provides a buffered output which means it automatically does not associate with cout. Buffered output is much more efficient than unbuffered output. In the case of a buffered output, the data is stored in a variable and finally stored in memory. To use a clog object you have to include the iostream header file and use the insertion operator(<<).

#include<iostream>
using namespace std;
int main()
{
  int pnum;
  char error[]="ENTERED NEGATIVE NUMBER";
  cout<<"ENTER A POSITIVE NUMBER : ";
  cin>>pnum;
  if(pnum<0)
    clog<<"ERROR : "<<error<<pnum;
  else
    cout<<"ENTERED POSITIVE NUMBER "<<pnum;
  return 0;
}

The program takes an integer and checks whether the user entered a positive number. The output of this code is –

[email protected]:~/basic$ g++ basic4.cpp
[email protected]:~/basic$ ./a.out
ENTER A POSITIVE NUMBER : -2
ERROR : ENTERED NEGATIVE NUMBER-2
[email protected]:~/basic$ 

So, when a user enters a negative number we display an error message. A clog object is an object of stderr stream and you can use it for logging purposes.

Thanks for reading this article and I hope it helped you.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *