Beginner⏱️ 7 min📘 Topic 5 of 32

🖨️ Input and Output in C++ — cin, cout and getline Explained

Master C++ input and output with cin, cout, getline and stream manipulators. Learn how to read user input, handle whitespace and format output — with practical examples.

C++ uses streams for input and output. The two you'll use daily come from <iostream>:

  • std::cout — standard output (screen)
  • std::cin — standard input (keyboard)
  • std::cerr — standard error (also screen, separate stream)

📤 Output basics

std::cout << "Hello" << " " << 42 << '\n';

The << operator inserts a value into the stream. Chain as many as you want. '\n' ends the line.

📥 Input basics

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

The >> operator extracts a value from the stream. It skips whitespace by default.

⚠️ Reading strings — the cin trap

std::cin >> name stops at the first whitespace, so it can't read "John Doe" as a single value. Use std::getline:

std::string fullName;
std::getline(std::cin, fullName);

💡 endl vs \n

Both end the line. endl additionally flushes the stream — slower. Prefer '\n' unless you need an immediate flush.

💻 Code Examples

Ask the user for their name and age

#include <iostream>
#include <string>
int main() {
  std::string name;
  int age;
  std::cout << "Name: ";
  std::getline(std::cin, name);
  std::cout << "Age:  ";
  std::cin >> age;
  std::cout << "Hi " << name << ", you are " << age;
}
Output:
Name: John Doe
Age:  29
Hi John Doe, you are 29

Mixing >> and getline — the pitfall

int age;
std::string line;
std::cin >> age;        // reads 25\n — leaves the \n in stream
std::getline(std::cin, line); // reads the empty line!

// Fix: consume leftover newline
std::cin.ignore();
Output:
Use std::cin.ignore() between >> and getline.

⚠️ Common Mistakes

  • Using `cin >> name` and being confused why only the first word is read.
  • Forgetting `std::cin.ignore()` between `>>` and `getline` — getline reads an empty line.
  • Using `endl` in tight loops — flushes are expensive, kills performance.
  • Not checking if cin succeeded — `if (std::cin.fail()) { ... }` to detect bad input.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What does `<<` mean in `std::cout << x`?Beginner
It's the stream insertion operator. It sends x to the output stream. Chaining works because each operator returns the stream, so the next call has somewhere to write.
Q2.Why does cin skip whitespace?Beginner
The default formatted extractor `>>` consumes leading whitespace and stops at the next whitespace — convenient for reading separate tokens. Use getline to read a full line including spaces.
Q3.When should you use `endl` instead of `'\n'`?Intermediate
Only when you need to FLUSH the output immediately — e.g., before a long pause or when debugging. In normal output, `'\n'` is faster because it doesn't flush.
Q4.How do you read multiple values on one line?Intermediate
Chain extractors: `std::cin >> a >> b >> c;` — each >> skips whitespace and reads the next token.
Q5.What's `std::cin.tie(nullptr)` used for?Advanced
It unties cin from cout. By default, cin flushes cout before reading (so prompts appear before input). Untying is a competitive-programming speedup when you don't need that behavior.

🧠 Quick Summary

  • cout for output, cin for input — both via the `<<` / `>>` operators.
  • cin stops at whitespace; use getline for full lines.
  • Always `cin.ignore()` between `>>` and `getline`.
  • Prefer `'\n'` over `endl` unless you need a flush.
  • Check `cin.fail()` to detect bad/incompatible input.