简体   繁体   中英

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

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.

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.

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!

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

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

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.

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.

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
}

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

int main( ){
   string word;

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

      //do sth on the input word
   }

// perform some other operations
}

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.

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM