while (getline(cin, line))
cout << line << endl;
本该在敲回车后,输出line的内容,但是虚多敲一个回车才达到目的。这是vc6.0的一个bug,在vs中此问题不存在。
通过多读一次char的方法放掉这个回车符,这个在C++编程思想里有很详细的解释。
- FIX: getline Template Function Reads Extra Character
-
- RESOLUTION
-
- Modify the getline member function, which can be found in the following system header file "string", as follows:
-
- else if (_Tr::eq((_E)_C, _D))
- {_Chg = true;
-
-
- _I.rdbuf()->sbumpc();
- break; }
-
-
- STATUS
-
- Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
-
- This problem was corrected in Microsoft Visual C++ .NET.
-
- MORE INFORMATION
-
- The following sample program demonstrates the bug:
-
- #include <string>
- #include <iostream>
- int main () {
- std::string s,s2;
- std::getline(std::cin,s);
- std::getline(std::cin,s2);
- std::cout << s <<'/t'<< s2 << std::endl;
- return 0;
- }
-
-
-
- Hello<Enter Key>
- World<Enter Key>
- <Enter Key>
- Hello World
-
-
-
- Hello<Enter Key>
- World<Enter Key>
- Hello World
-
-
- --------------------------------------------------------------------------------
-
- APPLIES TO
- • The Standard C++ Library, when used with:
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++ 6.0 Standard Edition
转自:http://blog.csdn.net/developinglife/article/details/6236260