Adrian Parker wrote:
>
> When will the newline character in an int not be 10?
On any system that uses a character set in which it is something else.
Why assume it will be 10? It only serves to decrease portability and
clarity.
>>
>>I think you could probably do better with something like this:
>>
>>while (!(cin >> marbles))
>>{
>> if (cin.eof() || cin.bad())
>
>
> I'm told that cin.eof is a bad practice, although I do not understand why.
>
It's bad to loop on cin.eof(), but checking it after an error has
occurred is good. You don't loop on it because it may not be true at the
time you expect it to be. After reading the last character, the flag
might not yet be set. Instead, it will be set after you attempt to read
one more (non-existent) character.
There's a FAQ entry about this, if you haven't read it:
http://www.parashift.com/c++-faq-lit....html#faq-15.5
>
>
> If it's user input though, why worry about EOF? The user can as easily hit
> C-c as C-z (Windows bindings anyway).
>
If you think it's acceptable for your program to force the user to
terminate the program in an unusual way, and enter an infinite loop if
they try to terminate the input in the usual way, or if they pipe in a
file, then I suppose you can do that. I consider it sloppy and wouldn't
do it myself, nor would I want to use a program that behaves that way.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.