如何在 switch 语句中选择一系列值?

2022-01-19 00:00:00 switch-statement c++

当我尝试编译时出现此错误:

<上一页>1>----- 构建开始:项目:snake,配置:Debug Win32 ------1> 练习.cpp1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(13):错误 C2059:语法错误:'>='1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(16):错误 C2059:语法错误:'>='1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(19):错误 C2059:语法错误:'>='1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(22):错误 C2059:语法错误:'>='1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(25):错误 C2059:语法错误:'>'1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(28):错误 C2059:语法错误:'=='1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(34): warning C4065: switch statement contains 'default' but no 'case' labels========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

代码:

#include <iostream>使用命名空间标准;诠释主要(){积分;//Vraag de scorecout <<分数:";cin>>分数;//转变开关(分数){案例>= 100:cout <<一个";休息;案例>= 50:cout <<乙";休息;案例>= 25:cout <

我该如何解决这个问题?这是一个控制台应用程序,Win32,我的 IDE 是 Windows Enterprise C++ 2010.

我正在学习从游戏编程开始 C++.

解决方案

在 C++ 中,case 标签是 constant 表达式,而不是一般的表达式.你需要一连串的 if-then-else 语句来做你想做的事情.

或者,您可以枚举开关中的值.这运行速度稍快(尽管在像你这样的情况下并不重要),但它的可读性要差得多:

switch(score) {情况 0:cout <

When I try to compile I get this error:

1>------ Build started: Project: snake, Configuration: Debug Win32 ------
1>  exercise.cpp
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(13): error C2059: syntax error : '>='
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(16): error C2059: syntax error : '>='
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(19): error C2059: syntax error : '>='
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(22): error C2059: syntax error : '>='
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(25): error C2059: syntax error : '>'
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(28): error C2059: syntax error : '=='
1>c:usersobindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(34): warning C4065: switch statement contains 'default' but no 'case' labels
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Code:

#include <iostream>
using namespace std;

int main(){
    int score;

    //Vraag de score
    cout << "Score:";
    cin >> score;

    //Switch
    switch(score){
        case >= 100:
            cout << "a";
            break;
        case >= 50:
            cout << "b";
            break;
        case >= 25:
            cout << "c";
            break;
        case >= 10:
            cout << "d";
            break;
        case > 0:
            cout << "e";
            break;
        case == 0:
            cout << "f";
            break;
        default:
            cout << "BAD VALUE";
            break;
    }
    cout << endl;
    return 0;
}

How can I fix this problem? It's a console application, Win32 and my IDE is Windows Enterprise C++ 2010.

I'm learning from Beginning C++ Through Game Programming.

解决方案

In C++ case labels are constant expressions, not expressions in general. You need a chain of if-then-else statements to do what you are trying to do.

Alternatively, you can enumerate the values in the switch. This runs marginally faster (though it does not matter in cases like yours), but it is considerably less readable:

switch(score) {
    case 0: cout << "f"; break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10: cout << "e"; break;
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25: cout << "c"; break;
    // ...and so on, you get the idea...

}

相关文章