(转)getline函数的一个小bug

当使用getline从cin读取字符串时,程序会在遇到分隔符后额外读取一个字符,导致需要两次回车才能继续执行。这是Visual C++ .NET中getline模板函数的一个已知bug。解决方案是修改系统头文件<string>中的getline成员函数,通过调整snextc和stossc来避免读取额外字符。完成修改后,问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个是出问题的程序
#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:240015
Last Review:September 2, 2005
Revision: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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值