[ACCEPTED]-How to signify no more input for string ss in the loop while (cin >> ss)-eof

Accepted answer
Score: 20

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

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

Score: 7

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

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

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

cin.clear();

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

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

0

Score: 1

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

Score: 1

cin >> some_variable_or_manipulator will always evaluate to a reference to 10 cin. If you want to check and see if there 9 is more input still to read, you need to 8 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 7 goodbit, which is set to true when none 6 of eofbit, failbit, or badbit are set. If 5 there is an error reading, or the stream 4 received an EOF character (from reaching 3 the end of a file or from the user at the 2 keyboard pressing CTRL+D), cin.good() will 1 return false, and break you out of the loop.

Score: 0

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

Score: 0

Take the input from a file. Then you will 8 find that the while loop terminates when 7 your program stops taking input. Actually 6 cin stops taking input when it finds an EOF 5 marker. Each input file ends with this EOF 4 marker. When this EOF marker is encountered 3 by operator>> it modifies the value of internal flag 2 eofbit into false and consequently the while loop 1 stops.

Score: 0

It helps me to terminate loop by hitting 1 ENTER.

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

    // perform some other operations
}
Score: 0

You can make a check for a special word 1 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
}
Score: 0

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 2 to end with CTRL+D(Z). program will quit 1 while sentence end

More Related questions