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.
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.
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.
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.
<< 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::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
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
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?