Thread: If EOF evaluates to -1 why does it break a while loop

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    If EOF evaluates to -1 why does it break a while loop

    In the conditional statement in the while loop below, the extraction operator is used on an input stream object "i". When the file reaches an EOF, the loop breaks.

    This doesnt make sense as I understand it EOF usually evaluates to -1 and a loop only breaks on a value of false or 0.

    Code:
    while (i >> next){
    //do stuff...
    }
    How can you explain this?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    Quote Originally Posted by Vespasian_2 View Post
    In the conditional statement in the while loop below, the extraction operator is used on an input stream object "i". When the file reaches an EOF, the loop breaks.

    This doesnt make sense as I understand it EOF usually evaluates to -1 and a loop only breaks on a value of false or 0.

    Code:
    while (i >> next){
    //do stuff...
    }
    How can you explain this?
    I suggest asking C++ questions in the C++ sub-forum; you are more likely to get faster answers.
    Edit: In case you did not know the code posted is C++ code!

    Tim S.
    Last edited by stahta01; 10-25-2018 at 06:10 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    operator >> only ever returns the stream itself (no EOF, no input counts, no nothing) because otherwise chaining would be ... problematic. If the stream is in any error state, it will evaluate to false.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    710
    EOF is just a macro defined to be a negative integer, usually -1. It happens to be returned by some functions to indicate the end of file condition, but there is nothing special about it.

    Assuming i is an istream, the extraction operator returns a reference to the istream object itself so that extraction operators can be chained. E.g., due to the left-to-right associativity of the >> operator, i >> first >> next >> last is processed like (((i >> first) >> next) >> last), with (i >> first) returning a reference to i so that the next operation is (i >> next), etc.

    Finally, when the final istream reference is returned and evaluated in a boolean context, it returns false if it's at eof or in an error condition.
    Code:
    #include <iostream>
    using namespace std;
     
    class Object {
        int n;
    public:
        Object(int n) : n{n} {}
     
        operator bool() { return n % 2 == 0; } // true if n is even
     
        friend Object& chain(Object& o, int n) {
            cout << '+' << n << '\n';
            o.n += n;
            return o;
        }
         
        friend ostream& operator<<(ostream& os, const Object& o) {
            return os << o.n;
        }
    };
     
    int main() {
        Object o(11), p(22);
     
        if (o) cout << "o\n";
        if (p) cout << "p\n";
     
        cout << o << '\n';
        chain(chain(chain(o, 1), 2), 3);
        cout << o << '\n';
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,266
    If you do want to write your own conversion function to bool for a similiar purpose, it would be better to declare it as explicit to avoid unintended implicit conversions. It typically would also be declared const as it is unlikely that the conversion will change the observable state of the object:
    Code:
    explicit operator bool() const { /* ... */ }
    Last edited by laserlight; 10-26-2018 at 03:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by tabstop View Post
    operator >> only ever returns the stream itself (no EOF, no input counts, no nothing) because otherwise chaining would be ... problematic. If the stream is in any error state, it will evaluate to false.
    Thanks all. Makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop won't break;
    By Dagda in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2012, 12:46 PM
  2. break from for loop
    By taurus in forum C Programming
    Replies: 3
    Last Post: 09-24-2009, 03:54 PM
  3. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  4. Loop will not break.
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 03-20-2008, 06:51 PM
  5. Need help with break in do while loop
    By JoelearningC in forum C Programming
    Replies: 3
    Last Post: 03-19-2008, 11:12 PM

Tags for this Thread