需要帮助在我的C编程中定义函数

Zac*_*ith 0 c

我正在学习C函数并尝试编写一个简单的脚本,根据用户输入的数字输出一个lettergrade.然而,我得到以下错误,我无法弄清楚我缺少的地方......

102809.c: In function ‘function_points’:
102809.c:44: error: ‘lettergrade’ redeclared as different kind of symbol
102809.c:41: error: previous definition of ‘lettergrade’ was here
102809.c:47: error: ‘A’ undeclared (first use in this function)
102809.c:47: error: (Each undeclared identifier is reported only once
102809.c:47: error: for each function it appears in.)
102809.c:49: error: syntax error before ‘lettergrade’ 
Run Code Online (Sandbox Code Playgroud)

如果你能提供指导,我将不胜感激.我将此脚本与简单的C函数脚本进行比较,它们看起来很相似:(

#include<stdio.h>
#include<stdlib.h>

//prototype
int function_points (char);

int main (void)
{
    //use a do or while loop to continue asking for user input, asking user to input test score. 
    //0 = quit

    int points; //this is student's points, we call this via scanf
    int counter; //our counter variable to increase loop
    char lettergrade;
    counter = 0;

    do {
        printf("Enter the student's points:  (0 to quit): ");
        scanf("%d", &points);
        printf("%d points = %c grade \n", points, function_points(lettergrade)); //declare function
        counter++;
    } while (points != 0); 
    //counter --;


    printf("Press any key to continue...");

    getchar();
    getchar();
    return 0;
}

//function definition
int function_points (char lettergrade)
{
    int points;
    char lettergrade;

    if (points < 90 && points > 100) 
        lettergrade = A;
    else if (points < 80 && points > 89 
             lettergrade = B;
    else if (points < 70 && points > 79 
             lettergrade = C;
    else if (point < 60 && point > 69) 
             lettergrade = D;
    else 
    {
             lettergrade = F;
    }
    return lettergrade;
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*aev 10

这个:

int function_points (char lettergrade)
{
    int points;
    char lettergrade;
Run Code Online (Sandbox Code Playgroud)

您将函数参数重新声明为局部变量.你不需要这样做(也不能那样做).只需从上面的代码段中删除最后一行即可.

您是否偶然使用了一些预先ANSI C的C书(例如K&R 1st ed.)?这个错误看起来像是一个混合的ANSI和旧的K&R声明.如果是这种情况,请查找更新的书籍.

此外,这:

if (points < 90 && points > 100) 
    lettergrade = A;
else if (points < 80 && points > 89 
             lettergrade = B;
else if (points < 70 && points > 79 
             lettergrade = C;
else if (point < 60 && point > 69) 
             lettergrade = D;
else 
{
             lettergrade = F;
}
Run Code Online (Sandbox Code Playgroud)

此代码,书面,尝试引用变量 A,B,C等你可能想要的是字符-那些将被写为'A','B','C'在单引号-等.