c -我想写一个程序来计算一个学生的结果,使用结构并打印位置



在这个程序中,我想使用结构获取学生信息并打印位置。在测试部分,只有最好的一个才会数到三。现在当我运行程序时,它不需要任何输入。它只要求学生的数量,然后它终止,不接受任何其他输入。谁能告诉我问题出在哪里?

struct student_profile
{
char name[100];
int ID[100];
int Final;
int mid;
int attendance;
int assignment;
int quiz1;
int quiz2;
int quiz3;
} s[100];

int comparison(int quiz1, int quiz2, int quiz3, int quiz_best)
{
if (quiz1>quiz2 && quiz1>quiz3)
{
printf("Best quiz mark is: n",quiz1);
quiz_best = quiz1;
}
else if (quiz2>quiz1 && quiz2>quiz3)
{
printf("Best quiz mark is: n",quiz2);
quiz_best = quiz2;
}
else
printf("Best quiz mark is: n",quiz3);
quiz_best = quiz3;
return quiz_best;
}

int main()
{
struct student_profile;
int i,n,quiz_best;
printf("Number of the student: n");
scanf("%d",&n);
for(i=0,i<n; i++;)
{
printf("nStudent No: %dn",i);
printf("Name: ");
scanf("%d", &s[i].name);
printf("ID: ");
scanf("%d",&s[i].ID);
printf("Final: ");
scanf("%d",&s[i].Final);
printf("mid: ");
scanf("%d",&s[i].mid);
printf("attendance: ");
scanf("%d",&s[i].attendance);
printf("assignment: ");
scanf("%d",&s[i].assignment);
printf("quiz1: ");
scanf("%d",&s[i].quiz1);
printf("quiz2: ");
scanf("%d",&s[i].quiz2);
printf("quiz3: ");
scanf("%d",&s[i].quiz3);
printf("n Students Informations n");
for(i=0,i<n; i++;)
{
printf("nStudent No: %dn",i);
printf("nName: %sn",s[i].name);
printf("nID: %dn",s[i].ID);
printf("nFinal: %dn",s[i].Final);
printf("nmid: %dn",s[i].mid);
printf("nattendance: %dn",s[i].attendance);
printf("nassignment: %dn",s[i].assignment);
printf("nquiz1: %dn",s[i].quiz1);
printf("nquiz2: %dn",s[i].quiz2);
printf("nquiz3: %dn",s[i].quiz3);
printf("nThe best quiz mark is: ",quiz_best);
comparison(s[i].quiz1,s[i].quiz2,s[i].quiz3,quiz_best);
}
}
} ```

你的if有问题

for(i=0,i<n; i++;)

应该

for(i=0;i<n; i++)

这解决了你的实际问题。

为了避免这种麻烦,使用使编译器更严格的标志进行编译。在gcc/clang中,有-Wall -Wextra -Werror,例如

你的代码有很多问题:

  • :

    中缺少大括号
    else
    printf("Best quiz mark is: n", quiz3);
    quiz_best = quiz3;
    

    应该是:

    else {
    printf("Best quiz mark is: n", quiz3);
    quiz_best = quiz3;
    }
    

main()函数中的struct student_profile;声明是无用的

scanf()使用%d而不是%s作为字符串值。

  • 你有嵌套循环相同的索引变量:for(i=0,i<n; i++;)是不正确的:它应该是for (i = 0; i < n; i++)

  • printf缺少%d来打印quiz_best变量。

  • quiz_best在计算前打印

  • comparison函数应该返回quiz_best,更新函数参数对调用者的变量没有影响。

下面是修改后的版本:

#include <stdio.h>
struct student_profile {
char name[100];
int ID[100];
int Final;
int mid;
int attendance;
int assignment;
int quiz1;
int quiz2;
int quiz3;
} s[100];
int comparison(int quiz1, int quiz2, int quiz3) {
int quiz_best;
if (quiz1 > quiz2 && quiz1 > quiz3) {
printf("Best quiz mark is: n", quiz1);
quiz_best = quiz1;
} else
if (quiz2 > quiz1 && quiz2 > quiz3) {
printf("Best quiz mark is: n", quiz2);
quiz_best = quiz2;
} else {
printf("Best quiz mark is: n", quiz3);
quiz_best = quiz3;
}
return quiz_best;
}
int main() {
int i, n, quiz_best;
printf("Number of students: n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("nStudent No: %dn", i);
printf("Name: ");
scanf("%99s", s[i].name);
printf("ID: ");
scanf("%99s", s[i].ID);
printf("Final: ");
scanf("%d", &s[i].Final);
printf("mid: ");
scanf("%d", &s[i].mid);
printf("attendance: ");
scanf("%d", &s[i].attendance);
printf("assignment: ");
scanf("%d", &s[i].assignment);
printf("quiz1: ");
scanf("%d", &s[i].quiz1);
printf("quiz2: ");
scanf("%d", &s[i].quiz2);
printf("quiz3: ");
scanf("%d", &s[i].quiz3);
}
printf("n Students Informations:n");
for (i = 0; i < n; i++) {
printf("Student No: %dn", i);
printf("Name: %sn", s[i].name);
printf("ID: %dn", s[i].ID);
printf("Final: %dn", s[i].Final);
printf("mid: %dn", s[i].mid);
printf("attendance: %dn", s[i].attendance);
printf("assignment: %dn", s[i].assignment);
printf("quiz1: %dn", s[i].quiz1);
printf("quiz2: %dn", s[i].quiz2);
printf("quiz3: %dn", s[i].quiz3);
quiz_best = comparison(s[i].quiz1, s[i].quiz2, s[i].quiz3);
printf("The best quiz mark is: %dn", quiz_best);
printf("n");
}
return 0;
}

相关内容

  • 没有找到相关文章

最新更新