我想写一个程序通过使用结构来计算学生的成绩并打印位置

I want to write a program to calculate the result of a student by using the structure and print the position

在这个程序中,我想使用结构获取学生信息并打印位置。在测验部分,只有最好的一个才会算三分。现在,当我 运行 程序时,它不接受任何输入。它只询问学生人数,然后终止并且不接受任何其他输入。谁能找出我的问题所在。

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: %d\n",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: %d\n",i);
            printf("\nName: %s\n",s[i].name);
            printf("\nID: %d\n",s[i].ID);
            printf("\nFinal: %d\n",s[i].Final);
            printf("\nmid: %d\n",s[i].mid);
            printf("\nattendance: %d\n",s[i].attendance);
            printf("\nassignment: %d\n",s[i].assignment);
            printf("\nquiz1: %d\n",s[i].quiz1);
            printf("\nquiz2: %d\n",s[i].quiz2);
            printf("\nquiz3: %d\n",s[i].quiz3);
            printf("\nThe best quiz mark is: ",quiz_best);
            comparison(s[i].quiz1,s[i].quiz2,s[i].quiz3,quiz_best);
        }

    }
} ```

你的代码有很多问题:

  • 缺少大括号:

    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函数应该returnquiz_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: %d\n", 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: %d\n", i);
        printf("Name: %s\n", s[i].name);
        printf("ID: %d\n", s[i].ID);
        printf("Final: %d\n", s[i].Final);
        printf("mid: %d\n", s[i].mid);
        printf("attendance: %d\n", s[i].attendance);
        printf("assignment: %d\n", s[i].assignment);
        printf("quiz1: %d\n", s[i].quiz1);
        printf("quiz2: %d\n", s[i].quiz2);
        printf("quiz3: %d\n", s[i].quiz3);
        quiz_best = comparison(s[i].quiz1, s[i].quiz2, s[i].quiz3);
        printf("The best quiz mark is: %d\n", quiz_best);
        printf("\n");
    }
    return 0;
}

你的 if

有问题
for(i=0,i<n; i++;)

应该是

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

这解决了您的实际问题。

为避免此类麻烦,请使用使编译器更严格的标志进行编译。使用 gcc/clang,例如 -Wall -Wextra -Werror