【问题标题】:detecting end of input with cin用 cin 检测输入结束
【发布时间】:2014-01-29 01:27:11
【问题描述】:

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

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

示例输入:2 1 4 -6

我如何查看cin 是否还有其他东西可以服用?

【问题讨论】:

标签: c++ user-input


【解决方案1】:

您必须执行以下操作

int temp;

vector<int> v;
while(cin>>temp){
    v.push_back(temp);
}

您也可以使用检查输入是否结束

if(cin.eof()){
    //end of input reached
}

【讨论】:

    【解决方案2】:

    如果cin 仍然是交互式的,那么就没有“不再输入”的概念,因为它只会等待用户提供更多输入(除非用户使用 CtrlEOF 发出信号>+DCtrl+Z(视情况而定)。如果您想处理一行数据,则从用户那里获取一行(例如,getline),然后处理该输入(通过从字符串流中提取或类似的方法)。

    【讨论】:

      【解决方案3】:

      这很简单。您需要做的就是执行提取作为条件:

      while (i < MAX_SIZE && std::cin >> x[i++])
      

      如果提取因任何原因失败(没有剩余字符、无效输入等),循环将终止,失败将在输入流的流状态中表示。

      考虑到最佳实践,您不应该使用静态 C 数组。您应该使用编译时容器 std::array&lt;T, N&gt;(或 std::vector&lt;T&gt;,如果前者不受支持)。

      这是一个使用std::vector 的示例。它还利用迭代器,无需显式创建输入副本:

      std::vector<int> v{ std::istream_iterator<int>{std::cin},
                          std::istream_iterator<int>{}};
      

      【讨论】:

        【解决方案4】:

        使用 fstream 你可以做这样的事情

        ifstream ifile("input.txt");
        while(!ifile.eof())
        {
            /* do something */
        }
        

        你也可以用这个

        if(!ifile.is_open())
        {
           /* do something */
        }
        

        【讨论】:

          【解决方案5】:

          你可能想要这样的东西:

          int inp;
          
          while(cin >> inp){  
              ....
              if(cin.peek() == '\n')
                  cin.clear(ios::eofbit);
              ....
              }
          

          只要 I/O 成功,while 循环就会运行。假设您想在整数行结束时结束输入,当遇到\n 时手动设置eofbit。这通过条件if(cin.peek() == '\n') 进行检查。当条件为真时,while 循环终止。在下面的例子中,我演示了如何将一行以空格分隔的整数读入一个向量,然后用空格分隔打印出来

          #include<iostream>
          #include<vector>
          #include<iterator>
          #include<algorithm>
          using namespace std;
          
          int main(){
              vector<int> coll;
              int inp;
          
              while(cin >> inp){ 
                  if(cin.peek() == '\n')
                      cin.clear(ios::eofbit);
                  coll.push_back(inp); 
              }
          
              copy(
                  coll.cbegin(),
                  coll.cend(),
                  ostream_iterator<int>(cout, " ")
              );
              cout << endl;
          }
          
          

          【讨论】:

            【解决方案6】:

            这个想法和下面的代码很相似,所以你可以试试:

            int tmp;
            while(cin >> tmp != NULL){ // C++ or while(scanf("%d", &tmp) != -1) {} for C
                 // do something
            }
            
            

            【讨论】:

            • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
            【解决方案7】:

            我通常会在下面检测到 cpp 流的结尾:

            while (cin.peek() != EOF) {
              // To do your stuff...
              // NOTE: peek() will set failbit when peeking end of stream and return EOF(-1).
            }
            

            【讨论】:

              猜你喜欢
              • 2013-11-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多