得到错误:“变量不能出现在常量表达式中”

脚本比例

我已经在C-Free5.0中编写了一个基本代码,如果要执行与if语句相同的操作,那么一切都很好。但是当我使用开关时会出现错误:'per'不能出现在Constant-expression中,为什么会给出错误?

#include<stdio.h>
#include<conio.h>
int main()
{
    int urdu=134;
    int eng=112;
    int isl=72;
    int ps=58;
    int maths=137;
    int phy=128;
    int chem=120;
    int bio=115;

    int total=(urdu+eng+isl+ps+maths+phy+chem+bio);
    int per=(total*100)/1050;

    printf("Urdu=               %d\n", urdu);
    printf("English=            %d\n", eng);
    printf("Islmiyat=           %d\n", isl);
    printf("Pakistan Studies=   %d\n", ps);
    printf("Mathematics=        %d\n", maths);
    printf("Physics=            %d\n", phy);
    printf("Chemistery=         %d\n", chem);
    printf("Biology=            %d\n\n", bio);

    printf("Percentage:         %d\n",per);

    switch(per)
    {
        case (per>80):
        printf("A+");

        case (per>70):
        printf("A");
    }
    getch();
}
穆雷尼克

切换条件是一系列ifs的简写形式,ifs检查变量与一系列常量相等性。您不能以指定的方式使用它。相反,您可以只使用简单的旧if语句:

if (per > 80) {
    printf("A+");
else if (per > 70) {
    printf("A");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++:“(静态const双变量)不能出现在常量表达式中”

为什么折叠表达式不能出现在常量表达式中?

“opt”不能出现在常量表达式中

const int <variable>不能出现在常量表达式中

'__u32 __fswab32(__ u32)'不能出现在netinet / tcp_md5.h中的常量表达式中

SQL错误:全文谓词不能出现在聚合表达式中

'this'不能在带有this->指针变量的常量表达式错误(C ++)中使用

为什么变量j初始化不能出现在for循环的终止表达式中?

飞镖:常数评估错误。方法“ []”不能在常量表达式中调用

常量表达式中引用类型的变量

枚举属性中的变量:需要常量表达式

“预期常量表达式”错误

如何获取出现在布尔表达式中的变量列表?

开关盒错误| 的值不能在常量表达式中使用

保持形式参数为常数时出现“需要常量表达式”错误

GLSL:由于非常量表达式初始化,For循环中出现错误

const变量的值在常量表达式中是否可用,取决于变量类型

错误:不是常量表达式。(扑)

Maven Android插件错误“需要常量表达式”

编译错误:需要常量表达式

MS Visual Studio 错误:预期的常量表达式

静态断言C ++-常量表达式错误

VBA - 错误“常量表达式需要”

PG :: InvalidColumnReference:错误:对于SELECT DISTINCT,ORDER BY表达式必须出现在选择列表中

错误:对于SELECT DISTINCT,ORDER BY表达式必须使用uniq的order by出现在选择列表中

SQL语法错误:SELECT DISTINCT,ORDER BY表达式必须出现在选择列表中

C结构对象堆栈-常量表达式中不允许函数调用(错误)

在常量表达式中调用的“静态constexpr”函数是...错误?

intel fortran编译错误“此内在函数在常量表达式中无效”