Thread: need help with .clear()

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    need help with .clear()

    hi i dont know how and why use the .clear() function
    ie:

    Code:
    file.clear();//if file is a fstream or
    cin.clear();//working with cin
    please any help about the when and why to use it?

    thanks for any help

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    you'd want to use it if you know there is garabage in you streams eg.
    someone inputs a letter instead of a number.
    Woop?

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    http://cplusplus.com/ref/iostream/ios/clear.html

    I use it the most when I read EOF yet need to loop back and read again. At that point i have to call clear() that will allow me to read, otherwise it won't.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    ok thanks for reply

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    expanding on what alphaoide said... it resets (clears) all the flags that have been set for that stream. for example, to set cout to only display two decimal points on floats:

    Code:
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout<<setprecision(2);
    and if you wrote
    Code:
    cout.clear()
    it would clear all the flags you just set.

    note: code not tested... I haven't used it in a long time and am not sure it works... you can try if you like... it would probably be good practice with flags... and here's a list of flags and their manipulators: http://cppreference.com/cppio_flags.html
    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

Popular pages Recent additions subscribe to a feed