【问题标题】:How to signify no more input for string ss in the loop while (cin >> ss)如何在循环中表示不再输入字符串 ss while (cin >> ss)
【发布时间】:2021-02-27 17:25:50
【问题描述】:

我使用“cin”从输入流中读取单词,比如

int main( ){
     string word;
     while (cin >> word){
         //do sth on the input word
     }

    // perform some other operations
}

代码结构与上述类似。它是可编译的。在执行过程中,我不断输入类似

aa bb cc dd

我的问题是如何结束这个输入?换句话说,假设文本文件只是“aa bb cc dd”。但是不知道怎么让程序知道文件结束了。

【问题讨论】:

    标签: c++ file-io cin eof


    【解决方案1】:

    您的代码是正确的。如果您是交互式输入,则需要发送 EOF 字符,例如 CTRL-D。

    读取文件时不需要此 EOF 字符。这是因为一旦你到达输入流的末尾,就没有任何东西可以“cin”了(因为流现在已关闭),因此 while 循环退出。

    【讨论】:

    • 值得注意的是,从键盘发出 EOF 信号的方式因操作系统而异。 MS Windows 需要按 CTRL-Z,而 Unix 和相关操作系统需要 CTRL-D。
    • @MikeLewis 不在 Clion 上工作我该怎么办?
    【解决方案2】:

    由于其他人已经回答了这个问题,我想补充一点:

    由于 Windows 上的 Ctrl-Z(和 unix 系统上的 Ctrl-D)导致 EOF 到达,并且您退出 while 循环,但在 while 循环之外您无法读取进一步的输入,因为 EOF 是已经达到了。

    所以要再次使用cin启用读取,您需要清除eof标志,以及所有其他失败标志,如下所示:

    cin.clear();
    

    完成此操作后,您可以再次使用cin 开始读取输入!

    【讨论】:

    • 正是我想要的。
    • @Nawaz 没有在 Clion 上工作我该怎么办?
    【解决方案3】:
    int main() {
         string word;
         while (cin >> word) {
             // do something on the input word.
             if (foo)
               break;
         }
        // perform some other operations.
    }
    

    【讨论】:

      【解决方案4】:

      按 Ctrl-Z(在 *nix 系统上按 Ctrl-D)并按 Enter。这会发送一个 EOF 并使流无效。

      【讨论】:

      • 这也会停止该过程。没有?
      • while 循环结束。我不确定你所说的过程是什么意思。我很困惑,您是从文件中还是从标准输入中获取输入?或者你用 重定向输入
      • @Nawaz:不,它不会停止这个过程。您可能会将其与 Ctrl-C 混淆。
      【解决方案5】:

      cin >> some_variable_or_manipulator 将始终评估为对cin 的引用。如果您想检查是否还有更多输入要读取,则需要执行以下操作:

      int main( ){
           string word;
           while (cin.good()){
               cin >> word;
               //do sth on the input word
           }
      
          // perform some other operations
      }
      

      这会检查流的 goodbit,当 eofbit、failbit 或 badbit 都没有设置时,它设置为 true。如果读取错误,或者流接收到 EOF 字符(到达文件末尾或用户在键盘上按下 CTRL+D),cin.good() 将返回 false,并让您脱离循环。

      【讨论】:

        【解决方案6】:

        我猜你想在文件末尾跳出。 你可以得到basic_ios::eof的值,它在流结束时返回true。

        【讨论】:

          【解决方案7】:

          从文件中获取输入。然后你会发现当你的程序停止接受输入时while循环终止了。 实际上,cin 在找到 EOF 标记时会停止输入。每个输入文件都以此 EOF 标记结尾。当operator>> 遇到此 EOF 标记时,它会将内部标志 eofbit 的值修改为 false,因此 while 循环停止。

          【讨论】:

            【解决方案8】:

            它可以帮助我通过按 ENTER 来终止循环。

            int main() {
                string word;
                while(getline(cin,word) && s.compare("\0") != 0) {
                    //do sth on the input word
                }
            
                // perform some other operations
            }

            【讨论】:

              【解决方案9】:

              您可以检查输入中的特殊单词。 F.e. “停止”:

              int main( ){
                 string word;
              
                 while (cin >> word){
                    if(word == "stop")
                        break;
              
                    //do sth on the input word
                 }
              
              // perform some other operations
              }
              

              【讨论】:

                【解决方案10】:

                你可以试试这个

                    string word;
                    vector<string> words;
                    while (cin >> word) {
                                                            
                        words.push_back(word);
                        if (cin.get() == '\n')
                            break;
                    }
                

                这样,您不必以 CTRL+D(Z) 结束。程序将在句子结束时退出

                【讨论】:

                  【解决方案11】:

                  您的程序不计算空格。区分 cin 和 getline...

                  这是一个带有技巧的示例:程序获取输入并打印输出,直到您按两次 Enter 退出:

                  #include <iostream>
                  #include <string>
                  using namespace std;
                  
                  int main(){
                  
                  
                      char c = '\0';
                      string word;
                      int nReturn = 0;
                  
                      cout << "Hit Enter twice to quit\n\n";
                  
                      while (cin.peek())
                      {
                          cin.get(c);
                  
                          if(nReturn > 1)
                              break;
                          if('\n' == c)
                              nReturn++;
                          else
                              nReturn = 0;
                          word += c;
                          cout << word;
                          word = "";
                      }
                  
                      cout << endl;
                      return 0;
                  }
                  

                  【讨论】:

                  • 您的答案完全不相关,因此,这不是答案,而是旁注。
                  猜你喜欢
                  • 2020-09-12
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-06-05
                  • 2020-05-10
                  • 1970-01-01
                  • 2017-06-21
                  • 2016-04-26
                  相关资源
                  最近更新 更多