iostream和getline [英] iostream and getline
问题描述
您好,
我只想从控制台输入中读取整行。我不能理解的是,我总是要按两次ENTER才能看到
我输入的行。
这是'我的代码:
#include< string>
#include< iostream>
int main(无效)
{
string input ="" ;;
while(input!= 退出)
{
cout<<" \ n $" ;; //显示操作员输入的提示
getline(cin,input,''\ n'');
cout<<输入;
}
cout<< " \ nBye";
返回0;
}
启动程序后,控制台输出如下
$ hello world< ENTER>
< ENTER>
hello world
$ hello world again< ENTER>
$< ENTER>
你好世界再次
$
我想要什么进入该行,按ENTER键并再次使用
作为输出到我的控制台。任何人都可以解释为什么这不起作用吗?
我玩过printf和scanf,效果相同。
我使用Windows NT4 Sp6a和Visual C ++ 6
谢谢
Erich
Hello,
I just want to read a whole line from a console input. What I don''t
understand is that I always have to press ENTER twice in order to see
the line I''ve entered.
Here''s my code :
#include <string>
#include <iostream>
int main(void)
{
string input = "";
while ( input != "exit")
{
cout <<"\n$"; // show prompt for operator input
getline(cin, input, ''\n'');
cout << input;
}
cout << "\nBye";
return 0;
}
After starting the program the console output is as follows
$hello world <ENTER>
<ENTER>
hello world
$hello world again <ENTER>
$ <ENTER>
hello world again
$
What I want is entering the line, pressing ENTER and having it again
as an output to my console. Can anyone explain why this doesn''t work ?
I''ve played around with printf and scanf which had the same effect.
I use Windows NT4 Sp6a and Visual C++ 6
thanks
Erich
推荐答案
" ;; //显示操作员输入的提示
getline(cin,input,''\ n'');
cout<<输入;
}
cout<< " \ nBye";
返回0;
}
启动程序后,控制台输出如下
"; // show prompt for operator input
getline(cin, input, ''\n'');
cout << input;
}
cout << "\nBye";
return 0;
}
After starting the program the console output is as follows
hello world< ENTER>
< ENTER>
hello world
hello world <ENTER>
<ENTER>
hello world
再次问候世界< ENTER>
hello world again <ENTER>
这篇关于iostream和getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!