简体   繁体   English

有人可以帮我解释一下我的 c 代码有什么问题吗?

[英]Can someone help me to explain what is wrong in my c code?

So first of all, here is my code :所以首先,这是我的代码:

#include <stdio.h>

double total(double assignment, double mid, double final){
    double totalScore;

    totalScore = (assignment * 0.2) + (mid * 0.3) + (final * 0.5);

    return totalScore;
}

char grade(char Z, char X, char Y, char P){



    char gradeAchieved;

    if (total(assignment, mid, final) >= 80 )`{
        gradeAchieved = Z;
    }
    else if ((total(assignment, mid, final) >= 70) && (total(assignment, mid, final) < 80)  ){
        gradeAchieved = X;
    }
    else if ((total(assignment, mid, final) >= 60) && (total(assignment, mid, final) < 70)  ){
        gradeAchieved = Y;
    }
    else if (total(assignment, mid, final) < 60){
        gradeAchieved = P;
    }
    return gradeAchieved;
}

int main()
{
    double x;
    double y;
    double z;
    char A = 'A';
    char B = 'B';
    char C = 'C';
    char D = 'D';
    printf("Input your assignment score: ");
    scanf("%lf", &x);
    printf("Input your midtest score: ");
    scanf("%lf", &y);
    printf("Input your final test score: ");
    scanf("%lf", &z);

    printf("Your total score is: %lf", total(x, y, z));
    printf("Your grade is: %c", grade(A, B, C, D));


    return 0;
}

in my code, its supposed that when I run it, you will get a prompt to input your assignment, mid, and final grade.在我的代码中,假设当我运行它时,您将收到输入作业、期中和期末成绩的提示。 Then it will calculate your final score and decides whether you get an A, B, C, or D. But it seems that every time i tried to run it, i got this error message:然后它会计算你的最终分数并决定你是否得到 A、B、C 或 D。但似乎每次我尝试运行它时,我都会收到以下错误消息:

main.c: In function ‘grade’:
main.c:17:15: error: ‘assignment’ undeclared (first use in this function)
     if (total(assignment, mid, final) >= 80 ){
               ^~~~~~~~~~
main.c:17:15: note: each undeclared identifier is reported only once for each function it appears in
main.c:17:27: error: ‘mid’ undeclared (first use in this function)
     if (total(assignment, mid, final) >= 80 ){
                           ^~~
main.c:17:32: error: ‘final’ undeclared (first use in this function)
     if (total(assignment, mid, final) >= 80 ){
                                ^~~~~

And I don't really know how to solve it.我真的不知道如何解决它。 So could anybody help me?那么有人可以帮助我吗? And sorry for my bad english很抱歉我的英语不好

The reason for the three error messages is that you haven't declared any variables named assignment , mid , and/or final in your function grade() .出现这三个错误消息的原因是您没有在函数grade()声明任何名为assignmentmid和/或final变量。 You could move the four char variables into the grade() function and change the function parameters to match total() and your code should run.您可以将四个char变量移动到grade()函数中并更改函数参数以匹配total()并且您的代码应该可以运行。

char grade(double assignment, double mid, double final){

    char A = 'A';
    char B = 'B';
    char C = 'C';
    char D = 'D';

    char gradeAchieved;

    if (total(assignment, mid, final) >= 80 ) {
        gradeAchieved = A;
    }
    else if ((total(assignment, mid, final) >= 70) && (total(assignment, mid, final) < 80)  ){
        gradeAchieved = B;
    }
    else if ((total(assignment, mid, final) >= 60) && (total(assignment, mid, final) < 70)  ){
        gradeAchieved = C;
    }
    else if (total(assignment, mid, final) < 60){
        gradeAchieved = D;
    }
    return gradeAchieved;
}

int main()
{
    double x;
    double y;
    double z;

    printf("Input your assignment score: ");
    scanf("%lf", &x);
    printf("Input your midtest score: ");
    scanf("%lf", &y);
    printf("Input your final test score: ");
    scanf("%lf", &z);

    printf("Your total score is: %lf", total(x, y, z));
    printf("Your grade is: %c", grade(x, y, z));


    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM