getline函数输入要击两次回车的解决办法

lab17 2008-10-24 09:40:12
这个是出问题的程序
#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.

MORE INFORMATION
The following sample program demonstrates the bug:
//test.cpp
//Compiler options : /GX
#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;
}
Actual Results:
Hello<Enter Key>
World<Enter Key>
<Enter Key>
Hello World
Expected Results:
Hello<Enter Key>
World<Enter Key>
Hello World
我给大家解释下意思吧:症状:c++的标准库模板函数getline在读到限定符后还要读取额外字符...解决方法:else if (_Tr::eq((_E)_C, _D)) {_Chg = true; // _I.rdbuf()->snextc(); /* 把这一行注释掉,添加下一行.*/ _I.rdbuf()->sbumpc(); break; }修改系统头文件时大家要小心点...现状:微软已经确认这是他们产品中的'臭虫'(bug),这个bug已经在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.


欢迎来QQ群讨论 49467266
访问原文请点击:http://virus.blog.51cto.com/51437/20433
...全文
753 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
duan27 2011-10-24
  • 打赏
  • 举报
回复
呵呵,改过之后,问题已解决
saidecade 2011-08-06
  • 打赏
  • 举报
回复
无效果 问题依旧
ewyoung0316 2011-02-26
  • 打赏
  • 举报
回复
按照上面的改了,还是有问题?还有谁知道怎么解决...==
dasddasdsa 2010-12-06
  • 打赏
  • 举报
回复
我也遇到这个问题了,求解释啊
aiken321 2010-05-17
  • 打赏
  • 举报
回复
对对对! 我的vc6.0就这样
xiaocilang 2010-04-24
  • 打赏
  • 举报
回复
我错了 你提了 抱歉
xiaocilang 2010-04-24
  • 打赏
  • 举报
回复
抄袭 真是过分 也不提原始的人
Vegertar 2008-10-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 weiyijiji 的回复:]
这只是VC6.0的问题
[/Quote]
code::blocks无此现象。
帅得不敢出门 2008-10-25
  • 打赏
  • 举报
回复
up
cismylife 2008-10-25
  • 打赏
  • 举报
回复
学习+节分
liubuweiright 2008-10-24
  • 打赏
  • 举报
回复
顶一下,一分
太乙 2008-10-24
  • 打赏
  • 举报
回复
呵呵,~~
weiyijiji 2008-10-24
  • 打赏
  • 举报
回复
这只是VC6.0的问题
luxiaoxun 2008-10-24
  • 打赏
  • 举报
回复
sys0003 2008-10-24
  • 打赏
  • 举报
回复
呵呵。
lzr4304061988012 2008-10-24
  • 打赏
  • 举报
回复
LZ,我没出现你这种现象:

Please input your name:
virus welcome back
Hello,virus welcome back!!
请按任意键继续. . .
baihacker 2008-10-24
  • 打赏
  • 举报
回复
呵.

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧