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 >> xgetline(...)
ReadsUp to the next space/newlineThe entire line, spaces included
Skips leading whitespace?YesNo — takes the line as-is
Consumes the trailing Enter?No — leaves itYes — that's how it knows the line ended
Good forNumbers, single wordsNames, 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

Interview / homework angle

  1. "Why does getline after cin fail?" — the answer above, in one breath: >> leaves the newline; getline consumes it and returns an empty line.
  2. "How would you read 'first name last name'?" — two >> reads (if it's always exactly two words) or one getline (if the format can vary), then justify.
  3. "What does >> return?" — the stream itself, which is why while (cin >> x) works as a read-until-failure loop.

Practice in the playground

  1. Fix the first demo by adding cin.ignore(); between the two reads — confirm name=[Tom].
  2. Read two full lines (a book title and its author) with two getline calls — no ignore needed. Explain why.
  3. Break it again: remove the ignore and change the input to 20 99 on separate lines. What does name hold now?

Related: for vs while loops (the read-until-sentinel pattern uses these reads), common compiler errors, what is int main().