cin >> vs getline(): why your string input comes up empty
Short answer: cin >> reads one whitespace-separated chunk and leaves the Enter key sitting in the input buffer. getline reads a whole line — spaces included — and consumes the Enter. Mix the two without cleaning up, and getline grabs that leftover Enter and gives you an empty string.
This is the most common input bug in beginner C++. The demo below reproduces it exactly — read a number, then read a line of text. The input box sends 20 on one line and Tom on the next. Predict what prints before you run:
#include <iostream>
using namespace std;
int main() {
int age;
char name[20];
cin >> age; // reads 20 — but leaves "\n" in the buffer!
cin.getline(name, 20); // reads the leftover "\n" → empty string
cout << "age=" << age << " name=[" << name << "]" << endl;
return 0;
}
name is empty. cin >> age stopped at the Enter you typed after 20, and that Enter stayed in the buffer. The very next getline woke up, saw "end of line already", and returned an empty line. Your program never got a chance to read Tom.
The mental model: two different readers
cin >> x | getline(...) | |
|---|---|---|
| Reads | Up to the next space/newline | The entire line, spaces included |
| Skips leading whitespace? | Yes | No — takes the line as-is |
| Consumes the trailing Enter? | No — leaves it | Yes — that's how it knows the line ended |
| Good for | Numbers, single words | Names, sentences, anything with spaces |
See the word-splitting behavior on its own — >> treats a space as "next value, please":
#include <iostream>
using namespace std;
int main() {
char a[20], b[20];
cin >> a >> b; // "Tom Smith" splits at the space
cout << "a=[" << a << "] b=[" << b << "]" << endl;
return 0;
}
And getline keeping spaces intact — the thing >> can't do:
#include <iostream>
using namespace std;
int main() {
char line[30];
cin.getline(line, 30); // whole line, spaces and all
cout << "line=[" << line << "]" << endl;
return 0;
}
The fix: eat the leftover Enter
Between a >> read and a getline, discard the stray newline with cin.ignore():
int age;
char name[20];
cin >> age;
cin.ignore(); // discard the leftover "\n"
cin.getline(name, 20); // now reads the real line: "Tom"
On a desktop compiler, the more bulletproof form throws away everything left on the line, not just one character:
#include <limits>
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Note: our in-browser engine covers the teaching core, so these demos use character arrays (char name[20]) — on your desktop compiler the usual form is std::string name; getline(cin, name); and the buffer behavior is exactly the same.
Rules of thumb
- Reading only numbers/words with
>>? Fine —>>skips leading whitespace on every read. - Mixing
>>andgetline? Putcin.ignore()at every boundary. Every time. Even when it seems to work without it. - Reading full lines only? Use
getlinethroughout — no traps. - "My getline is skipped!" is now a bug you can diagnose in five seconds: something before it left an Enter in the buffer.
Interview / homework angle
- "Why does getline after cin fail?" — the answer above, in one breath:
>>leaves the newline;getlineconsumes it and returns an empty line. - "How would you read 'first name last name'?" — two
>>reads (if it's always exactly two words) or onegetline(if the format can vary), then justify. - "What does
>>return?" — the stream itself, which is whywhile (cin >> x)works as a read-until-failure loop.
Practice in the playground
- Fix the first demo by adding
cin.ignore();between the two reads — confirmname=[Tom]. - Read two full lines (a book title and its author) with two
getlinecalls — no ignore needed. Explain why. - Break it again: remove the ignore and change the input to
20 99on separate lines. What doesnamehold now?
Related: for vs while loops (the read-until-sentinel pattern uses these reads), common compiler errors, what is int main().