-
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?
-
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.
-
> 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.
-
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.
-
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.
???
-
> 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).
-
clear() should clear the eofbit.
-
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.