EOF is only guaranteed to be negative, but that means that comparing for "less than 0" will do.Originally Posted by Adak
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
But that is REALLY not the right solution. If the original poster is not supposed to use EOF, then comparing with negative values is just cheating - you are assuming that EOF is a negative number [I'm not sure the spec says so - it probably does], and that anything else ever coming out of getchar that is negative is a failure.
I'd like to tell the teacher that set these rules that he/she is wrong in forbidding correctly written code (presumably "because we haven't talked about it yet") - it's one thing if the code is "cheating" by for example using more advanced functions when the student is supposed to learn how to do it at a lower level.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
did i substituted correctly the EOF condition
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i) { input2[i] = ch; } input2[i] = '\0';
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
yep
so in a case of getchar error
this will work fine?
Yes, it is cheating, but I am in Adak's good companyOriginally Posted by matsp
Ah yes, that is true: it is guaranteed by the standard that EOF is negative, but it is not guaranteed that anything that is negative is EOF. Consequently, the "cheat" is not guaranteed to work.Originally Posted by matsp
EDIT:
Oops, that is not true: the C standard does guarantee that anything that is negative is EOF, since "the fgetc function obtains that character as an unsigned char converted to an int" and "the getc function is equivalent to fgetc" and "the getchar function is equivalent to getc with the argument stdin".
Last edited by laserlight; 01-05-2009 at 05:41 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
what do you negative number is a failure
"
[I'm not sure the spec says so - it probably does], and that anything else ever coming out of getchar that is negative is a failure."
if i am going to enter a negative number into my string it will stop the input?
If you enter a negative number as text, no. It would merely be interpreted as the characters of a string, within the ASCII range.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Actually, read my edit. The "cheat" is guaranteed to work (but that does not make it any less of a cheat).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thanks
the final nail in a coffin
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i) { input2[i] = ch; } input2[i] = '\0';
Last edited by transgalactic2; 01-05-2009 at 05:52 AM.
comparing with -1 will simply EXPECT EOF to be -1 (which it probably is on 98% or more of all compiler&C Library implementations). Since getchar() is just a wrapper around fgetc() and fgetc() returns only positive numbers for valid characters, and EOF is the only defined negative value, comparing with >=0 should be fine.
However, you are still doing an EOF comparison - just avoiding the name EOF. Which is why we are calling it a cheat. Yes, it will work - but it's harder to understand, and you are still "using EOF" - just not by name. (And NOT using EOF means that you are actually hiding what that part of the code does - making it harder to understand. For example, the reader must know that EOF is a negative number).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You could do this, without assuming anything about the range of char
Throw in an ferror() for good measure.Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' ; ++i) { if ( feof(stdin) ) break; input2[i] = ch; }
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.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.