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

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

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

    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';

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What?!

    The current code will change the last valid character into NUL. Not what you want, infact you don't want anything.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i want to keep this loop doing what it does but
    without EOF

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It already does... perhaps brush up on your loops.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so i can delete this EOF condition?

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No... because the condition stops you from putting EOF in the array.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this loop works fine

    but i cant use the term EOF

    i am looking for a way to replace it

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Why? Because your teacher says "You can't use that"? If so, remove it - but be aware that it IS correct to have EOF there.

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

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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';

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    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';
    That is what EOF means - something went wrong when reading the next character [most often because you reached end of file or pressed CTRL-D/CTRL-Z on the keyboard - but anything that "fails" the read itself will return EOF from getchar()].

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

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

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    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
    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. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how can i recieve EOF in the running of a program?
    or in the string input part??

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you actually read? I am having my doubts here...
    You only seem to notice what you want and ignore everything else...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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, 07: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, 04:45 PM