求助c中参数相关问题
#include"stdio.h"int change(struct student *p, long num, int n, int *i, float *score);
#define NUM 3
struct student
{
char name;
long num;
float math, English, computer;
float average;
}students;
int main ()
{
int i, n, pos, a = {0};
float score = {0};
char choice;
long num1;
printf("请输入n为学生:");
scanf("%d", &n);
printf("\t\t数学\t英语\t计算机\n\n");
for(i = 0; i < n; i++)
{
printf("请输入第 %d 位同学成绩:", i + 1);
scanf("%f%f%f", &students.math, &students.English, &students.computer);
students.average = (students.math + students.English + students.computer) / 3;
}
printf("是否修改成绩(y/n):");
scanf("%c", &choice);
if(choice == 'y')
{
printf("请输入序号修改成绩 1-数学 2-英语 3-计算机\n\n");
}
while(choice == 'y')
{
printf("请输入要修改的学生学号:\n");
scanf("%d", &num1);
for(i = 0; i < 3; i++)
{
a = i + 1;
printf("请输入修改后的分数:");
scanf("%f", score);
}
printf("是否修改成绩(y/n):");
scanf("%c", &choice);
pos = change(students, num1, n, a, score);
}
//打印
if(pos == -1)
{
printf("NOT FOUND!\n");
}
else
{
printf("After Update, the new information:\n");
printf(" 姓名\t 学号 \t数学\t英语\t计算机\n\n");
printf("%s\t%d\t%f\t%f\t%f\t\n\n", students.name, students.num, students.math, students.English, students.computer);
printf("该生平均分为:%f\n", students.average);
}
return 0;
}
int change(struct student *p, long num, int n, int *i, float *score) //编译器在这儿报错,说参数不兼容,不太明白,求指教。
{
int j, pos;
for(j = 0; j < n; j++)
{
if(p->num == num)
{
break;
}
p++;
}
if(j < n)
{
for(j = 0; j < NUM; j++)
{
switch(i)
{
case 1 : p->math = score; break;
case 2 : p->English = score; break;
case 3 : p->computer = score; break;
}
}
pos = j;
}
else
{
pos = -1;
}
return pos;
}
这是源码,问题写在上面,参数不兼容,求指教 你在什么IDE环境下编译的? 我用vs2012没有编译错误。
不过,我建议你将 第二行int change(struct student *p, long num, int n, int *i, float *score);放在struct student结构体声明之后,int main()函数之前试试 alexw_h264 发表于 2020-2-10 17:02
你在什么IDE环境下编译的? 我用vs2012没有编译错误。
不过,我建议你将 第二行int change(struct student...
好的
谢谢
你的源程序还有不少错误。我这边做了修改。调试结果没有问题了。
#include"stdio.h"
int change(struct student *p, long num, int n, int *i, float *score);
#define NUM 3
struct student
{
char name;
long num;
float math, English, computer;
float average;
}students;
int main ()
{
int i, n, pos, a = {0};
float score = {0};
char choice;
long num1;
printf("请输入n为学生:");
scanf("%d", &n);
printf("\t\t数学\t英语\t计算机\n\n");
for(i = 0; i < n; i++)
{
printf("请输入第 %d 位同学成绩:", i + 1);
scanf("%f%f%f", &students.math, &students.English, &students.computer);
students.average = (students.math + students.English + students.computer) / 3;
students.num = i+1;
}
fflush(stdin);
printf("是否修改成绩(y/n):");
scanf("%c", &choice);
if(choice == 'y')
{
printf("请输入序号修改成绩 1-数学 2-英语 3-计算机\n\n");
}
while(choice == 'y')
{
printf("\n请输入要修改的学生学号:");
scanf("%d", &num1);
for(i = 0; i < 3; i++)
{
a = i + 1;
printf("请输入修改后的分数:");
scanf("%f", &(score));
}
pos = change(students, num1, n, a, score);
//打印
if(pos == -1)
{
printf("NOT FOUND!\n");
}
else
{
printf("After Update, the new information:\n");
printf(" 姓名\t 学号 \t数学\t英语\t计算机\n\n");
printf("%s\t%d\t%2.2f\t%2.2f\t%2.2f\t\n\n", students.name, students.num, students.math, students.English, students.computer);
printf("该生平均分为:%2.2f\n", students.average);
}
fflush(stdin);
printf("是否还需要修改成绩(y/n):");
scanf("%c", &choice);
}
return 0;
}
int change(struct student *p, long num, int n, int *i, float *score) //编译器在这儿报错,说参数不兼容,不太明白,求指教。
{
int id, j, pos;
for(id = 0; id < n; id++)
{
if(p->num == num)
{
break;
}
p++;
}
if(id < n)
{
for(j = 0; j < NUM; j++)
{
switch(i)
{
case 1 : p->math = score; break;
case 2 : p->English = score; break;
case 3 : p->computer = score; break;
}
}
p->average = (p->math + p->English + p->computer) / 3;
pos = id;
}
else
{
pos = -1;
}
return pos;
}
tt
页:
[1]