Making an input stream fail.

From: Jason Heyes (jasonheyes_at_optusnet.com.au)
Date: 10/23/04


Date: Sat, 23 Oct 2004 14:47:33 +1000

I have a function whose job is to read a non-negative integer. Is this the
right way to do it?

std::istream &read_integer(std::istream &is, int &i)
{
    if (!(is >> i) || i < 0)
        is.setstate(is.failbit);
    return is;
}

Any help is appreciated.