最小
void read_inputs(studentsT array[],int size,FILE* fp,int *males,int *females,double *malesP,double *femalesP){
int nscan,i,antres,gynekes;
double grades[6],avg,sum;
char name[15],surname[25],gender,termch;
antres = gynekes =0;
*males = 0;
*females = 0;
*malesP = 0;
*femalesP = 0;
while(true){
nscan = fscanf(fp,"%14[^,], %25[^,], %lf, %lf, %lf, %lf, %lf, %lf, %c%c",name,surname,&grades[0],&grades[1],&grades[2],&grades[3],&grades[4],&grades[5],&gender,&termch);
if (nscan==EOF)break;
if (nscan != 10 || termch != '\n')
printf("ERROR");
if (gender == 'A')
antres++;
else
gynekes++;
sum = 0;
sum = sum + grades[0]+grades[1]+grades[2]+grades[3]+grades[4]+grades[5];//makes a sum of grades
avg = sum /6; //find the avg
for (i=0; i<size;i++){
if (avg >= 10){ //if the avg is greater than 10, it gives the values it read to a struct
array[i].avg = avg;
strcpy(array[i].onoma,name);//copies the name to struct member
strcpy(array[i].epitheto,surname);//copies the surname to struct member
array[i].grades[0] = grades[0];//copies the grades
array[i].grades[1] = grades[1];
array[i].grades[2] = grades[2];
array[i].grades[3] = grades[3];
array[i].grades[4] = grades[4];
array[i].grades[5] = grades[5];
strcpy(&array[i].sex,&gender);
if (gender == 'A')
++*males; //counts males with avg more than 10
else
++*females; //counts females with avg more than 10
}
}
}
*malesP = (*males * 100)/antres; //percentage of males with more than 10 avg
*femalesP= (*females * 100)/gynekes; //percentage of females with more than 10 avg
}
我的问题是它应该用大于10 avg的人的信息填充结构,然后将其打印在一个新文件中,当我在main中打印它以进行检查时,结构没有任何信息一切都在其中。
答案 0 :(得分:0)
以下代码包含注释
由于缺少typedefstudentT
的定义,无法完全编译
将执行所需的操作。
问题的根源是即使调用array[]
失败,也会设置每个fscanf()
条目
#include <stdio.h>
void read_inputs(
studentsT array[],
int size,
FILE* fp,
int *males,
int *females,
double *malesP,
double *femalesP)
{
int nscan;
int i;
int antres = 0;
int gynekes = 0;
double grades[6];
double avg;
double sum;
char name[15];
char surname[25];
char gender;
char termch;
*males = 0;
*females = 0;
*malesP = 0;
*femalesP = 0;
int i = 0;
while( (i < size) && (10 == (nscan = fscanf(fp,
"%14[^,], %24[^,], %lf, %lf, %lf, %lf, %lf, %lf, %c%c",
name,
surname,
&grades[0],
&grades[1],
&grades[2],
&grades[3],
&grades[4],
&grades[5],
&gender,
&termch) ) ) )
{
if (gender == 'A')
antres++;
else
gynekes++;
sum = grades[0]+grades[1]+grades[2]+grades[3]+grades[4]+grades[5];//makes a sum of grades
avg = sum /6.0; //find the avg
if (avg >= 10)
{ //if the avg is greater than 10, it gives the values it read to a struct
array[i].avg = avg;
strcpy(array[i].onoma,name);//copies the name to struct member
strcpy(array[i].epitheto,surname);//copies the surname to struct member
array[i].grades[0] = grades[0];//copies the grades
array[i].grades[1] = grades[1];
array[i].grades[2] = grades[2];
array[i].grades[3] = grades[3];
array[i].grades[4] = grades[4];
array[i].grades[5] = grades[5];
strcpy(&array[i].sex,&gender);
if (gender == 'A')
(*males)++; //counts males with avg more than 10
else
(*females)++; //counts females with avg more than 10
}
i++;
} // end while
*malesP = ((*males) * 100.0)/antres; //percentage of males with more than 10 avg
*femalesP= ((*females) * 100.0)/gynekes; //percentage of females with more than 10 average
} // end function: main