is there a way to replace EOF in this loop..

This is a discussion on is there a way to replace EOF in this loop.. within the C Programming forums, part of the General Programming Boards category; Originally Posted by transgalactic2 this loop works fine but i cant use the term EOF i am looking for a ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by transgalactic2 View Post
    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. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    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
    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.

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    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
    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.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yep

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

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote 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

    Quote 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".
    Last edited by laserlight; 01-05-2009 at 04:41 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    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. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Actually, read my edit. The "cheat" is guaranteed to work (but that does not make it any less of a cheat).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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 04:52 AM.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  14. #29
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    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.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    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
    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.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Replies: 1
    Last Post: 11-19-2001, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21