Taken from Accelerated C++ book, I modified those code to use getline() instead of std::cin >> only to find out that the output has extra line. Could anybody explain why is that so?
getline consumes the \n character, but is supposed to discard it. You should check to see if the compiler you're using conforms to the standard, and if it doesn't, I highly suggest you try a different one.
I am using Eclipse Luna with Gygwin. But it was ok when I tried with Bloodshed Dev-c++ 5.0 though. Since eclipse is more recent, I never thought that it is because of compiler conformation.
There are a number of differences between your example and the source from Accelerated C++. Also, your example does not produce the ouput you give in two ways:
- extra newline
- extra space after the name
No matter what the age of your compiler, the code -- if it compiles -- will not put an extra newline in there unless it is put there by the user (you).
The two obvious ways to do that are to have an inappropriate control code in the source and to have your terminal doing something odd. If neither of these are the case, try to figure out how else you may be introducing an extraneous newline to either the source or the progam input.