Thread: EOF / cin.clear()

  1. #1
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300

    EOF / cin.clear()

    I'm having Windows/linux EOF issues.

    Given the snippet:
    Code:
    std::string sWord;
    while (std::cin >> sWord)
    {
    //blah blah blah
    }
    std::cin.clear();
    When I build the program on Windows XP (Code::Blocks), I have no problems. I am able to retrieve input from the keyboard after this block.

    When I build the program on the school's computers (I'm sorry, I don't know what the OS is, some *NIX...we bypass the Debian login, so I'm not sure.) the cin.clear() doesn't seem to really be resetting the stream, the program shoots through the next input (which is to ask whether to do it all again) and exits.

    I've tried cin.ignore(1000,'/n') with the cin.clear(), but to no avail. Maybe I'm just too tired to see an obvious solution, but I need help. Anyone have an idea?
    There is a difference between tedious and difficult.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you std::cout << std::cin.get(); after the loop breaks, can you check if it prints out a value without pausing? If it does, this could indicate the presence of some trash in the stream, and would warrant some ignoring. It is a peculiar problem however, and there really should not be any problems I don't think. P.S. Black Keys are awesome, The Moan was the first music of theirs I got. Tee hee.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I've tried cin.ignore(1000,'/n')
    Did you really?

    Or did you try this
    cin.ignore(1000,'\n')
    which is completely different, and should work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Did you really?
    Unbelievable. I think I did. I can't access the school's computer right now, and I'd have to check to make sure, but the file on my Windows box says I did.

    I didn't get a 'multi-character constant' warning at all like I usually do when I do something boneheaded like that. Hmmm. I'll see if that works once they get the computers back up. Damn good eye, Salem, thanks.

    Tonto, I'll try that, too, once I get access to the grid at school. It's true, The Black Keys are awesome, I recently saw them play in Madison; it was a fun show.
    There is a difference between tedious and difficult.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    OK, scratch that. On the school's version, the newline character was typed correctly. I tried Tonto's suggestion, and it outputs -1. I did a few cin.get()s in a row and they all came back that way. Even with:
    Code:
    std::cin.clear();
    std::cout << std::cin.get();
    std::cout << std::cin.get();
    std::cout << std::cin.get();
    std::cout << std::cin.get();
    std::cout << std::cin.get();
    std::cin.ignore(1000, '\n');
    std::cin.ignore(1000, '\n');
    It still does the same thing. It outputs a -1 for each std::cin.get(), then continues to act as if the stream is in a fail state.

    ???
    There is a difference between tedious and difficult.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It outputs a -1 for each std::cin.get()
    That sounds like they're returning EOF then.

    Which is what I would expect having pressed ctrl-d or ctrl-z to get out of the loop.

    On the face of it, clear() should be resetting the end of file state, but perhaps it on clears errors and there is some other method for resetting end of file state (so you can read from cin again).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    clear() should clear the eofbit.

  8. #8
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    That's always what I've used in the past, and I've never had a problem before.

    I've been thinking, and maybe the problem is outside of the code. I haven't tried running this while sitting in front of the school's computer; I use telnet to access it. Could that have an effect on the EOF signal I send with Ctrl-D? I won't be able to test that theory until early next week.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM