In C++, iostream is the standard library header for input and output. It gives you four ready-made streams: std::cin reads from the keyboard, std::cout writes to the screen, std::cerr writes error messages, and std::clog writes log messages. Include <iostream> to use them.
A stream is a flow of characters. You put data into an output stream with << and take data out of an input stream with >>. Streams also exist for files (<fstream>) and for text in memory (<sstream>), and they all work the same way.
Manipulators such as endl, hex, fixed, and setw() change how a stream formats data. Stream functions such as getline(), fail(), and clear() read lines and check for errors.
This page teaches what iostream is. The Iostream Functions index lists every guide in this series.
Always check your input.
After std::cin >> x, test if (std::cin) or cin.fail(). If the user types letters where a number is expected, the stream goes into a failed state and every later read does nothing until you call clear().
Syntax
C++ Syntax
The most common iostream statements:
C++
#include <iostream>
#include <string>
int n;
std::string word, line;
std::cout << "Hello" << '\n'; // output
std::cerr << "Something went wrong\n"; // error output
std::cin >> n; // read one number
std::cin >> word; // read one word
std::getline(std::cin, line); // read a whole line
if (!std::cin) { std::cin.clear(); } // recover from bad input
Reference
Which Input Tool Should I Use?
C++ gives you several ways to read input. Here is a quick guide:
Three short programs that show how iostream works.
Example 1 — Read Two Numbers with cin and Print the Sum with cout
A first program. The user types 12 7 and presses Enter.
C++
#include <iostream>
int main() {
int a = 0;
int b = 0;
std::cout << "Enter two numbers: ";
std::cin >> a >> b;
std::cout << "Sum: " << a + b << '\n';
return 0;
}
Output
Enter two numbers: 12 7
Sum: 19
How It Works
std::cout << "Enter two numbers: " sends the prompt text to the screen. No new line is added, so the cursor stays on the same line.
std::cin >> a >> b reads two whitespace-separated numbers. The typed 12 goes into a and 7 goes into b.
The program prints a + b, which is 19. The '\n' at the end moves to a new line.
Example 2 — Read a Number and Then a Full Line
After >> the Enter key stays in the buffer. ignore() removes it so getline() reads the next real line. The user types 25, then Mari Selvan.
C++
#include <iostream>
#include <limits>
#include <string>
int main() {
int age = 0;
std::string name;
std::cout << "Age: ";
std::cin >> age;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Name: ";
std::getline(std::cin, name);
std::cout << name << " is " << age << " years old\n";
return 0;
}
Output
Age: 25
Name: Mari Selvan
Mari Selvan is 25 years old
How It Works
std::cin >> age reads 25 but leaves the Enter key (a '\n') waiting in the input buffer.
ignore(max, '\n') throws away everything up to and including that new line. Without it, getline() would read an empty line at once.
std::getline(std::cin, name) now reads the whole line Mari Selvan, spaces included, which >> could never do.
Example 3 — Format a Small Table with setw, fixed, and setprecision
Manipulators change how output looks. This program prints names and prices in neat columns.
iostream is a standard header that gives you the input and output streams: std::cin for input, std::cout for normal output, std::cerr for error messages, and std::clog for log messages. A stream is a flow of characters between your program and a device such as the keyboard or screen.
cin >> reads one value and stops at the first space or new line. getline() reads a whole line, spaces included. Use >> for numbers and single words, and getline() for names and sentences.
A manipulator is a small helper you send into a stream with << or >>. Examples are endl (new line and flush), hex (print in base 16), fixed (fixed decimal places), and setw (field width). They change how the stream formats data.
Both print to the screen by default. cout is for normal output and is buffered. cerr is for error messages, is not buffered, and can be redirected separately from cout in a shell.
Open the C++ Iostream Functions index. It lists 60 guides, from good(), fail(), and getline() to endl, setw(), and the C++23 print functions, each with three examples.