Thanks spydoor..
Code:
#include <stdio.h>
struct A{
char name[50];
char id[8];
char year[8];
char grade[3];
};
struct Record{
char name[30];
long int id;
char year[8];
char major[20];
struct A course[6];
int credit_hours;
float gpa;
char deanlist[4];
};
int main()
{
struct Record student[13];
FILE *infile;
int i,j;
for(i=0;i<13;i++){
printf("Please enter student info\n");
fgets(student[i].name,30,stdin);
scanf("%ld %s %s %d %f %s",&student[i].id
,student[i].year
,student[i].major
,&student[i].credit_hours
,&student[i].gpa
,student[i].deanlist);
for(j=0;j<6;j++){
printf("Please enter course info\n");
fgets(student[i].course[j].name,50,stdin);
scanf("%s %s %s",student[i].course[j].id
,student[i].course[j].year
,student[i].course[j].grade);
}
}
if((infile=fopen("student.txt","w"))==NULL)
printf("Error\n");
else{
for(i=0;i<13;i++){
fprintf(infile,"%s %ld %s %s %d %.2f %s",student[i].name
,student[i].id
,student[i].year
,student[i].major
,student[i].credit_hours
,student[i].gpa
,student[i].deanlist);
for(j=0;j<6;j++){
fprintf(infile,"%s %s %s %s",student[i].course[j].name
,student[i].course[j].id
,student[i].course[j].year
,student[i].course[j].grade);
}
}
fclose(infile);
}
return 0;
}
Okay, i've changed to fgets to get a string of name for both loops.I think it works for the first loop, but not for the second loop.I can only enter 3 data instead of 4 data.Also, how to parse with sscanf?
One more question, for the second loop(course info), not every student will be taking 6 courses.They can be taking less than that,maybe 3 or 4 courses only.How can i stop the loop when i've entered enough amount of courses taken by the student?
Thanks alot for checking my codes here, u guys help me lots!