Thread: End Of File (EOF) Statement

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    End Of File (EOF) Statement

    Hello everyone,
    I tried to enter variable as much as I want and at the end, I want program to show how many variable I entered with EOF statement(or function. I dont know formally). But I get wrong outputs. And I couldn't reveal my fault can you please tell me?

    Additionally, I want some more in output. I want program to display variables I entered as output. For example:

    Input: 1 2 3
    Output: 1 2 3

    Input: 10 9 54 67 11 2
    Output: 10 9 54 67 11 2
    Attached Images Attached Images End Of File (EOF) Statement-4-jpg 
    Last edited by erdemtuna; 11-29-2015 at 01:03 PM. Reason: attachment

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    EOF is a constant; specifically, it is a macro declared via #define.

    Also, there is nothing wrong with your output. Each digit is its own character.

    Take screen 3 for example. 1 zero, 2 ones, 1 two, 1 three, 1 four, 1 five, and 6 newlines (\n). That's a total of 13. Maybe you wanted to use scanf() instead?

    Additionally, I want some more in output. I want program to display variables I entered as output.
    Is that not what is happening, or do I misunderstand?
    Last edited by whiteflags; 11-29-2015 at 01:15 PM.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Quote Originally Posted by whiteflags View Post
    Maybe you wanted to use scanf() instead?
    How can I use scanf to count the quantity of entered variables?


    Quote Originally Posted by whiteflags View Post

    Is that not what is happening, or do I misunderstand?
    I think you misunderstood a little bit. Because in this code, I want my inputs to be counted and output it as how many variable I entered.

    What I want further is it will output my inputs as they are.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by erdemtuna View Post
    Because in this code, I want my inputs to be counted and output it as how many variable I entered.

    How can I use scanf to count the quantity of entered variables?
    As I said before, the input is correctly counted - the output is just not what you expect. Each digit is its own character, as I explained before. I hope that is clear. scanf can be used to improve the output, so it is more like you expect.

    Normally scanf will analyze the input and try to match it with the conversions specified in the first argument. When there is a match, the value is stored in the corresponding pointer to a variable. Once that is understood, then the next thing is to control the loop using the return value of the function.

    So, as an example, let's recreate the broken getchar version using scanf and try to understand its meaning. This may help us realize where we went wrong.
    Code:
    while (scanf("%c", &c) == 1) {
       b += 1;
    }
    This will take every character and store it in c. The characters are counted in b.

    So if the user enters:
    10
    20
    30
    ^Z

    The result is 9, because of each digit and the three newline characters.

    The return value of scanf is different too, as you've no doubt realized. If we turn to the manual page though, there is a clear explanation for that.
    [scanf] return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

    The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.
    By my reading of this, since we have one thing we want to match, the easiest way to stop the loop is at the point scanf() does not return 1.

    But the output is not what you expect. Luckily, scanf can analyze the input in more ways than getchar can. The easiest, and most permissive way to change scanf is to look for integers, instead:
    Code:
    while (scanf("%i", &c) == 1) {
       b += 1;
    }
    So now this counts integers - the input:
    10
    20
    30
    ^Z
    would make 3.
    Last edited by whiteflags; 11-29-2015 at 02:03 PM. Reason: == is not !=

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Thanks whiteflag, I got it. If possible, can I steal your time some more? I'm novice in programming, and I have studied till functions on Deitel book so far, is it possible for me to understand and code a program that gives same as your input? Or at this time I don't have enough knowledge?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you post on this forum, I am generally available.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing an fprintf statement toa file as text
    By gfotedar in forum C Programming
    Replies: 11
    Last Post: 02-06-2012, 09:44 AM
  2. reading file then if statement not working
    By patrink in forum C Programming
    Replies: 3
    Last Post: 12-17-2010, 01:48 AM
  3. Batch File Selection Statement- If
    By heylin in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-06-2005, 10:50 AM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

Tags for this Thread