Error handling inside overloaded >>

This is a discussion on Error handling inside overloaded >> within the C++ Programming forums, part of the General Programming Boards category; A class... field has the following types of members : Code: int index; std::string name; std::string type; bool p_key; bool ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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) ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    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,803
    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, 09:39 AM
  2. Replies: 3
    Last Post: 12-09-2008, 10: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, 07:38 PM
  5. Error handling...
    By XSquared in forum C Programming
    Replies: 5
    Last Post: 07-13-2003, 03:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21