yiwll
yiwll

Reputation: 65

Populate Arrays of Structures from a File

I have a .dat file filled with ints and doubles that I need to populate an array of structures with, and I'm having issues figuring out the syntax.

Here is the file:

9383      8.86
2777     69.15
7793     83.35
5386      4.92
6649     14.21
2362      0.27
8690      0.59
7763     39.26
540     34.26
9172     57.36
5211     53.68
2567     64.29
5782     15.30
2862     51.23
4067     31.35
3929     98.02
4022     30.58
3069     81.67
1393     84.56
5011     80.42
6229     73.73
4421     49.19
3784     85.37
5198     43.24
8315     43.70
6413     35.26
6091     89.80
9956     18.73
6862     91.70
6996     72.81

Here is my code:

typedef struct student
{
    double score;
    int id;
    char grades;
} Student;

int getScores(FILE *input, Student class);

void main(void)
{
    char filename[] = "scores.dat";
    FILE *input;
    Student class[MAXNUM];
    int numScores;
    double average;

    input = fopen("scores.dat", "r");

    if (input == NULL)
    {
        printf("EOF");
        exit(1);
    }
}


int getScores(FILE *input, Student class)
{
    double s;
    int i, count = 0;

    while(fscanf(input, "%d %lf", &i, &s) == 2)
    {
        class[count].score = s;
        class[count].id = i;
        count++;
    }

My main issue lies in the while loop where I am trying to populate the array with the int and doubles of the scores.dat file.

I get the following error when I compile:

lab5.c:53:8: error: subscripted value is neither array nor pointer nor 
vector
class[count].score = s;
    ^
lab5.c:54:8: error: subscripted value is neither array nor pointer nor 
vector
class[count].id

Thank you for any help or tips.

Upvotes: 1

Views: 325

Answers (1)

VietHTran
VietHTran

Reputation: 2318

Just change function getScores definition and declaration argument class from

`int getScores(FILE *input, Student class);` 

to

`int getScores(FILE *input, Student *class);` 

Upvotes: 1

Related Questions