Thread: Error handling inside overloaded >>

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Error handling inside overloaded >>

    A class... field has the following types of members :
    Code:
            int index;
            std::string name;
            std::string type;
            bool p_key;
            bool c_key;
    I'm asking it to handle inputs of the following format:
    [Int,String1,String2,Bool1,Bool2]
    //The bools seem to be automatically stored and retrieved as o and 1

    So..in the following code...
    What do I put in /*Error*/ ?
    Also, when an unexpected type of input goes through, is it sufficient to return the istream object in its fail state ?

    Code:
        std::istream& operator>>(std::istream& is,field& f)
        {
            char c;
            is>>c;if(c!='['){/*Error*/};
            is>>f.index;
            is>>c;if(c!=','){/*Error*/};
            std::getline(is,f.name,',');
    //         is>>c;if(c!=','){/*Error*/}else is.unget(); //Wrong
            std::getline(is,f.type,',');
    //         is>>c;if(c!=','){/*Error*/}else is.unget(); //Wrong
            is>>f.p_key;
            is>>c;if(c!=','){/*Error*/};
            is>>f.c_key;
            is>>c;if(c!=']'){/*Error*/};
            return is;
        }
    Last edited by manasij7479; 10-24-2011 at 04:36 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    What do I put in /*Error*/ ?
    Also, when an unexpected type of input goes through, is it sufficient to return the istream object in its fail state ?
    That's one way of reporting the problem: simply return the stream when you detect a read error.
    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

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    That's one way of reporting the problem: simply return the stream when you detect a read error.
    And when a read error isn't there....like in the 1st,2nd,5th && 6th ..checks ....do I have to force the stream into such a state ?
    Which bit(?) do I need to set ? ...if I later determine by success by something like if(cin>>object) ?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    do I have to force the stream into such a state ?
    Which bit(?) do I need to set ?
    Looks like you can do something along the lines of:
    Code:
    is.clear(std::ios_base::badbit);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You mean
    is.set(...);
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, I looked up "set stream state" on the interweb and what came back suggested using clear (which I thought was odd but the page I linked to says it also sets the state). I also tested it and it worked so that's what I went with. Maybe setstate would work as well?

    void clear ( iostate state = goodbit );

    Set error state flags
    Sets a new value for the error control state.
    void setstate ( iostate state );

    Set error state flag
    Modifies the current error state value by combining (like with a bitwise OR operation) the error state flags passed as argument with those currently set for the stream.

    Any error bitflag already set is not cleared. If you want to modify the state flags without keeping the current value, use clear() instead.
    Last edited by hk_mp5kpdw; 10-24-2011 at 07:32 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-12-2009, 10:39 AM
  2. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  3. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM
  4. overloaded operator for type conversion error
    By mangoMan in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2004, 08:38 PM
  5. Error handling...
    By XSquared in forum C Programming
    Replies: 5
    Last Post: 07-13-2003, 03:57 AM