Setting fail state in istream::operator>>()

Setting fail state in istream::operator>>()

Post by David Arnste » Fri, 13 May 1994 04:03:48

I find it impossible to create an extractor (operator>>) to work with a data
type that I define myself.  Specifically, after I program a
     class Number;
(for example) I would like to enable reading an instance of Number using the
familiar extractor syntax, ie
     Number num;
     cin >> num;

My problem is that inside my extractor operator
     istream& operator>> (istream& ist, Number& num);
I would like to call
when I encounter garbage reading stream ist.  Guess what.  ios::setstate() is
protected.  So I can't call it!

I propose that a public method ios::setfail(void) be added to class ios which
simply calls ios::setstate(ios::failbit).

I find that the iostream package is moderately awkward to use and extremely
difficult to extend.  Since C++ is supposed to be object oriented, I'm very
disappointed.  I hope that the ANSI committee will consider standardizing the
iostream library, and making it easier to extend.
David Arnstein       |          What do you mean, "get a life"?


Setting fail state in istream::operator>>()

Post by Terry L » Fri, 13 May 1994 07:47:51

>My problem is that inside my extractor operator
>     istream& operator>> (istream& ist, Number& num);
>I would like to call
>     ist.setstate(ios::failbit);
>when I encounter garbage reading stream ist.  Guess what.  ios::setstate() is
>protected.  So I can't call it!

        Try "ist.clear(ios::failbit | ist.rdstate());".  This sets failbit and
doesn't disturb the other bits.  I think the naming of ios::clear() is a little


Coordinated Science Lab
University of Illinois


1. safe istream operator>>()

I've tried the following in my Attack object:

istream& operator>> (istream& is, attack& attackToLoad)
    is >> attackToLoad.description;  // string
    is >> attackToLoad.toHit;           // float
    is >> attackToLoad.damage;      // float
    is >> attackToLoad.numTries;    // unsigned int
    return is;

Trouble with these tribbles is user input; if I accidentally do
something like separating my input variables with a comma, the program
goes into an infinite loop in the input routine. Obviously, the input
must match the data type, or something wicked this way comes.

Is there a method, or more correct way to load data from an istream?

< 1K Download

2. talking to other phone manufactures

3. Resizing a CListBox>>>>>>>>>>>>>>>>>>>>>>>>>>>

4. Same page link

5. " INNOVATION INFOS + IMAGING " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

6. please help

7. Defect Report: operator>>(istream&, string&) doesn't set failbit

8. Precedence of operator>>()?

9. Help, can't overload operator>>()

10. HELP >>>>>>>>>>>>> WINAPI <<<<<<<<<<<<<<<<<

11. Windows 3.x printqueue >>>>>>>

12. b.template to_string<char, char_traits<char>, allocator<char> >()