【问题标题】:Interrupt cin >> str while loop [duplicate]中断cin >> str while循环[重复]
【发布时间】:2017-07-24 14:39:52
【问题描述】:

当我使用Ctrl+D时,它显示:

process finished with exit code 0

而且,循环后面的代码永远不会被执行(cout 语句)。如果我想继续执行这个程序怎么办?

string str;
while(cin >> str);
cout << "outside of the loop";
return 0

环境:适用于 Linux 的 CLion。

【问题讨论】:

  • 比处理 EOF (ctrl-d)。
  • 确定吗?你可能只是没有时间看它?或者您可能会因为它不在自己的路线上而错过它?尝试在输出后添加另一个输入(使用例如std::getline),或添加尾随换行符。
  • 试试"outside of the loop\n"
  • @Some "尝试添加另一个输入..." 这不需要clear() cin吗?
  • 从命令行运行你的程序。

标签: c++


【解决方案1】:

我认为这是 Clion 的错误,因为当我尝试在终端上运行时,它就像一个魅力。或者,我更喜欢 std::cerr 而不是 std::cout,它也适用于 Clion 和 Terminal。

【讨论】:

  • 程序从main返回时会刷新缓冲区。
  • 这是我的错误。我已经编辑过了。 @BenjaminLindley
猜你喜欢
  • 2014-04-27
  • 2017-06-21
  • 2011-11-23
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多