0

I am trying to load elements into an array using a function but I don't know what I'm doing wrong. I want to load elements from a data file until -1 is entered. Here is what I have and I don't know what to do from here.

#include <stdio.h>

/*Function to scan in grades*/
int LoadArray (int grade[ ])
{
    int i = 0;
    while(scanf("%i", grade[i]) != -1) {
        i++;
    }
    return i;
}


/*Main program*/
int main (void)
{
    int grade[200], count=0;
    /*Call function*/
    count = LoadArray(grade);
    printf("%i", count);
    return 0;

}
8
  • What problems do you have with the code you show? If you enable more warnings from the compiler, what does the compiler say (a good compiler will give you warnings about your code)? Oh, and I recommend you check e.g. this scanf (and family) reference, pay close attention to the argument types in the format-code table (this is really important!). Commented Nov 15, 2015 at 17:18
  • What makes you think that scanf() would return -1 at some point? is t EOF that you mean? Commented Nov 15, 2015 at 17:19
  • 1
    while(scanf("%d", &grade[i])!=-1); Commented Nov 15, 2015 at 17:21
  • @milevyo: better without trailing ;! Commented Nov 15, 2015 at 17:22
  • 2
    while(i < 200 && 1==scanf("%i", &grade[i]) && grade[i] != -1) { Commented Nov 15, 2015 at 17:22

2 Answers 2

2

Two problems.

1) You're missing an ampersand:

scanf("%i", &grade[i]);

you need to pass the address of the variable scanf should put the result in. You were passing the contents of grade[i] instead, and reading uninitialized memory is undefined behavior.

2) scanf doesn't return the value it read from stdin; you should compare the variable you read to -1. Also, it's bad practice to pass an array to a function but not its size, as then the function has no way of knowing how big the array is.

To sum up, the code with fixes looks like this:

/*Function to scan in grades*/
int LoadArray(int grade[], size_t gradeMaxCount)
{
    int i = 0;

    while (1)
    {
        int tmp;
        if (i >= gradeMaxCount || scanf("%i", &tmp) != 1 || tmp == -1)
            break;

        grade[i++] = tmp;
    }

    return i;
}

Call the function like this:

count = LoadArray(grade, sizeof(grade) / sizeof(grade[0]));

Note that funny things will happen if i somehow becomes negative. You could make it size_t instead (this would also require you to change the function return type and print it using %zu), depending on how much of a purist you are.

Sign up to request clarification or add additional context in comments.

1 Comment

@BLUEPIXY Good catch, fixed.
-1
int LoadArray (int grade[ ])
{
    int i = 0;
    for(int j=0;j<200;j++){
       if((scanf("%d", grade+j) == -1) || (grade[j]==-1))
            break;

        i++;
    }
    return i;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.