cin.get on an empty line and cin.clear

This is a discussion on cin.get on an empty line and cin.clear within the C++ Programming forums, part of the General Programming Boards category; In my book, C++ Primer Plus 4th Edition, it says that cin.get(buf, 80) will set the failbit and block further ...

  1. #1
    FOX
    Join Date
    May 2005
    Posts
    188

    cin.get on an empty line and cin.clear

    In my book, C++ Primer Plus 4th Edition, it says that cin.get(buf, 80) will set the failbit and block further reading if it encounters an emtpy line (just a newline in the stream).

    So in order to use cin again, you should use cin.clear(). Why doesn't this simple code work then (it should wait for the user to enter something at the end):
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            int i;
            char buf[80];
    
            cout << "Enter an integer: ";
            cin >> i;
    
            cout << "You entered: " << i << endl;
            cin.get(buf, 80); // Just an example, I know you can use cin.get() here.
            cin.clear();
            cin.get();
    
            return 0;
    }
    It works if I add cin.ignore(), but there shouldn't be anything left in the stream since I read the trailing newline with cin.get(buf, 80)?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use cin.ignore(1);

    clear gets rid of the failbit, but the newline stays in the stream, so the next cin.get() just picks it up...

    get leaves the trailing newline in the stream. use getline() to take it out of the stream:
    Code:
    getline(buf,80,'\n');
    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

  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    Oh man I'm stupid heh. I actually knew that cin.get(buf, 80) doesn't read the trailing newline, but I just couldn't put one and two together.

    Thanks!

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    not a problem
    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

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Here's a little thing that might prove useful:
    Code:
            cout << "Enter an integer: ";
            cin >> i;
    
            cout << "You entered: " << i << endl;
    	cin.sync();
            cin.get();
    Hope it helps,
    Brian

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Br5an
    Here's a little thing that might prove useful:
    Code:
            cout << "Enter an integer: ";
            cin >> i;
    
            cout << "You entered: " << i << endl;
    	cin.sync();
            cin.get();
    Hope it helps,
    Brian
    why would that prove useful?
    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

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Because cin.clear() isn't doing what ^xor's book claims, from what I can tell, and cin.sync() might be just what he wants.
    Regards,
    Brian

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    cin.clear() is clearing the flags... what his problem was a return character left in the stream by cin.get()... I still don't see how sync() would help...
    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

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    cin >> i; also leaves a return character in the stream which is picked up by the final cin.get( ) in my code. Remove cin.sync( ) and see for yourself. I was just compacting the exercise a bit. BK

  10. #10
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    wow kinda flamy in here *unsubscribes*

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    those aren't flames... I was just asking because I've never used sync()...
    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

  12. #12
    FOX
    Join Date
    May 2005
    Posts
    188
    cin.clear() was doing exactly what the book claimed. The only problem was that I for some unexplainable reason thought that cin.get(buf, 80) would read in the trailing newline (the one thing it won't do lol).

    This will work:
    Code:
    cin >> i;
    cin.get(buf, 80); // Set the failbit (just experimenting with it)
    cin.clear(); // Set failbit to false again
    cin.get(); // Get rid of the trailing newline. Can use cin.ignore() instead.
    cin.get(); // Wait for user input

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I would recommend using cin.ignore(1); in place of cin.get() like you mentioned in your comments... it just makes what you're doing more explicit...
    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

  14. #14
    FOX
    Join Date
    May 2005
    Posts
    188
    Will cin.ignore() without any params clear the entire input buffer, or just one character?

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just one character (IIRC).

    if you want, you could try something like cin.ignore(32000); (32k is about the limit for an int), but I generally stick with a 1 in there.
    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

Similar Threads

  1. EOF / cin.clear()
    By Decrypt in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2006, 02:29 PM

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