Hello I have the following code hoping to build an array of pointers to structures and dinamically allocate strings within the structures. Any help will suffice!
Code:
#include "v4.h"
int main()
{
int i;
int num;
studentRecord *student;
scanf("%d",&num);
for(i = 0; i < num; i++)
{
student = (studentRecord *) malloc (sizeof(studentRecord));
scanf("%s %s %s %d %d %d",student->lastName, student->firstName,
student->idNum, &student->exam1, &student->exam2,
&student->exam3);
student->avg = (student->exam1 + student->exam2 + student->exam3)/3.;
if( student->avg >= 90.0 ) student->grade = 'A';
else if( student->avg >= 80.0 ) student->grade = 'B';
else if( student->avg >= 70.0 ) student->grade = 'C';
else if( student->avg >= 60.0 ) student->grade = 'D';
else student->grade = 'F';
printf("%s, %s: %s, %s \n\t %4d %4d %4d %s %6.2f %s %c\n",
student->lastName, student->firstName, student->idNum,
"has exams scores of:", student->exam1, student->exam2,
student->exam3, "for an average of ", student->avg,
" and a grade of ",student->grade);
}
return 0;
}
v4.h file
Code:
#include <stdio.h>
typedef struct {
char lastName[30];
char firstName[30];
char idNum[9];
int exam1;
int exam2;
int exam3;
float avg;
char grade;
}studentRecord;