Why does std::getline() skip input after a formatted extraction?



When using std::getline after a formatted extraction (like std::cin >>), you might see an unexpected behavior, where std::getline is skipping the next input line. In this article, we will understand why this happens and how to handle it.

Getline skipping input after formatted extraction

The code below shows how getline() is skipping input after a formatted extraction:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    string city;
    
    cin << name;
    getline(cin, city); 

    cout << "Your name " << name << " and city " << city << endl;
    return 0; 
}

Output of above code will be:

Enter your name: John
Your name John and city

You can clearly see that the getline() function is not reading the city input, and it is skipping the line. Let's understand why this happens.

Why getline skips input?

Both getline and cin uses standard input stream. Now, when you use formatted extraction (like std::cin >> ), it reads the input until it encounters whitespace or newline. This means that if you enter a string (in this case, your name) and then pressed Enter key, the newline character ('\n') will get added to the input buffer of the standard input stream.

Next, when you call std::getline, it will also read input from the same standard input stream. However, since the newline character ('\n') is still in the input buffer from the previous input, the getline function will take that newline character as its input (which is an empty line). This creates an illusion that getline is skipping the input.

// Input buffer after formatted extraction
cin >> "John\n" (newline character is still there)

// getline takes '\n' as input
getline(cin, city);
// So, city will be empty

Preventing std::getline() from Skipping Input

To prevent this issue, you have to clear the input buffer before calling std::getline. You can do this by adding an extra std::cin.ignore() statement after the formatted extraction. This will ignore the newline character left in the input buffer.

Example Code

The code below is modified version of the previous code that prevents getline from skipping input:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    string city;
    
    cout << "Enter your name: ";
    cin >> name;
    
    // Clear the input buffer
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    cout << "Enter your city: ";
    getline(cin, city); 

    cout << "Your name " << name << " and city " << city << endl;
    return 0; 
}

Output of the modified code will be:

Enter your name: John
Enter your city: New York
Your name John and city New York
Note: The compiler inside articles may not support taking input from the user. So, we have disabled compiling code inside this article. You can copy the code and run it in our external compiler to see the expected output.
Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-06-06T18:58:39+05:30

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements