I've made this class:
class object {
// data...
public:
friend std::istream& operator>>(std::istream& in, object& o) {
char c, d;
in >> c >> d;
if (c == d) {
/*set the fail bit some how*/
in.putback(d);
in.putback(c);
} else
o.set_data(c, d);
return in;
}
};
I was looking at the documentation (not well) but couldn't find a proper way to set the fail bit. The reason I care is I'd like to be able to while(std::cin>>obj)/*do stuff*/;
like one can do with an int. But if I currently do this there would be an infinite loop any time there was an error. -_-
Is setting the fail bit possible or am I going to have to work with this problem a different way?
You can use setstate. Note you should put it after the invoke of putback, otherwise the chars won't be putback
ed successfully because the stream has been in an error condition. i.e.
if (c==d) {
in.putback(d);
in.putback(c);
/*set the fail bit some how*/
in.setstate(std::ios_base::failbit);
}