C++ iostream

Beginner
10 min read
Updated: Nov 2026
3 examples
iostream

Definition and Usage

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().

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

Which Input Tool Should I Use?

C++ gives you several ways to read input. Here is a quick guide:

ToolReadsStops atBest for
cin >> xOne valueFirst space or new lineNumbers and single words
getline()A whole lineThe new line (it is removed)Names, sentences, file lines
get()One characterAfter one characterSpaces and new lines included
read()A fixed number of bytesWhen the count is reachedBinary data
ignore()Nothing (skips input)Count reached or delimiter foundClearing leftover input

C++ Examples

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;
}

How It Works

  1. 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.
  2. std::cin >> a >> b reads two whitespace-separated numbers. The typed 12 goes into a and 7 goes into b.
  3. 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;
}

How It Works

  1. std::cin >> age reads 25 but leaves the Enter key (a '\n') waiting in the input buffer.
  2. ignore(max, '\n') throws away everything up to and including that new line. Without it, getline() would read an empty line at once.
  3. 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.

C++
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::left << std::setw(8) << "Item"
              << std::right << std::setw(8) << "Price" << '\n';

    std::cout << std::fixed << std::setprecision(2);
    std::cout << std::left << std::setw(8) << "Pen"
              << std::right << std::setw(8) << 1.5 << '\n';
    std::cout << std::left << std::setw(8) << "Book"
              << std::right << std::setw(8) << 12.0 << '\n';
    return 0;
}

How It Works

  1. setw(8) makes the next item at least 8 characters wide. left pads on the right and right pads on the left.
  2. fixed with setprecision(2) prints exactly two digits after the decimal point, so 1.5 becomes 1.50.
  3. setw only lasts for one item, so it is written again before each value. fixed and setprecision stay active until you change them.

Next: C++ Iostream Functions Index

After you know what iostream is, open the function index for a full tutorial on each member function and manipulator.

Start withWhy
getline()Read a full line of text
fail()Detect bad input
endlEnd a line and flush the output
setw()Print neat columns

Full list: C++ Iostream Functions index (/cpp/iostream/function).

Frequently Asked Questions

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.

Browse every iostream guide

Open the functions index for categorized guides with examples and FAQs.

Iostream Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful