"c"未在此范围内声明

'c' was not declared in this scope

本文关键字:范围内 声明      更新时间:2023-10-16

在我的平均成绩代码中,我有字母等级a,b,c,d和f,但是当我尝试运行它时,我的字母没有在这个范围内声明。我不知道这意味着什么。

这是我的代码:

#include<iostream>
using namespace std;
string average(int g);
int main()
{
    int grade;
    cout << "Enter your grade:" << endl;
    cin >> grade;
    cout << "Your grade is a" << average(grade) << "!" << endl;
    return 0;
}
string average(int g)
{
    if (g >= 90 && g <= 100)
    {
        return a;
    }
    else if(g >= 80 && g <90)
    {
        return b;
    }
    else if (g > 70 && g < 80)
    {
        return c;
    }
    else if (g == 70)
    {
        return d;
    }
    else if (g < 70)
    {
        return f;
    }
    return 0;
}

你的程序中没有任何地方声明等级 - a,b,c,d,e,f。编译器怎么知道 a 代表等级 a,b 代表等级 b 等等。

你可能可以使用枚举来声明成绩,就像这样——

enum grades {a='a', b='b', c='c', d='d', e='e', f='f'};

还有其他方法可以做到这一点。无论你用什么来实现你的目标,你都需要根据你的决定修改你的程序。