cin vs getline in C++: Why getline Gets Skipped
Short answer: cin >> leaves the newline sitting in the buffer. The next getline reads that newline, decides the line is over, and returns an empty string without waiting for you.
The fix is one line:
std::cin >> age;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, name);
The Bug
#include <iostream>
#include <string>
int main() {
int age;
std::string name;
std::cout << "Age: ";
std::cin >> age;
std::cout << "Full name: ";
std::getline(std::cin, name); // SKIPPED
std::cout << "Name: [" << name << "]\n"; // Name: []
}
The program never pauses for the name. It prints the prompt and runs straight past it.
What Is Actually Happening
When you type 25 and press Enter, the buffer holds:
2 5 \n
cin >> age reads the digits and stops at the newline — it does not consume it, because >> stops at whitespace and leaves it there. The buffer still holds:
\n
getline then reads from the buffer until it finds a newline. There is one waiting immediately, so it reads zero characters, stores an empty string, consumes the newline, and returns successfully. No error, no pause — just an empty name.
Fix 1: Ignore the Leftover Newline
#include <iostream>
#include <limits>
#include <string>
int main() {
int age;
std::string name;
std::cout << "Age: ";
std::cin >> age;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Full name: ";
std::getline(std::cin, name);
std::cout << age << " / " << name << '\n';
}
ignore(count, delim) throws away up to count characters, stopping after it sees delim. Using numeric_limits<streamsize>::max() means “as many as needed to reach the newline” — which also handles the case where the user typed 25 extra junk.
You will see cin.ignore() with no arguments in older code. That discards exactly one character, which works for a plain 25⏎ but fails the moment there is anything else on the line. Use the full form. More detail in cin.ignore and clearing the buffer.
Fix 2: Use getline for Everything
Often the cleaner answer is to stop mixing the two styles:
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string line;
std::cout << "Age: ";
std::getline(std::cin, line);
int age = std::stoi(line);
std::cout << "Full name: ";
std::string name;
std::getline(std::cin, name);
std::cout << age << " / " << name << '\n';
}
Every read consumes a full line including its newline, so nothing is ever left behind. Parse numbers afterwards with stoi or stringstream. This is the approach I would recommend in any program that reads more than one value — and it pairs naturally with proper input validation.
When to Use Each
| Situation | Use |
|---|---|
| Reading a single number | cin >> |
| Reading a single word | cin >> |
| Reading a full name or sentence | getline |
| Reading a whole line of anything | getline |
| Mixing both in one program | getline for everything, then parse |
The core distinction: >> stops at whitespace, getline stops at the newline. That is why cin >> name on “John Smith” gives you John and leaves Smith in the buffer to surprise you later.
Reading Multiple Values from One Line
#include <sstream>
std::string line;
std::getline(std::cin, line); // "12 34 56"
std::istringstream ss(line);
int a, b, c;
ss >> a >> b >> c;
Here >> is doing what it is good at — tokenising — but on a string you already own, so the console buffer is never involved.
Reading Until the User Stops
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) break; // blank line ends input
std::cout << "You said: " << line << '\n';
}
The loop also ends naturally at end of input, which is Ctrl+D on Linux and macOS or Ctrl+Z then Enter on Windows.
Quick Reference
| Problem | Fix |
|---|---|
getline skipped after cin >> | cin.ignore(max, '\n') before it |
cin >> name only reads one word | use getline(cin, name) |
| Mixed reads keep breaking | use getline everywhere + stringstream |
| Input loops forever on letters | cin.clear() then cin.ignore(...) |
Take Your C++ Further
If you want input handling and the rest of the fundamentals explained properly rather than patched together from forum answers, the C++ Better Explained Ebook covers it in plain English. Just $19.
👉 Get the C++ Better Explained Ebook — $19
Related Articles
- cin.ignore() and Clearing the Input Buffer — the mechanics in depth.
- C++ Input Validation: Handling Bad cin Input — stopping infinite loops on bad input.
- Using getline() for String Input — getline on its own.
- C++ cin and User Input — how extraction works.
- C++ stringstream Explained — parsing lines after reading them.
- How to Read Multiple Inputs on One Line in C++ — several values from one line