C++ 如何表示循环中不再有字符串 ss 的输入 while (cin >> ss)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5360129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:59:29  来源:igfitidea点击:

How to signify no more input for string ss in the loop while (cin >> ss)

c++file-iocineof

提问by user630983

I used "cin" to read words from input stream, which like

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

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

    // perform some other operations
}

The code structure is something like the above one. It is compilable. During the execution, I keep inputting something like

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

aa bb cc dd

My question is how to end this input? In other words, suppose the textfile is just "aa bb cc dd". But I do not know how to let the program know that the file ends.

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

回答by Mike Lewis

Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as CTRL-D.

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

This EOF character isn't needed when you are reading in a file. This is because once you hit the end of your input stream, there is nothing left to "cin"(because the stream is now closed), thus the while loop exits.

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

回答by Nawaz

As others already answer this question, I would like add this important point:

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

Since Ctrl-Z on Windows (and Ctrl-D on unix systems) causes EOF to reach, and you exit from the whileloop, but outside the whileloop you cannot read further input, since the EOF is already reached.

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

So to enable reading using cinagain, you need to clear eofflag, and all other failure flags, as shown below:

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

cin.clear();

After doing this, you can start reading input using cinonce again!

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

回答by fulvio

int main() {
     string word;
     while (cin >> word) {
         // do something on the input word.
         if (foo)
           break;
     }
    // perform some other operations.
}

回答by jonsca

Hit Ctrl-Z (Ctrl-D on *nix systems) and hit enter. That sends an EOF and invalidates the stream.

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

回答by Null Set

cin >> some_variable_or_manipulatorwill always evaluate to a reference to cin. If you want to check and see if there is more input still to read, you need to do something like this:

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
}

This checks the stream's goodbit, which is set to true when none of eofbit, failbit, or badbit are set. If there is an error reading, or the stream received an EOF character (from reaching the end of a file or from the user at the keyboard pressing CTRL+D), cin.good() will return false, and break you out of the loop.

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

回答by Mostafiz Rahman

Take the input from a file. Then you will find that the while loop terminates when your program stops taking input. Actually cinstops taking input when it finds an EOF marker. Each input file ends with this EOF marker. When this EOF marker is encountered by operator>>it modifies the value of internal flag eofbitinto false and consequently the while loop stops.

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

回答by Eldar

It helps me to terminate loop by hitting ENTER.

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

int main() {
    string word;
    while(getline(cin,word) && s.compare("
int main( ){
   string word;

   while (cin >> word){
      if(word == "stop")
          break;

      //do sth on the input word
   }

// perform some other operations
}
") != 0) { //do sth on the input word } // perform some other operations }

回答by Nickolas

I guess you want to jump out at the end of file. You can get the value of basic_ios::eof, it returns true at the end of stream.

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

回答by Serid

You can make a check for a special word in input. F.e. "stop":

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

#include <iostream>
#include <string>
using namespace std;

int main(){


    char c = '##代码##';
    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;
}

回答by Raindrop7

your program doesn't take in count white spaces. make difference between cin and getline...

您的程序不考虑空格。cin 和 getline 之间的区别...

here is an example with a trick: the program get input and prints output until you hit twice Enter to quit:

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

##代码##