C++ Basic Input/Output

In this lesson, you will learn about basic input and output operations in C++, their usage, and some examples to better understand the topic.

C++ input and output operations are based on stream concepts. The stream is a sequence of bytes to flow the data. If the data that is in bytes form flow from main memory to device, then it is called output operation. If the bytes flow from devices to main memory, then it is called input operation. These devices can be a display screen, printer, network connection, and many others.

Input/Output library header files

Following are the header files that are available for input and output operations;

iostream: standard input and output stream. This header contains the definition of the objects cin, cout, and cerr.
iomanip: for input and output manipulations. The methods included in this header file are used for manipulating streams. These files elaborate on the definitions of setw, setprecision, etc.
fstream: This header file is used to define the file stream. This header file is used to handle the data that is being read from the files as input or the data that is being written to the files as the output.

More like this:

C++ output

The cout is an object in the ostream class. The cout is connected with the standard output stream that is displayed on the user output. The cout is used with the stream insertion operator to display the output on the devices.

Example of output in C++

int main() {
  std::cout << "Welcome to C++ Class!\n";
}

C++ input

The cin is the object of the istream class that is defined in the header file. It is connected with the standard input devices such as a keyboard, mouse, etc. The cin is used with the combination of the stream extraction operator >> to read the input from the console window.

Example of input in C++

#include <iostream>
int main()
{
    int number;

    cout << "Please enter a number:";
    cin >> number;
    cout << "\nYou entered: " << number;

    return 0;
}

 Standard Error stream (cerr)

The object cerr is the instance of the ostream class. This object is used to attach the standard error device that is also displayed on the output but the object cerr is un-buffered and stream insertion to cerr is used to display the output immediately.

Example of cerr in C++

// Program to find a division
int main() {

  int a, b;

  cout << "Please enter the first number:  ";
  cin >> a;

  cout << "Please enter the second number: ";
  cin >> b;

  if (b != 0) {
    int division = a / b;
    return division;

  } else { // if b = 0
    cerr << "Cannot divide by zero\n\n";
  }

}

Standard log stream (clog)

The standard log stream object clog is also an instance of the ostream class. This object is attached to the standard error device that is also displayed on the screen but the clog object is buffered. This means that the output will be held in the buffer until the buffer is filled or flushed.

Example of clog

#include <iostream>

using namespace std;

int main() {

  clog << "An exception has been thrown." << endl;

  return 0;
}

Summary

Note: The examples are given to show the only basic syntax of how we can use these objects in programs and applications.

  • iostream, iomanip, and fstream are three header files used for performing input and output operations in C++.
  • cin is used with the stream insertion operator <<
  • cout is used with the stream extraction operator >>
  • endl is used to display the output on the next line.
  • cerr is used to display the error immediately.
  • clog also display an error message but its first store in the buffer and display when a buffer is full.

 

Back to: Learn C++ Programming > Introduction to C++

Leave a Reply

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