If
getline
fails then you've got some junk ending in a newline in the input buffer for the
istream
you're using it on. It might be as simple as you bashing return twice by accident when you run the code from the command line or your IDE doing something a bit strange when you invoke your program from inside it. An older (actually antique by now) version of Visual Studio had a habit of sticking a spurious return when invoking a program leading to the sort of thing you're seeing.
You also see this sort of thing happen when you mix extraction operators (>>) with
getline
. Most extraction operators work up to the first whitespace character. As most terminals are line oriented (input doesn't get passed to a program until you press return) if you do something like:
int n = 0;
std::cin >> n;
std::string name;
std::getline( std::cin, name );
and then the user enters an integer for n, presses return and wonders why the string for name is empty.
Basically it's get the hang around the way terminals work, how iostreams work and how they integrate with whatever you're using to invoke your program.