c++primer上一段程序该如何改进好!

wbh0360 2003-01-30 08:03:20
#include <iostream>
#include <string>
int main()
{
string word;
while ( cin >> word )
cout ≪ "word read is: " ≪ word ≪ '\n';
cout ≪ "ok: no more words to read: bye!\n";
return 0;
}

这个程序死循环,怎样改进让它正常推出。
...全文
25 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
allen1981813 2003-02-01
  • 打赏
  • 举报
回复
有意思
idler 2003-01-31
  • 打赏
  • 举报
回复
要用getline输入:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string word;

while ( getline(cin, word, '\n') )
{
if (word.empty())
break;

cout << "word read is: " << word << '\n';
}

cout << "ok: no more words to read: bye!\n";

return 0;
}
wbh0360 2003-01-31
  • 打赏
  • 举报
回复
不行啊,我试过!
idler 2003-01-31
  • 打赏
  • 举报
回复
那就规定一个特定的输入结束程序,比如空串?
wbh0360 2003-01-31
  • 打赏
  • 举报
回复
按F6是可行的,不过有没有更好的方法,无须去按功能键?
seok 2003-01-30
  • 打赏
  • 举报
回复
结束条件是:^z
idler 2003-01-30
  • 打赏
  • 举报
回复
不过你漏了
using namespace std;

#include <iostream>
#include <string>

using namespace std;

int main()
{
string word;

while ( cin >> word )
cout << "word read is: " << word << '\n';

cout << "ok: no more words to read: bye!\n";

return 0;
}
idler 2003-01-30
  • 打赏
  • 举报
回复
这个程序从standard input读入字符串直到读到EOF。
在命令行方式下调试时,按F6即可退出(当时在DOS下用copy编辑过文本都知道)。
xyc6838 2003-01-30
  • 打赏
  • 举报
回复
if word==NULL
break;
加入到程序里

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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