we havent learned files
there is no End Of File in this program
can i change it into '\0'
or some like that?
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
Printable View
we havent learned files
there is no End Of File in this program
can i change it into '\0'
or some like that?
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
What?!
The current code will change the last valid character into NUL. Not what you want, infact you don't want anything.
i want to keep this loop doing what it does but
without EOF
It already does... perhaps brush up on your loops.
so i can delete this EOF condition?
No... because the condition stops you from putting EOF in the array.
getchar() returns EOF on *error* or when it reaches the EOF.
So getchar() will return EOF, even if there *is* no EOF char at the end of the file! :)
So I believe you will want to keep EOF just where it is, in your for statement.
Can you show the input that the for statement is not handling right?
this loop works fine
but i cant use the term EOF
i am looking for a way to replace it
is there a way to state EOF in getchar() terms
like
Code:for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch !=(getchar error); ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
What will happen if i will remove this EOF condition regarding the string??
will it stop putting chars into the string and put '\0' like before
In most situations NOTHING will change. However, if you happen to receive an EOF, it will continue to fill the string with EOF (converted to a character). And unless some part of your code detects and exits (or does something else meaningful) on EOF, it will loop forever.
--
Mats
how can i recieve EOF in the running of a program?
or in the string input part??
Can you actually read? I am having my doubts here...
You only seem to notice what you want and ignore everything else...
EOF is only guaranteed to be negative, but that means that comparing for "less than 0" will do.Quote:
Originally Posted by Adak
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
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';
yep :)
so in a case of getchar error
this will work fine?
Yes, it is cheating, but I am in Adak's good company :pQuote:
Originally 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.Quote:
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".
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.Quote:
Originally Posted by transgalactic2
Actually, read my edit. The "cheat" is guaranteed to work (but that does not make it any less of a cheat).
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';
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
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;
}
Is this another one of those pointless "can you tie your shoelaces with one hand" type of assignments?
Yes, that's my understanding.
As I said earlier, it's fair to say "don't use functions that do the job for you, when you are learning the basics", but certainly "correct code that solves the problem in the way that the teacher expects" should not be rejected just because it uses some "we haven't got there in the book yet". Fair enough that you can't do strcpy() when learning about strings and your task is to copy a string - but to check if the result of getchar() is not EOF in a loop is valid, and not a "trick" to solve the problem easier - it's just more correct.
--
Mats
I really don't think the teacher forbids the use of EOF, since it is the only way of finding the end-of-file and/or an error. There is no other way around. The "tricks" and "cheatings" use EOF, they just don't type it...