Easy Tutorial
❮ Cpp Examples Sum Natural Number Cpp Polymorphism ❯

Basic Input/Output in C++

The C++ Standard Library provides a rich set of input/output functionalities, which we will introduce in subsequent chapters. This chapter will discuss the most basic and common I/O operations in C++ programming.

C++ I/O occurs in streams, which are sequences of bytes. If the byte stream flows from a device (such as a keyboard, disk drive, network connection, etc.) to memory, it is called an input operation. If the byte stream flows from memory to a device (such as a display, printer, disk drive, network connection, etc.), it is called an output operation.

I/O Library Header Files

The following header files are important in C++ programming:

Header File Functions and Description
<iostream> This file defines the cin, cout, cerr, and clog objects, which correspond to the standard input stream, standard output stream, unbuffered standard error stream, and buffered standard error stream, respectively.
<iomanip> This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators (such as setw and setprecision).
<fstream> This file declares services for user-controlled file handling. We will discuss its details in the chapter on files and streams.

Standard Output Stream (cout)

The predefined object cout is an instance of the iostream class. The cout object is "connected" to the standard output device, which is usually the display. cout is used in conjunction with the stream insertion operator <<, as shown below:

Example

#include <iostream>

using namespace std;

int main()
{
   char str[] = "Hello C++";

   cout << "Value of str is: " << str << endl;
}

When the above code is compiled and executed, it produces the following result:

Value of str is: Hello C++

The C++ compiler selects the appropriate stream insertion operator to display the value based on the data type of the variable to be output. The << operator is overloaded to output data items of built-in types (integer, float, double, string, and pointer).

The stream insertion operator << can be used multiple times in a single statement, as shown in the example above. endl is used to add a newline at the end of the line.

Standard Input Stream (cin)

The predefined object cin is an instance of the iostream class. The cin object is attached to the standard input device, which is usually the keyboard. cin is used in conjunction with the stream extraction operator >>, as shown below:

Example

#include <iostream>

using namespace std;

int main()
{
   char name[50];

   cout << "Please enter your name: ";
   cin >> name;
   cout << "Your name is: " << name << endl;
}

When the above code is compiled and executed, it prompts the user to enter their name. Once the user inputs a value and presses Enter, the following result is displayed:

Please enter your name: cplusplus
Your name is: cplusplus

The C++ compiler selects the appropriate stream extraction operator to extract the value based on the data type of the input value and stores it in the given variable.

The stream extraction operator >> can be used multiple times in a single statement. If multiple data inputs are required, the following statement can be used:

cin >> name >> age;

This is equivalent to the following two statements:

cin >> name;
cin >> age;

Standard Error Stream (cerr)

The predefined object cerr is an instance of the iostream class. The cerr object is attached to the standard output device, which is usually the display, but the cerr object is unbuffered, meaning each stream insertion into cerr is output immediately.

cerr is used in conjunction with the stream insertion operator <<, as shown below:

Example

#include <iostream>

using namespace std;

int main()
{
   char str[] = "Unable to read....";

   cerr << "Error message: " << str << endl;
}

When the above code is compiled and executed, it produces the following result:

Error message: Unable to read....

Standard Log Stream (clog)

The predefined object clog is an instance of the iostream class. The clog object is attached to the standard output device, which is usually the display, but the clog object is buffered. This means each stream insertion into clog is stored in the buffer until the buffer is full or flushed.

clog is used in conjunction with the stream insertion operator <<, as shown below:

Example

#include <iostream>

using namespace std;

int main()
{
   char str[] = "Unable to read....";

   clog << "Error message: " << str << endl;
}

When the above code is compiled and executed, it produces the following result:

Error message: Unable to read....
char str[] = "Unable to read....";

clog << "Error message: " << str << endl;
}

When the above code is compiled and executed, it produces the following result:

Error message: Unable to read....

Through these small examples, we cannot distinguish the differences between cout, cerr, and clog. However, their differences become very apparent when writing and executing large programs. Therefore, good programming practices tell us to use the cerr stream to display error messages, while other log messages should be output using the clog stream.

❮ Cpp Examples Sum Natural Number Cpp Polymorphism ❯