01 / 21

Iostreams for
C++ I/O

Learn how to read input and write output in C++ using std::cin and std::cout: the standard iostream library.

C++

What Is an Iostream?

An iostream (input/output stream) is a C++ abstraction that lets your program communicate with the outside world: the keyboard, the screen, or files. Think of it as a flowing channel of data between your code and a device.

Keyboard Your Program Screen std::cin std::cout

Setting Up: <iostream> and std

Every C++ program that does I/O needs two things at the top of the file: an include directive and a way to reference the standard namespace.

#include <iostream>    // brings in cin, cout, endl

int main() {
    std::cout << "Hello, world!";
    return 0;
}

Output with std::cout

std::cout sends data to the standard output (usually your terminal screen). You use the insertion operator << to push values into the stream.

<< Operator
"Insert into the stream." Chain multiple values in a single statement.
std::endl
Inserts a newline character and flushes the output buffer.
"\n"
Inserts a newline without flushing: usually faster in loops.
std::cout << "Age: " << 17 << std::endl;
// Output:  Age: 17

Input with std::cin

std::cin reads data from standard input (the keyboard). You use the extraction operator >> to pull values out of the stream and store them in variables.

int age;
std::cout << "Enter your age: ";
std::cin  >> age;         // waits for user input
std::cout << "You are " << age << " years old.\n";

The Stream Model

Streams act like a conveyor belt: data flows in one direction. You can chain multiple operations because << and >> return a reference to the stream itself.

"Hello" 42 3.14 '\n' Stream Buffer Data flows left to right, type-agnostic

<< vs >>: The Two Operators

<< (Insertion) >> (Extraction)
Used with std::cout std::cin
Direction Program → Screen Keyboard → Program
Mnemonic Arrows point toward cout Arrows point toward variable
Whitespace Outputs as-is Skips leading whitespace
Chainable? Yes Yes

cin >> vs getline

std::cin >> var
Reads a single token (word or number). Stops at whitespace. Best for reading individual values like an int or a single word.
std::getline(cin, str)
Reads an entire line up to the newline character. Best for names, sentences, or any input with spaces.
std::string word, line;
std::cin >> word;               // reads "Hello" from "Hello World"
std::getline(std::cin, line);  // reads " World" (rest of line)

Writing an I/O Program: Step by Step

1
Include the header: write #include <iostream> at the top of your file.
2
Declare variables for the data you want to store: choose the correct type (int, double, string).
3
Prompt the user with std::cout << so they know what to type.
4
Read input with std::cin >> (single token) or std::getline (full line).
5
Process and output results with std::cout <<, chaining values as needed.

Example 1: Simple Greeting

Read the user's name and greet them. This uses std::string with >> to capture a single word.

#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "What is your name? ";
    std::cin  >> name;
    std::cout << "Hello, " << name << "!\n";
    return 0;
}

Example 2: Multiple Inputs

Read two numbers, compute their sum. Demonstrates chaining the extraction operator and mixing types.

#include <iostream>

int main() {
    double a, b;
    std::cout << "Enter two numbers: ";
    std::cin  >> a >> b;    // reads both in one line
    std::cout << "Sum = " << (a + b) << std::endl;
    return 0;
}

Example 3: Full-Line Input

Read a full sentence (with spaces) and count its length. This requires std::getline instead of >>.

#include <iostream>
#include <string>

int main() {
    std::string sentence;
    std::cout << "Type a sentence: ";
    std::getline(std::cin, sentence);
    std::cout << "Length: " << sentence.size() << "\n";
    return 0;
}

Mixing Input: cin.ignore()

When you use >> followed by getline, a leftover newline in the buffer causes getline to read an empty string. The fix: call cin.ignore() between them.

int age;
std::string fullName;

std::cout << "Age: ";
std::cin  >> age;

std::cin.ignore();  // discard the leftover '\n'

std::cout << "Full name: ";
std::getline(std::cin, fullName);

Type Safety and Input Errors

If the user types text when cin expects a number, the stream enters a fail state. All subsequent reads will silently fail until you clear the error.

int x;
std::cin >> x;  // user types "abc"

if (std::cin.fail()) {
    std::cout << "Invalid input!\n";
    std::cin.clear();              // reset the fail flag
    std::cin.ignore(1000, '\n');  // discard bad input
}

Common Mistakes

✕ Wrong Operator Direction
Writing cin << x or cout >> x. Remember: arrows point toward the destination.
✕ Forgetting #include
Omitting <iostream> causes "undeclared identifier" errors for cout and cin.
✕ Missing std:: Prefix
Writing bare cout without std:: or using namespace std; won't compile.
✕ Ignoring the Newline Bug
Mixing >> then getline without cin.ignore() silently reads an empty string.

Summary Cheat Sheet

Task Code Notes
Print text std::cout << "hi"; Use << to chain
Newline "\n" or std::endl endl flushes buffer
Read a word/number std::cin >> x; Skips whitespace
Read a full line std::getline(cin, s); Reads until '\n'
Clear bad input cin.clear(); cin.ignore(); Always pair both

Question 1 of 5

Which header must you include to use std::cout and std::cin?

Question 2 of 5

What does the following code print?

std::cout << "A" << "B" << "C";

Question 3 of 5

If the user types "Hello World" and your code uses std::cin >> word;, what value does word contain?

Question 4 of 5

You read an int with cin >> n; and then call getline(cin, s); immediately after. What happens?

Question 5 of 5

Which line correctly reads two integers x and y from standard input using iostream?