I am currently working on a program that takes input from a file and calculates the averages. I decided to just write the program they way I "thought" I could do it.
struct student{
char name[15];
double average;
double score1,score2,score3;
};
struct student num_students[50];
int main (){
int i = 1; /* Array Counter */
int total = 0; /* Counter for Total Students */
FILE *input;
input = fopen ("grades_file","r");
if ( input == NULL ){
perror ("The File grades_file could not be opened\n");
}
while ( feof(input)== 0){
fscanf ( input, "%s", num_students[i].name );
fscanf ( input, "%lf", &num_students[i].score1 );
fscanf ( input, "%lf", &num_students[i].score2 );
fscanf ( input, "%lf", &num_students[i].score3 );
num_students[i].average = (((num_students[i].score1)+(num_students[i].score2)+(num_students[i].score3))/3);
i++;
total++;
}
for ( i = 1; i < total; i++){
printf("%s ", num_students[i].name);
printf("%.2lf\n", num_students[i].average);
}
return 0;
}
So, now I have the data I need, but I can't wrap my head on how to pass the average member through a function. So I guess my question is: Can you assign a pointer to a member in a structure? Or am going in the wrong direction?
Thanks,
twburkle