Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 5 of 10
Input & Output - cin, cout & Formatting
`std::cout`, `std::cin`, `std::cerr`, and `std::clog` are the four standard streams. C++ I/O uses **stream operators** (`<<` and `>>`). Unlike C's `printf`, the C++ stream I/O is **type-safe** - the correct overload is automatically selected. `<iomanip>` provides formatting manipulators for width, precision, fill, hex, etc.
cin, cout, cerr & Formatting Manipulators
#include <iostream>
#include <iomanip> // setw, setprecision, fixed, hex
#include <string>
#include <limits> // numeric_limits for input clearing
int main() {
// ── Basic output ─────────────────────────────────────────
std::cout << "Hello, World!
"; // newline via
(faster)
std::cout << "Value: " << 42 << "
"; // chaining
std::cout << std::endl; // flush + newline (slower)
// ── std::cerr - unbuffered, for errors ───────────────────
std::cerr << "Error: something went wrong
";
// ── Formatting with <iomanip> ────────────────────────────
double pi = 3.14159265358979;
std::cout << std::fixed << std::setprecision(4) << pi << "
"; // 3.1416
std::cout << std::scientific << pi << "
"; // 3.1416e+00
std::cout << std::defaultfloat; // restore
// Width and fill
std::cout << std::setw(10) << std::right << 42 << "
"; // " 42"
std::cout << std::setw(10) << std::left << "hi" << "
"; // "hi "
std::cout << std::setfill('0') << std::setw(5) << 7 << "
"; // "00007"
std::cout << std::setfill(' '); // reset fill
// Hex, octal, binary
int n = 255;
std::cout << std::hex << n << "
"; // ff
std::cout << std::oct << n << "
"; // 377
std::cout << std::dec << n << "
"; // 255 (restore)
// Boolean alpha
std::cout << std::boolalpha << true << " " << false << "
"; // true false
// ── Basic input ───────────────────────────────────────────
std::string name;
int age;
std::cout << "Enter name: ";
std::cin >> name; // reads until whitespace (no spaces captured)
std::cout << "Enter age: ";
std::cin >> age;
// ── Safe input with validation ────────────────────────────
int value;
std::cout << "Enter a number: ";
while (!(std::cin >> value)) {
// cin failed (e.g., user typed 'abc')
std::cin.clear(); // reset error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
'); // discard bad input
std::cout << "Invalid. Try again: ";
}
std::cout << "Got: " << value << "
";
// ── Reading full lines ────────────────────────────────────
std::cin.ignore(); // discard newline left in buffer from previous >>
std::string line;
std::cout << "Enter a sentence: ";
std::getline(std::cin, line);
std::cout << "You said: " << line << "
";
return 0;
}Common Mistakes
- `std::cin >> x` leaves the newline in the buffer - after reading with `>>`, the newline remains. A subsequent `std::getline()` reads an empty string. Fix: `std::cin.ignore()` or `std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')` before `getline`.
- `std::cin >> name` doesn't read spaces - `cin >> str` reads until whitespace. For a name with spaces ('Alice Smith'), use `std::getline(std::cin, name)` instead.
- `std::endl` flushes the buffer - `std::endl` is slower than `'\n'` because it flushes the output buffer. In tight loops, prefer `'\n'`. Use `std::endl` only when you explicitly need immediate flush.
Tip
Tip
Practice Input Output cin cout Formatting in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Type-safe, buffered stream operations: std::cin, std::cout, std::cerr, std::clog
Practice Task
Note
Practice Task - (1) Write a working example of Input Output cin cout Formatting from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Input Output cin cout Formatting is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.
Key Takeaways
- `std::cout`, `std::cin`, `std::cerr`, and `std::clog` are the four standard streams.
- `std::cin >> x` leaves the newline in the buffer - after reading with `>>`, the newline remains. A subsequent `std::getline()` reads an empty string. Fix: `std::cin.ignore()` or `std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')` before `getline`.
- `std::cin >> name` doesn't read spaces - `cin >> str` reads until whitespace. For a name with spaces ('Alice Smith'), use `std::getline(std::cin, name)` instead.
- `std::endl` flushes the buffer - `std::endl` is slower than `'\n'` because it flushes the output buffer. In tight loops, prefer `'\n'`. Use `std::endl` only when you explicitly need immediate flush.
Finished this lesson?