cin.eof() functionality

前端 未结 8 1169
夕颜
夕颜 2021-01-11 13:40

I understand that cin.eof() tests the stream format. And while giving input, end of character is not reached when there is wrong in the input. I tested

相关标签:
8条回答
  • 2021-01-11 13:49

    cin.eof() test if the stream has reached end of file which happens if you type something like Ctrl+C (on Windows), or if input has been redirected to a file etc.

    To test if the input contains an integer and nothing but an integer, you can get input first into a string and then convert that with a stringstream. A stringstream indeed reaches eof if there's no more to be extracted from it.

    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main() {
        using namespace std;
        int i;
        string input;
        cin >> input; //or getline(cin, input)
        stringstream ss(input);
        if (ss >> i && ss.eof()) {  //if conversion succeeds and there's no more to get
            cout<< "\n Correct Input \n";
        }
        else {
            cout<< "\n Format Error \n";
        }
    
      return 0;
    }
    
    0 讨论(0)
  • 2021-01-11 13:53

    Assuming your input is line based, I suggest that you read the whole line using std::getline(). Once you have the line, you can analyse it and decide whether it contains correct or wrong input. Put the line into std::istringstream and do something like the following:

    Edit: Changed !! iss to static_cast<bool>(iss) for compatibility with C++0x.

    std::istringstream iss (line);
    char ch;
    long lval;
    // read the input
    iss >> lval;
    // result variable will contain true if the input was correct and false otherwise
    result
        // check that we have read a number of at least one digit length
        = static_cast<bool>(iss)
        // check that we cannot read anything beyond the value read above
        && ! (iss >> ch);
    
    0 讨论(0)
  • 2021-01-11 13:58

    std::cin.eof() tests for end-of-file (hence eof), not for errors. For error checking use !std::cin.good(), the built-in conversion operator (if(std::cin)) or the boolean negation operator (if(!std::cin)).

    0 讨论(0)
  • 2021-01-11 14:04
    #include <iostream>
    
    int main() {
      using namespace std;
      int i;
      if (cin >> i) {
        cout << "Extracted an int, but it is unknown if more input exists.\n";
        char c;
        if (cin.get(c)) {  // Or: cin >> c, depending on how you want to handle whitespace.
          cin.putback(c);
          cout << "More input exists.\n";
          if (c == '\n') {  // Doesn't work if you use cin >> c above.
            cout << "But this was at the end of this line.\n";
          }
        }
        else {
          cout << "No more input exists.\n";
        }
      }
      else {
        cout << "Format error.\n";
      }
      return 0;
    }
    

    Also see Testing stream.good() or !stream.eof() reads last line twice.

    Sample session with the above program, note that input lines are marked with comments not present in the actual output:

    $ your-program
    12  # input
    Extracted an int, but it is unknown if more input exists.
    More input exists.
    But this was at the end of this line.
    
    $ your-program
    12a  # input
    Extracted an int, but it is unknown if more input exists.
    More input exists.
    
    $ echo -n 12 | your-program
    Extracted an int, but it is unknown if more input exists.
    No more input exists.
    
    $ your-program
    a  # input
    Format error.
    
    0 讨论(0)
  • 2021-01-11 14:09

    Use a direct test of the status of the stream with:

    while (cin >> i)
    {
        ...
    }
    
    0 讨论(0)
  • 2021-01-11 14:10

    EOF stands for e nd o f f ile. std::cin is the standard input. The standard input, under normal circumstances, never reaches the end: you can always just type some more.

    Further, .eof() only returns true if an input operation has already failed because of trying to read past the end of the file. The program actually can't tell that it's "at the end of file"; that is, the only way it can find out that the next attempt to read data will fail is by actually making the attempt.

    0 讨论(0)
提交回复
热议问题