About clear(). Please help!

This is a discussion on About clear(). Please help! within the C++ Programming forums, part of the General Programming Boards category; I recently learnt from C++ Primer 4th Edition that 8.2. Condition States ... ... s.clear(flag): Set specified condition state(s) in ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    About clear(). Please help!

    I recently learnt from C++ Primer 4th Edition that
    8.2. Condition States
    ... ...
    s.clear(flag): Set specified condition state(s) in s to valid state. Type of flag is strm::iostate.
    ... ...
    cin.clear(istream::failbit); // clear the failbit state
    ... ...
    But it turns out that clear sets the condition states according to flag, that's, after clear is called, the condition states is equal to flag.

    Does it mean that the author is wrong?
    Or is it in the new standard, if any, that clear is used to set specified condition states to valid?

    When I refered to C++ Primer 3rd Edition, I found that
    20.7 Condition States
    ... ...
    Using the clear() member function, we reset the condition state to an explicit value.
    ... ...
    I'm really very confused now!

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    As far as I know, clear() resets failed states to normal. Say you try to open a non-existant file, now that stream will evaluate to a fail state, but if you call clear() on it, the state gets restored and you can use it again.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I'm sure the author is wrong. I think that code will clear all flags then set failbit. Have you checked the errata?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Yes, clear() resets failed states to normal. I read the prototype of clear, and found that it's prototyped as "clear(state=goodbit)". So if it's to call clear(), then it resets the states to normal. But here the author says that by callig clear(istream::failbit) clear the failbit state. But in my compiler, it clears all other states and sets the failbit instead.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    as others have said, the author was wrong in the 3rd edition... that's probably one of the reasons there's a 4th edition
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    As i said clear(goodbit) would be a SETTING of goodbit so obviously clear(failbit) isnt going to clear the failbit state as the comment suggested.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    check this definition: http://www.cppreference.com/cppio/clear.html

    in that definition, clear defaults to goodbit, in which case it clears everything... but if you pass it the failbit, it'll only clear that...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i reckon this says it all
    void clear(iostate state = goodbit);
    4 Postcondition: If rdbuf()!=0 then state == rdstate(); otherwise
    rdstate()==state|ios_base::badbit.
    5 Effects: If (rdstate() & exceptions()) == 0, returns. Otherwise, the function throws an
    object fail of class basic_ios::failure (27.4.2.1.1), constructed with implementationdefined
    argument values.
    __________________
    270) This suggests an infinite amount of copying, but the implementation can keep track of the maximum element of the arrays that is
    nonzero.
    616
    © ISO/IEC ISO/IEC 14882:1998(E)
    27 Input/output library 27.4.4.3 basic_ios iostate flags functions
    void setstate(iostate state);
    6 Effects: Calls clear(rdstate() | state) (which may throw basic_ios::failure
    (27.4.2.1.1)).
    look at how setstate is implemented. Kinda says it all for me.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Later in the 4th edition, the author says
    8.2. Condition States
    ... ...
    // remember current state of cin
    istream::iostate old_state = cin.rdstate();
    cin.clear();
    process_input(); // use cin
    cin.clear(old_state); // now reset cin to old state
    ... ...
    I think this means that clear is used to set the states to a given states, isn't it?
    If clear is used to set specified condition states to valid states, isn't the author self-contradiction here?

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Is it "C++ Primer Plus" by Stephen Prata?

    gg

  11. #11
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    NO. It's C++ Primer 4th Edition by Stanley B. Lippman, Josée Lajoie, Barbara E. Moo

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Well, the 3rd edition is highly recommended by two reviewers on the ACCU website:
    http://www.accu.org/cgi-bin/accu/rvo...file=cp001565a

    Hopefully, the 4th edition has improved further. But the authors did leave in a "typo" in this case.

    gg

  13. #13
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    I guess the author is wrong at this point because the author is self-contradiction.
    And when I refered to C++ Programming Language By Bjarne Stroustrup, I found
    ... ...
    s.clear(ios_base::badbit); // set state
    ... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC: Clear Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2005, 01:35 PM
  2. How to Clear in Visual C++
    By MyDestiny in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2005, 09:40 PM
  3. How to Clear in Visual C++ 6.0
    By MyDestiny in forum Windows Programming
    Replies: 1
    Last Post: 03-16-2005, 10:57 AM
  4. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 04:14 AM
  5. Using a button to clear a list box!
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2002, 07:44 PM

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