In this lesson, you will learn how the C++ standard libraries offer fundamental and typical I/O operations needed for C++ programming, with examples to understand the topic better.
I/O in C++ happens in streams, which are collections of bytes. It is referred to as an input operation when data flows into the main memory from a keyboard, disc drive, network connection, etc., and as an output operation when data flows out of the main memory to a display screen, printer, disc drive, network connection, etc. The following header files are crucial for C++ programs:
<iostream>
: The standard input stream, standard output stream, unbuffered standard error stream, and buffered standard error stream are all represented by the objects defined in this file, cin
, cout
, cerr
, and clog
, respectively.<iomanip>
: With so-called parameterized stream manipulators like setw
and setprecision
, this file declares services that help conduct formatted I/O.<fstream>
: This file lists services for file processing that the user manages. You will cover in full the chapter on files and streams.cout
is a standard C++ function that transmits formatted output to the screen and other output devices. For output display, the cout object and the operator are used. OStream class is an instance of the predefined object cout. The standard output device, frequently the display screen, is referred to as “attached to” the cout
object. The following example demonstrates this.
// Program displays the double-quoted string #include <iostream> using namespace std; int main() { cout << "My favourite subject is C++" << endl; return 0; }
The main()
function is the first line of code in every C++ program in the above example. We use the using namespace std; statement to use the function of the std namespace. The std namespace contains the definition of the cout object. The iostream header file, which enables us to display output, is initially included.
The cout
prints the string enclosed in quotation marks, ” “. You can add a new line with the endl
manipulator. That’s why each output is presented in a separate line. The main()
function’s “exit status” is returned by the return 0; statement, which the operator follows.
using namespace std;
statement, we must substitute std::cout
for cout.Istream class instance cin
is a predefined object. The standard input device, the keyboard, has the cin object attached. The stream extraction operator, denoted by the symbols >>
, which are two greater-than signs, is used in conjunction with the cin. Let’s demonstrate this in the example below.
cin >> age; cin >> dob;
Aside from identifying the data type of the entered value, the C++ compiler also chooses the proper stream extraction operator to extract the value and store it in the specified variables. More than one statement may contain the stream extraction operator >>
.
// Program takes input #include <iostream> using namespace std; int main() { int age; cout << "Please enter age: "; cin >> age; cout << "Age: " << age << endl; return 0; }
After compiling and running the previous code, a name entry prompt will appear. To see the following outcome, input a value and press Enter.
Output
Please enter age: 1 Age: 1
An instance of the iostream class is the predefined object cerr
. Although the standard error device, which is also a display screen, is supposed to be attached to the cerr object, the object cerr
is un-buffered.
Therefore, each stream inserted into cerr
results in its output appearing instantly. As demonstrated in the following example, the cerr can also be used with the stream insertion operator.
Example
// Program displays error message #include <iostream> using namespace std; int main() { char error_msg[] = "Unable to connect"; cerr << "Error: " << error_msg << endl; return 0; }
The outcome of running the above code is as follows:
Output
Error: Unable to connect
This concludes the Syntax of the C++ Basic Input Output lesson. In the next lesson, you will learn about Implicit Conversion in C++.