(转)getline函数的一个小bug

来源:互联网 发布:豫鲁书院 知乎 编辑:程序博客网 时间:2024/05/17 01:29

这个是出问题的程序
#include <iostream>
#include <string>
int main()
{    using namespace std;
    string str;
    std::cout<<"Please input your name:/n";
    getline(cin,str);
    std::cout<<"Hello,"<<str<<"!!/n";
}
比如说,我们输入"virus welcome back!",但是当我们输入后按回车,程序并不运行cout语句,而是光标还在编绎窗口上闪动,要再按一下[ENTER]才会运行cout这个语句输出,最后在microsof得解:
The getline template function reads an extra character after encountering the delimiter
Article ID:240015Last Review:September 2, 2005Revision:3.0
This article was previously published under Q240015

SYMPTOMS

The Standard C++ Library template getline function reads an extra character after encountering the delimiter. Please refer to the sample program in the More Information section for details.
 
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()->snextc(); /* Remove this line and add the line below.*/   _I.rdbuf()->sbumpc();                break; }
Note Because the resolution involves modifying a system header file, extreme care should be taken to ensure that nothing else is changed in the header file. Microsoft is not responsible for any problems resulting from unwanted changes to the system header files.
 

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section. This problem was corrected in Microsoft Visual C++ .NET.
该文件的一般路径:C:/Program Files/Microsoft Visual Studio/VC98/Include/string      注意是string文件,不是string.h,修改后问题解决
snextc:Advances the get pointer, then returns the next character.   
stossc:Moves the get pointer forward one position, but does not return a character.

原创粉丝点击