Output with cout, Input with cin in C++
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.
Print with <<
std::cout << x << " text " << y; chains any mix of values β types are handled automatically, no %d-style codes needed.
Read with >>
std::cin >> age; waits for input and parses it into age's type. Chained extractions fill variables left to right.
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).
endl vs newline
std::endl writes a newline AND flushes the buffer; '\n' just writes the newline. Prefer '\n' in performance-sensitive loops.
#include <iostream>Β int main() { int age = 0; std::cout << "Age? "; std::cin >> age; std::cout << "In 5 years: " << (age + 5) << std::endl; return 0;}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself β building beats watching.