01 / 21

std::cin &
Whitespace

Master how C++ reads input from the keyboard: how std::cin handles spaces, tabs, and newlines, and why it matters for your programs.

C++

What is std::cin?

std::cin is the standard input stream object in C++. It reads data that users type on the keyboard and stores it into variables using the extraction operator >>.

#include <iostream> int main() { int age; std::cout << "Enter age: "; std::cin >> age; // reads one integer std::cout << "You are " << age; }

The Input Buffer

When you type on the keyboard, characters go into an input buffer (a waiting area). When you press Enter, the buffer becomes available to your program.

USER TYPES: John Smith↵ J o h n SP S m i t h \n read by cin >> STOP left in buffer newline

What Counts as Whitespace?

In C++, whitespace refers to invisible characters that create spacing. The extraction operator >> treats all of them the same way: it skips leading whitespace and stops at the next whitespace.

Space
The spacebar character ' '. ASCII code 32. Most common delimiter.
Tab
The tab character '\t'. ASCII code 9. Often in formatted files.
Newline
The enter key '\n'. ASCII code 10. Marks end of a line.

How >> Actually Works

The extraction operator follows a two-step process every time it reads:

1
Skip leading whitespace: Discard any spaces, tabs, or newlines at the front of the buffer. Keep going until a non-whitespace character is found.
2
Read until the next whitespace: Consume characters and store them. Stop at the first whitespace character (but leave it in the buffer).

The Core Rule

std::cin >> variable reads ONE token (word or number), never a full line Skips leading whitespace spaces, tabs, newlines before data Stops at next whitespace leftover stays in the buffer

>> vs std::getline

std::cin >> std::getline()
Reads One token (word) Entire line
Delimiter Any whitespace Newline '\n' only
Leading spaces Skipped Preserved
Delimiter consumed? No (stays in buffer) Yes (discarded)
Use case Single words, numbers Full names, sentences

Chaining Multiple Reads

You can chain >> to read multiple values. Each extraction skips whitespace, reads a token, and leaves the rest for the next.

C++ Code
int a, b; std::cin >> a >> b; // Input: " 42 17\n" // a = 42, b = 17
BUFFER AFTER INPUT: SP SP 4 2 SP SP SP 1 7 \n → a = 42 → b = 17 remains

Step-by-Step Approach

Before writing any input code, follow this checklist to avoid whitespace bugs:

1
Identify the data type: Is it a single word, a number, or a full line with spaces?
2
Choose the right tool: Use >> for single tokens (words, numbers). Use std::getline for full lines.
3
Check for mixing: If you use >> before getline, call std::cin.ignore() in between to discard the leftover newline.
4
Test with messy input: Try extra spaces, tabs, and blank lines to verify your program handles them correctly.

Example 1: Reading a Single Word

C++ Code
std::string name; std::cout << "Enter name: "; std::cin >> name; std::cout << "Hello, " << name << "!\n";
User Input
Alice
Output
Hello, Alice!

Example 2: The Whitespace Trap

C++ Code
std::string name; std::cout << "Enter full name: "; std::cin >> name; std::cout << "Hello, " << name << "!\n";
User Input
Alice Johnson
Output (unexpected!)
Hello, Alice!

Example 3: The Fix with getline

C++ Code
std::string name; std::cout << "Enter full name: "; std::getline(std::cin, name); std::cout << "Hello, " << name << "!\n";
User Input
Alice Johnson
Output
Hello, Alice Johnson!

The Mixing Problem

The most common bug: using >> followed by getline. The leftover newline from >> gets consumed by getline instantly.

Buggy Code
int age; std::string city; std::cout << "Age: "; std::cin >> age; // reads 25, leaves "\n" std::cout << "City: "; std::getline(std::cin, city); // reads "\n" → empty! std::cout << city.size(); // prints 0
BUFFER AFTER cin >> age: 2 5 \n consumed by >> getline reads THIS → empty string!

The Fix: cin.ignore()

Fixed Code
int age; std::string city; std::cout << "Age: "; std::cin >> age; std::cin.ignore(); // discard the leftover '\n' std::cout << "City: "; std::getline(std::cin, city); std::cout << "You live in " << city;
Input / Output
Age: 25 City: New York You live in New York

Common Mistakes

✕ Using >> for Full Lines
Writing cin >> fullName when the user enters "John Smith". Only "John" gets stored.
✕ Forgetting cin.ignore()
Mixing >> then getline without clearing the leftover newline. The getline reads an empty string.
✕ Assuming Input is Clean
Not accounting for extra spaces or tabs in user input. Always test with messy whitespace patterns.
✕ Using getline for Numbers
Reading a number with getline gives you a string. You would need std::stoi() to convert it.

Summary Reference Table

Scenario Tool Example
Read one integer cin >> n "42" → 42
Read one word cin >> word "Hello" → "Hello"
Read full line getline(cin, str) "Hello World" → "Hello World"
Number then line cin >> n; cin.ignore(); getline(...); Must ignore '\n'
Two numbers cin >> a >> b "3 7" → a=3, b=7

Question 1 of 5

What does std::cin >> do when it encounters leading spaces in the input buffer?

Question 2 of 5

What is printed to the screen?

Question 3 of 5

You use cin >> age to read an integer, then immediately use getline(cin, city). The getline reads an empty string. What is the cause?

Question 4 of 5

Which function should you use to read an entire line of text that may contain spaces?

Question 5 of 5

What is the output?