質問

#include <stdio.h>
#include <stdlib.h>

struct not{
int id,hw,mdt,fnl;
char name[20];
char lname[20];
        }rec;


int main(){
FILE *fp1,*fp2;
char a1[3]="A",a2[3]="B",a3[3]="C",a4[3]="D",a5[3]="F";
float numgrade;
char letgrade[3];

I got inf.txt file with 10 student's ID,NAME,LAST NAME,HOMEWORK GRADE,MIDTERM AND FINAL GRADE.

fp1=fopen("inf.txt","r");
fp2=fopen("outf.txt","w");
   while( !feof(fp1)){
fscanf(fp1,"%d %s %s %d %d %d\n",&rec.id,rec.name,rec.lname,&rec.hw,&rec.mdt,&rec.fnl);
numgrade = (0.15)*rec.hw + (0.35)*rec.mdt + (0.5)*rec.fnl;     

I got incompatible types in assignment error at if-else if part

 if(numgrade>=0 && numgrade <=40) letgrade=strcat(a5,a5);
 else if(numgrade>=41 && numgrade<=45) letgrade=strcat(a4,a4);
 else if(numgrade>=46 && numgrade<=52) letgrade=strcat(a4,a3);
 else if(numgrade>=53 && numgrade<=60) letgrade=strcat(a3,a3);
 else if(numgrade>=61 && numgrade<=69) letgrade=strcat(a3,a2);
 else if(numgrade>=70 && numgrade<=79) letgrade=strcat(a2,a2);
 else if(numgrade>=80 && numgrade<=89) letgrade=strcat(a2,a1);
 else if(numgrade>=90) letgrade=strcat(a1,a1);
fprintf(fp2,"%d %-12s %-12s %3d %3s",rec.id,rec.name,rec.lname,numgrade,letgrade);

}
fclose(fp1);
fclose(fp2);
system("pause");
return 0;
}

I searched incompatible types in assignment error in SOF but couldnt find something useful for my code.

役に立ちましたか?

解決

You declared

char letgrade[3];

as an array. In C, arrays cannot be assigned with = operator. Pointers can be assigned, but you would need to manage memory pointed to by the pointers.

If you would like to concatenate two strings into letgrade, use the following code:

strcpy(letgrade, a5); // Copy the first part
strcat(letgrade, a5); // Append the second part

Note that in order for the above code to work properly, the length of a5 must not exceed 1. Otherwise, strcat would write past the end of letgrade.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top