Skip to content
C++ Better Explained
Go back
cin vs getline in C++: Why getline Gets Skipped

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.

Learning C++ properly? The C++ Better Explained Ebook covers input, strings and the STL in plain English — 87 pages, just $19.

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

SituationUse
Reading a single numbercin >>
Reading a single wordcin >>
Reading a full name or sentencegetline
Reading a whole line of anythinggetline
Mixing both in one programgetline 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

ProblemFix
getline skipped after cin >>cin.ignore(max, '\n') before it
cin >> name only reads one worduse getline(cin, name)
Mixed reads keep breakinguse getline everywhere + stringstream
Input loops forever on letterscin.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


📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Get the Size of a File in C++
Next Post
How to Remove a Character from a String in C++

Keep Learning