Last Updated: January 3, 2026
In C++, we primarily use streams to handle input and output. A stream is essentially a sequence of data elements made available over time. There are two main types of streams we'll work with:
The C++ standard library provides a set of objects for stream handling, with the most common being cin for input and cout for output.
Here’s a simple example that shows how to read input from the user and print it out:
In this code, cout is used to display a prompt for the user, and cin reads the input. The endl manipulator is used to insert a newline and flush the output buffer.
When we want our output to be more user-friendly, we often need to format it. C++ provides several ways to format output, including manipulators and the iomanip library.
Manipulators are convenient functions that alter the way data is presented. Here are a few commonly used manipulators:
std::setw(n): Sets the width of the next output field to n.std::setprecision(n): Sets the number of digits to display after the decimal point.std::fixed: Forces the output to use fixed-point notation.Let’s look at an example that demonstrates these manipulators:
In this code, setprecision(2) ensures that pi is displayed with two decimal places, and setw(10) makes the output occupy a field of 10 characters, aligning it to the right.
While reading input, it's essential to ensure the data type matches what you're expecting. C++ handles various data types, such as integers, floating-point numbers, and strings.
However, there are some nuances when dealing with different types:
cin will read input until it encounters whitespace. If you want to read an entire line, you should use std::getline.Here’s an example that handles both integers and strings:
This code snippet illustrates how to read a full line for the name and an integer for the age. Using getline allows for spaces in the input, which is common in names.
Input handling isn't just about reading data; it’s also about ensuring the data is valid. C++ provides mechanisms to check for errors during input operations.
For instance, if the input fails (e.g., the user types a letter when an integer is expected), the cin stream enters a "fail" state. We can check for this and take appropriate actions.
Here’s how you can handle input errors:
In this example, if the user enters invalid input, a message prompts them to enter a valid number. The cin.clear() function clears the error state, and cin.ignore() skips to the next line, allowing for new input.
In addition to standard input and output, C++ provides the ability to read from and write to files. This is essential for persistent data storage and retrieval.
To work with files, we use the <fstream> library, which provides ifstream for input file streams and ofstream for output file streams.
Here’s a simple example demonstrating file output:
In this code, we create an output file stream to write to output.txt. Always remember to check if the file opened successfully, and don't forget to close the file when you're done.
For reading from a file, the process is similar. Here’s how you can read from the file created above:
This snippet reads from the output.txt file line by line and prints each line to the console. This is a common approach for processing text files.