Learn C++
#include "ioformatting/cout_cin.hpp"

Output with cout, Input with cin in C++

//concept

std::cout and std::cin are the standard console streams, chained with << (insertion, toward output) and >> (extraction, from input). Unlike printf/scanf there are no format specifiers to get wrong β€” the compiler picks the right conversion from each variable's type, which eliminates an entire family of crashes.

Chaining reads left to right (cin >> a >> b fills a then b), whitespace separates >> inputs (spaces, tabs, newlines all delimit), and std::endl ends the line and flushes. Flushing every line is convenient but slow in hot loops β€” there, '\n' plus one final flush wins.

//how it works
1

Print with <<

std::cout << x << " text " << y; chains any mix of values β€” types are handled automatically, no %d-style codes needed.

2

Read with >>

std::cin >> age; waits for input and parses it into age's type. Chained extractions fill variables left to right.

3

Whitespace delimits input

>> skips leading whitespace and stops at the next whitespace β€” perfect for numbers and single words, wrong for full sentences (use getline there).

4

endl vs newline

std::endl writes a newline AND flushes the buffer; '\n' just writes the newline. Prefer '\n' in performance-sensitive loops.

//example
#include <iostream>
Β 
int main() {
int age = 0;
std::cout << "Age? ";
std::cin >> age;
std::cout << "In 5 years: " << (age + 5) << std::endl;
return 0;
}
Output
Age? 30
In 5 years: 35
1The prompt prints without a newline, so the cursor waits on the same line for input.
2Typing 30 and pressing Enter feeds characters to cin, which parses them into the int age.
3The final chained output computes age + 5 inline and prints 35 on its own line.
//use cases
Every interactive console program's prompt-then-read loop
Competitive programming fast input (with sync_with_stdio(false) for volume)
Dumping debug values mid-run without touching a debugger
Chaining mixed-type output in one statement instead of format strings

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself β€” building beats watching.