#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];
我得到了带有10个学生的
inf.txt
,ID
,NAME
,LAST NAME
,HOMEWORK GRADE
和MIDTERM
的FINAL GRADE
文件。 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;
我在if-else if部分的赋值错误中遇到了不兼容的类型
fprintf(fp2,"%d %-12s %-12s %3d %3s",rec.id,rec.name,rec.lname,numgrade,letgrade);
}
fclose(fp1);
fclose(fp2);
system("pause");
return 0;
}
我在SOF中的赋值错误中搜索了不兼容的类型,但是找不到对我的代码有用的东西。
最佳答案
你宣布
char letgrade[3];
作为数组。在C语言中,无法使用
=
运算符分配数组。可以分配指针,但是您需要管理指针指向的内存。如果要将两个字符串连接成
letgrade
,请使用以下代码:strcpy(letgrade, a5); // Copy the first part
strcat(letgrade, a5); // Append the second part
请注意,为了使上述代码正常工作,
a5
的长度不得超过1
。否则,strcat
将写在letgrade
的末尾。