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

发布于 2024-10-24 05:41:10 字数 351 浏览 1 评论 0原文

我使用“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”。但我不知道如何让程序知道文件结束。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

嘿看小鸭子会跑 2024-10-31 05:41:11

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

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

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

cin.clear();

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

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 while loop, but outside the while loop you cannot read further input, since the EOF is already reached.

So to enable reading using cin again, you need to clear eof flag, and all other failure flags, as shown below:

cin.clear();

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

幸福还没到 2024-10-31 05:41:11
int main() {
     string word;
     while (cin >> word) {
         // do something on the input word.
         if (foo)
           break;
     }
    // perform some other operations.
}
int main() {
     string word;
     while (cin >> word) {
         // do something on the input word.
         if (foo)
           break;
     }
    // perform some other operations.
}
泅人 2024-10-31 05:41:11

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

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

驱逐舰岛风号 2024-10-31 05:41:11

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 时,该 Goodbit 将设置为 true。如果读取错误,或者流接收到 EOF 字符(到达文件末尾或用户在键盘上按 CTRL+D),cin.good() 将返回 false,并让您退出环形。

cin >> some_variable_or_manipulator will 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:

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.

空城旧梦 2024-10-31 05:41:11

我猜你想在文件末尾跳出。
可以获取 basic_ios::eof 的值,最后返回 true的流。

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.

可是我不能没有你 2024-10-31 05:41:11

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

Take the input from a file. Then you will find that the while loop terminates when your program stops taking input.
Actually cin stops 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 eofbit into false and consequently the while loop stops.

不醒的梦 2024-10-31 05:41:11

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

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

    // perform some other operations
}

It helps me to terminate loop by hitting ENTER.

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

    // perform some other operations
}
烏雲後面有陽光 2024-10-31 05:41:11

您可以检查输入中是否有特殊单词。
Fe“停止”:

int main( ){
   string word;

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

      //do sth on the input word
   }

// perform some other operations
}

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

int main( ){
   string word;

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

      //do sth on the input word
   }

// perform some other operations
}
于我来说 2024-10-31 05:41:11

你可以

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

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

you can try this

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

in this way, you don't have to end with CTRL+D(Z). program will quit while sentence end

安人多梦 2024-10-31 05:41:11

你的程序不接受 count 个空格。区分 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;
}

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

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

#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;
}
橘亓 2024-10-31 05:41:10

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

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

Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文