Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c++ - How do I select a range of values in a switch statement?

When I try to compile I get this error:

1>------ Build started: Project: snake, Configuration: Debug Win32 ------
1>  exercise.cpp
1>c:users
obindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(13): error C2059: syntax error : '>='
1>c:users
obindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(16): error C2059: syntax error : '>='
1>c:users
obindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(19): error C2059: syntax error : '>='
1>c:users
obindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(22): error C2059: syntax error : '>='
1>c:users
obindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(25): error C2059: syntax error : '>'
1>c:users
obindocumentsvisual studio 2010projectssnakesnakeexercise.cpp(28): error C2059: syntax error : '=='
1>c:users
obindocumentsvisual 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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some compilers support case ranges like case x ... y as an extension to the C++ language.

Example:

#include <iostream>
using namespace std;

int main(){
    int score;

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

    //Switch
    switch(score){
       case 0:
            cout << "a";
            break;
       case 0 ... 9:
            cout << "b";
            break;
       case 11 ... 24:
            cout << "c";
            break;
       case 25 ... 49:
            cout << "d";
            break;
       case 50 ... 100:
            cout << "e";
            break;         
        default:
            cout << "BAD VALUE";
            break;
    }
    cout << endl;
    return 0;
}

GCC 4.9, Clang 3.5.1 and Intel C/C++ Compiler 13.0.1 seem to support it (tried on http://gcc.godbolt.org/). On the other hand, Visual C++ 19 doesn't (tried on http://webcompiler.cloudapp.net/).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...