It's certainly not a bug in the compiler, despite its being Microsoft's. If it were a bug in anything, it'd be a bug in the C++ iostreams library.
However, it's not a bug at all. That's the expected behavior of [tt]getline[/tt]. Reread Salem's post; it explains what's going on, which is what's supposed to go on.
To explain it using some "graphics" and a hypothetical program fragment:
Pretend you start off with a blank input buffer:
Input Buffer
Your code does:
Code:
cout << "Gimme an integer: ";
You user obeys and types "53", causing your program's input buffer to look like:
Input Buffer
Still blank? Yup. Your terminal doesn't deliver anything to your program until the user presses the ENTER key. Assuming they've used a computer before, they probably will, leaving you with:
Input Buffer
Note that there's a newline character in your input buffer, which was placed there when the user pressed ENTER.
Now, say the next line of your program reads:
Now that there's something in the input buffer, [tt]cin[/tt] can do its job and read some digits from the input buffer, the use them to set [tt]num[/tt] to an appropriate value.
After it pulls the digits out, you're left with:
Input Buffer
and [tt]num[/tt] equal to 53.
It didn't do anything with the newline. It's not a digit; it can't be part of this integer [tt]cin[/tt] is trying to read, so [tt]cin[/tt] leaves it in the buffer.
Then, your program does:
Code:
cout << "What's your name? ";
Your user eloquently replies by typing "bob" and pressing ENTER.
Input Buffer
Check out that newline that's still there at the beginning. You never got rid of it, so it stays there.
If the next line of your program reads:
then you've told it to store the next line in the input buffer into [tt]name[/tt].
So it will. It'll read the next line in the input buffer. That means it'll read up to the next newline character. That means it'll read up to the first character in the input stream. That's because the first character in the input stream is a newline. That's because you never cleared that newline out.
Your input buffer will look like:
Input Buffer
and your [tt]name[/tt] will be the blank line that newline read.
Note that getline did remove the newline from the buffer. That's what it's supposed to do. Note also that it didn't put that newline in [tt]name[/tt]. That's also correct behavior.
So instead of using [tt]getline[/tt] right away, you use [tt]ignore[/tt]:
which tells it to throw out everything up to the newline that got left in there.
That gives you:
Input Buffer
When you use [tt]getline[/tt] at this point (after using [tt]ignore[/tt]), there's no newline left at the beginning, so the first newline it hits is the one
after "bob" in the buffer.
So [tt]getline[/tt] grabs "bob\n" out of the buffer, leaving you with:
Input Buffer
and the string "bob" (without the newline) in the [tt]name[/tt] variable.
Just like before, [tt]getline[/tt] removed the newline from the buffer, and left it out of [tt]name[/tt]. Again, that's what [tt]getline[/tt] is supposed to do.
I've actually seen some libraries that get this wrong and leave the newline in the input buffer.
That's a bug, but it's in the library, not the compiler.
If your library exhibits that bug, you may have to use [tt]ignore[/tt] after calls to [tt]getline[/tt]. That's only if your library is broken, though.