Thread: EOF in a while loop for arrays

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    9

    EOF in a while loop for arrays

    Hi,

    I dont know how many numbers will be the input only that its going to be up to 10000. EOF should be active. I tried it like this:

    Code:
    int i = 0;
    int del[10000]
    
    while (scanf("%d",del[i])!=EOF) 
    {
      i++;
    }
    But it seems the value of i doesn't increment, could you provide some tips on how to scanf the inputs into an array if I don't know how many numbers will I have?

    Thanks a lot

  2. #2
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    Where is del initialized?
    You have declared del as an array of 10000 int's but have put no values into it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    With so many syntax errors, who can say.

    Missing ; and & all over the place.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    Oh sorry, now I'm very embarrased (and even more so because I can't figure out how to edit my original post). I didnt copy and paste, just written it of the top of my head (seems I'm lost without compiler warnings and errors) :
    Code:
    int i = 0;
    int del[10000];
    
    while (scanf("%d",&del[i])!=EOF)
    {
      i++;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest a comparing with 1 rather than EOF, since you expect one assignment from scanf on each iteration:
    Code:
    int i = 0;
    int del[10000];
    
    while (scanf("%d", &del[i]) == 1)
    {
        i++;
    }
    Next, how do you know that i is not incrementing? Did you print it out? What is the input that you provided for the testing? Did you redirect it from a file, or did you trigger EOF (e.g., with CTRL + Z or CTRL + D)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    Hi yes I printed it out. I put in some 7 random numbers myself, triggered EOF and printed out the value of i which was 1. I am obviously a total newbie with C and programming in general so the question might be dumb but if I use your code is EOF going to be active?

    Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I tested with this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i = 0;
        int j;
        int del[10000];
         
        while (i < 10000 && scanf("%d", &del[i]) == 1)
        {
            i++;
        }
    
        printf("These are the entries:\n");
        for (j = 0; j < i; ++j)
        {
            printf("%d\n", del[j]);
        }
        printf("Total of %d entries.\n", i);
    
        return 0;
    }
    Entering this input followed by triggering EOF:
    Code:
    1 2 3 4 5 6 7
    I received this output:
    Code:
    These are the entries:
    1
    2
    3
    4
    5
    6
    7
    Total of 7 entries.
    EDIT:
    Quote Originally Posted by Snowflake
    if I use your code is EOF going to be active?
    EOF means "end of file". If the end of file has been reached, then there will not be any more input from that input stream, so when you attempt to read with scanf, you will get EOF (the value, not the condition) returned. EOF != 1, so the loop will end. While you could compare with EOF instead of 1, with more complex format strings that might be undesirable, or it might even be possible for scanf to return 0.
    Last edited by laserlight; 11-24-2014 at 03:02 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    Brilliant, thank you very much... for now. I'm sure I will have some more question before finishing the programme. But again thanks, really appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Pointers in a loop
    By Or Ellenbogen in forum C Programming
    Replies: 3
    Last Post: 06-28-2013, 06:04 AM
  2. Help with arrays and while loop
    By tnyalc in forum C Programming
    Replies: 2
    Last Post: 03-15-2012, 03:20 PM
  3. Loop that contains arrays
    By christianB in forum C Programming
    Replies: 13
    Last Post: 07-20-2011, 07:58 AM
  4. help with for loop with arrays
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 11-03-2008, 06:42 PM
  5. Arrays and For Loop
    By Jack1982 in forum C++ Programming
    Replies: 9
    Last Post: 10-10-2007, 04:57 AM