用cin检测输入结束 [英] detecting end of input with cin

查看:106
本文介绍了用cin检测输入结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从用户那里读取一行整数。我不确定如何检查输入是否结束。例如,我希望能够做类似的事情

I want to read a line of integers from the user. I'm not sure how to check to see if the input has ended. For example I want to be able to do something like

int x[MAX_SIZE];
int i = 0;
while(cin.hasNext())
{
  cin >> x[++i];
}

示例输入:2 1 4 -6

Example input: 2 1 4 -6

如何检查 cin 是否还有其他用途?

how can I check to see if there's any more for cin to take?

推荐答案

如果 cin 仍然是交互式的,则没有不再输入的概念,因为它只会等待用户提供更多输入输入(除非用户已通过 Ctrl + D Ctrl + <发出 EOF 信号kbd> Z )。如果要处理一行数据,请从用户那里获取一行(例如, getline ),然后处理该输入(通过从字符串流中提取出来)或类似)。

If cin is still interactive, then there's no notion of "no more input" because it will simply wait for the user to provide more input (unless the user has signaled EOF with Ctrl+D or Ctrl+Z as appropriate). If you want to process a line of data, then get a line from the user (with, say, getline) and then deal with that input (by extracting out of a stringstream or similar).

这篇关于用cin检测输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆