2016-01-12 34 views
-3

最小:結構不打印上新的文件

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的平均信息,然後在打印新文件,事情是,當我打印在主要檢查的目的,結構中沒有任何信息。

+0

你所說的「任何信息」的意思是設定?零?垃圾?嘗試使用調試器。設置一箇中斷點並逐步執行應用程序,並在使用Watch Window時檢查所有變量。 –

+0

爲可讀性和易於理解,始終縮進代碼。切勿使用製表符來縮進。在每個大括號'{'後縮進。在每個大括號之前取消縮進'};單獨的代碼塊(對於,如果,其他,同時,做...時,切換,大小寫,默認)通過一個空行。請遵循以下公理:對於這兩條線,每行只有一條語句(至多)一條語句 – user3629249

+0

的變量聲明:while(true)nscan = fscanf(fp,「%14 [^,],%25 [ ^,],%lf,%lf,%lf,%lf,%lf,%lf,%c%c「,名字,姓氏和等級[0],等級[1],等級[2],等級[3 ],和等級[4],等級[5],和性別,和學期);'應該是:'while(10 ==(nscan = fscanf(fp,「%14 [^,],%25 [^,], lf,%lf,%lf,%lf,%lf,%lf,%c%c「,姓名,等級[0],等級[1],等級[2],等級[3],等級[4 ],&grades [5],&gender,&termch)){'但是在while()語句中還需要檢查是否沒有超出array []中的最大條目數 – user3629249

回答

0

下面的代碼結合的評論

不乾淨編譯由於缺乏的typedef studentT

將執行所需的操作的定義。

問題的根源是每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