Categories
c++ c++-faq input iostream istream

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

138

I have the following piece of code that prompts the user for their cat’s age and name:

#include <iostream>
#include <string>

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

    std::cin >> age;
    std::getline(std::cin, name);
    
    if (std::cin)
    {
        std::cout << "My cat is " << age << " years old and their name is " << name << std::endl;
    }
}

What I find is that the age has been successfully read, but not the name. Here is the input and output:

Input:

"10"
"Mr. Whiskers"

Output:

"My cat is 10 years old and their name is "

Why has the name been omitted from the output? I’ve given the proper input, but the code somehow ignores it. Why does this happen?

1

  • 1

    I believe std::cin >> name && std::cin >> std::skipws && std::getline(std::cin, state) should also work as expected. (In addition to the answers below).

    – jww

    Nov 11, 2018 at 17:30


163

Why does this happen?

This has little to do with the input you provided yourself but rather with the default behavior std::getline() has. When you provided your input for the age (std::cin >> age), you not only submitted the following characters, but also an implicit newline was appended to the stream when you typed Enter:

"10\n"

A newline is always appended to your input when you select Enter or Return when submitting from a terminal. It is also used in files for moving toward the next line. The newline is left in the buffer after the extraction into age until the next I/O operation where it is either discarded or read. When the flow of control reaches std::getline(), it will see "\nMr. Whiskers" and the newline at the beginning will be discarded, but the input operation will stop immediately. The reason this happens is because the job of std::getline() is to attempt to read characters and stop when it finds a newline. So the rest of your input is left in the buffer unread.

Solution

cin.ignore()

To fix this, one option is to skip over the newline before doing std::getline(). You can do this by calling std::cin.ignore() after the first input operation. It will discard the next character (the newline character) so that it is no longer in the way.

std::cin >> age;
std::cin.ignore();
std::getline(std::cin, name);

assert(std::cin); 
// Success!

std::ws

Another way to discard the whitespace is to use the std::ws function which is a manipulator designed to extract and discard leading whitespace from the beginning of an input stream:

std::cin >> age;
std::getline(std::cin >> std::ws, name);

assert(std::cin);
// Success!

The std::cin >> std::ws expression is executed before the std::getline() call (and after the std::cin >> age call) so that the newline character is removed.

The difference is that ignore() discards only 1 character (or N characters when given a parameter), and std::ws continues to ignore whitespace until it finds a non-whitespace character. So if you don’t know how much whitespace will precede the next token you should consider using this.

Match the operations

When you run into an issue like this it’s usually because you’re combining formatted input operations with unformatted input operations. A formatted input operation is when you take input and format it for a certain type. That’s what operator>>() is for. Unformatted input operations are anything other than that, like std::getline(), std::cin.read(), std::cin.get(), etc. Those functions don’t care about the format of the input and only process raw text.

If you stick to using a single type of formatting then you can avoid this annoying issue:

// Unformatted I/O
std::string age, name;
std::getline(std::cin, age);
std::getline(std::cin, name);

or

// Formatted I/O
int age;
std::string firstName, lastName;
std::cin >> age >> firstName >> lastName;

If you choose to read everything as strings using the unformatted operations you can convert them into the appropriate types afterwards.

9

  • 1

    Why not simply if (getline(std::cin, name) && getline(std::cin, state))?

    Aug 19, 2016 at 19:41

  • @FredLarson Good point. Though it wouldn’t work if the first extraction is of an integer or anything that isn’t a string.

    – David G

    Aug 19, 2016 at 20:30


  • 1

    Of course, that isn’t the case here and there’s no point in doing the same thing two different ways. For an integer you could get the line into a string and then use std::stoi(), but then it’s not so clear there’s an advantage. But I tend to prefer to just use std::getline() for line-oriented input and then deal with parsing the line in whatever way makes sense. I think it’s less error prone.

    Aug 19, 2016 at 20:35

  • @FredLarson Agreed. Maybe I’ll add that in if I have the time.

    – David G

    Aug 19, 2016 at 20:39

  • 1

    @Albin The reason you might want to use std::getline() is if you want to capture all characters up to a given delimiter and input it into a string, by default that is the newline. If those X number of strings are just single words/tokens then this job can be easily accomplished with >>. Otherwise you would input the first number into an integer with >>, call cin.ignore() on the next line, and then run a loop where you use getline().

    – David G

    Apr 3, 2020 at 18:54


13

Everything will be OK if you change your initial code in the following way:

if ((cin >> name).get() && std::getline(cin, state))

3

  • 3

    Thank you. This will also work because get() consumes the next character. There’s also (std::cin >> name).ignore() which I suggested earlier in my answer.

    – David G

    Mar 26, 2014 at 12:14

  • “..work because get()…” Yes, exactly. Sorry for giving the answer without details.

    – Boris

    Mar 26, 2014 at 13:14


  • 4

    Why not simply if (getline(std::cin, name) && getline(std::cin, state))?

    Aug 19, 2016 at 19:41

2

This happens because an implicit line feed also known as newline character \n is appended to all user input from a terminal as it’s telling the stream to start a new line. You can safely account for this by using std::getline when checking for multiple lines of user input. The default behavior of std::getline will read everything up to and including the newline character \n from the input stream object which is std::cin in this case.

#include <iostream>
#include <string>

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

    if (std::getline(std::cin, name) && std::getline(std::cin, state))
    {
        std::cout << "Your name is " << name << " and you live in " << state;
    }
    return 0;
}
Input:

"John"
"New Hampshire"

Output:

"Your name is John and you live in New Hampshire"