# Thread: is there a way to replace EOF in this loop..

1. Originally Posted by transgalactic2
this loop works fine

but i cant use the term EOF

i am looking for a way to replace it
Try replacing it with -1. iirc, that was the actual numerical value of EOF.

EOF is better to use, of course, but -1 may serve you well, if you can't use EOF.

2. Originally Posted by Adak
Try replacing it with -1. iirc, that was the actual numerical value of EOF.

EOF is better to use, of course, but -1 may serve you well, if you can't use EOF.
EOF is only guaranteed to be negative, but that means that comparing for "less than 0" will do.

3. Originally Posted by laserlight
EOF is only guaranteed to be negative, but that means that comparing for "less than 0" will do.
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

4. 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';```

5. Originally Posted by transgalactic2
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';```
Yes, that should work - but be aware that it's BAD code. But I guess it's a case of "anything to get past the teacher".

--
Mats

6. yep

so in a case of getchar error
this will work fine?

7. Originally Posted by matsp
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
Yes, it is cheating, but I am in Adak's good company

Originally Posted by matsp
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.
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.

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".

8. 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?

9. Originally Posted by transgalactic2
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.

10. Originally Posted by laserlight
Yes, it is cheating, but I am in Adak's good company

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.

so i need to say that ch differs -1 like ADAK said
to be sure??

11. Actually, read my edit. The "cheat" is guaranteed to work (but that does not make it any less of a cheat).

12. 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';```

13. 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

14. You could do this, without assuming anything about the range of char
Code:
```for (i = 0; i < 39 && (ch = getchar()) != '\n' ; ++i)
{
if ( feof(stdin) ) break;
input2[i] = ch;
}```
Throw in an ferror() for good measure.

15. Originally Posted by Salem
You could do this, without assuming anything about the range of char
Code:
```for (i = 0; i < 39 && (ch = getchar()) != '\n' ; ++i)
{
if ( feof(stdin) ) break;
input2[i] = ch;
}```
Throw in an ferror() for good measure.
But SURELY if you are not supposed to use EOF, then feof() is also bad, right? I mean the poor chap(ess) is not allowed to use fgets, hence the loop here [avoiding gets()].

--
Mats

Popular pages Recent additions