Skip to content
C++ Better Explained
Go back
C++ cin.ignore(): Fix getline Skipping After cin >>

C++ cin.ignore(): Fixing the getline Skip

Almost every C++ beginner hits this baffling bug: you read a number with cin, then call getline to read a name — and the program blows right past the name prompt without letting you type. Nothing is broken. You’ve just met the input buffer, and cin.ignore() is the fix.


The Bug in Action

Here’s the classic setup that fails:

#include <iostream>
#include <string>

int main() {
    int age;
    std::string name;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "Enter your name: ";
    std::getline(std::cin, name);   // gets skipped!

    std::cout << "Hi " << name << ", age " << age << "\n";
    return 0;
}

Run it, type 25, press Enter — and the program prints Hi , age 25 without ever pausing for the name. The name came out empty. So what happened?


Why It Happens: The Leftover Newline

When you type 25 and press Enter, the input buffer actually holds 25\n — the number and the newline from your Enter key. The statement std::cin >> age reads the 25 but stops at the newline, leaving that \n sitting in the buffer.

Now std::getline runs. Its job is to read up to the next newline. It looks at the buffer, immediately finds that leftover \n, and says “done — that’s an empty line.” It never waits for you to type, because as far as it’s concerned, a line already ended.

The short version: cin >> leaves a newline behind, and getline chokes on it.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

The Fix: cin.ignore()

The solution is to throw away that leftover newline before calling getline. That’s exactly what std::cin.ignore() does. The robust form ignores everything up to and including the next newline:

#include <iostream>
#include <string>
#include <limits>   // for std::numeric_limits

int main() {
    int age;
    std::string name;

    std::cout << "Enter your age: ";
    std::cin >> age;

    // Clear the leftover newline from the buffer:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::cout << "Enter your name: ";
    std::getline(std::cin, name);   // now it waits for input

    std::cout << "Hi " << name << ", age " << age << "\n";
    return 0;
}

Now the program pauses for the name as expected. The call std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') means “discard characters until you’ve thrown away a newline, or up to a huge maximum count.” It wipes the buffer clean so getline starts fresh.


What the Two Arguments Mean

cin.ignore() takes two arguments: how many characters to discard at most, and a delimiter to stop at. Passing std::numeric_limits<std::streamsize>::max() as the count is a way of saying “as many as needed,” and '\n' tells it to stop after eating the newline. You’ll sometimes see beginners write std::cin.ignore() with no arguments, which discards just one character — that works for a single leftover newline but is less reliable, so the full form is the safer habit.


When You Do and Don’t Need It

You only need cin.ignore() when a formatted read (cin >>) is followed by a getline. Here’s the quick guide:

SequenceNeed cin.ignore()?
cin >> then getlineYes — clear the newline
getline then getlineNo — getline consumes its own newline
cin >> then cin >>No — >> skips leading whitespace anyway
getline then cin >>No

The reason getline after getline is fine: getline reads and discards the newline that ends the line, so it never leaves one behind. Only cin >> leaves the newline stranded.


The Takeaway

Whenever you mix cin >> something with a later getline, insert std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') in between. It’s not a magic incantation — it simply clears the newline that cin >> refused to consume, so your next line of input is read correctly.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. 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.


Next Post
GCD and LCM in C++: The Euclidean Algorithm Explained

Keep Learning