Basic Input/Output in C++
Written by
C++ is a programming language, using it we can create all sorts of programs, even the ones that require user interaction as well. Such piece of codes that provide user interaction requires some input from the user and in turn displays a proper output as well.
In order to do so, the C++ programming languages need some of the standard input and output libraries.
C++ I/O occurs in streams that are nothing but sequences of bytes.
If bytes flow into the code from an input device like a keyboard, a disk drive, or a network connection, etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.
So, if the code wants to take an input from the user then the input operation will be called, and if the program wants to show some result to the user then the output operation will be called. For these inputs as well as output operations there are some standard header files that are defined in the C++.
These header files must be included in every C++ program wherein the coder wants to use the basic I/O operations. These header files are as follows:
<iostream>
This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively.
<iomanip>
This file declares services useful for performing formatted I/O with parameterized stream manipulators, such as setw and setprecision.
<fstream>
This file declares services for user-controlled file processing. We will discuss about it in detail later on.
Example:
#include <iostream>
int main()
{
int a, b, c = 0;
cout << "Enter two numbers: ";
cin >> a >> b;
c = a + b;
cout << "Sum of entered numbers is: " << c << endl;
return 0;
}
Output
Enter two numbers: 5 10
Sum of entered numbers is: 15
The above program uses cin and cout, which are the standard input and output objects of the stream class. We will learn more about them later on.