Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(13): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(16): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(19): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(22): error C2059: syntax error : '>=' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(25): error C2059: syntax error : '>' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.cpp(28): error C2059: syntax error : '==' 1>c:\users\robin\documents\visual studio 2010\projects\snake\snake\exercise.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.

like image 678
Robin Van den Broeck Avatar asked Feb 24 '12 14:02

Robin Van den Broeck


People also ask

Can you do ranges in switch statements?

Using range in switch case in C/C++ In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement. After writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.

How do you specify a range in C++?

Use the range-based for statement to construct loops that must execute through a range, which is defined as anything that you can iterate through—for example, std::vector , or any other C++ Standard Library sequence whose range is defined by a begin() and end() .

Is switch statement a selection?

In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.

How to detect range of value in switch case?

The function range return some value according to the input argument, so the "range of value" detection could be done here, coupled with switch case clause. I would suggest using an enum instead of magic int values. Or you could use your solo cases as intended and use your default case to specify range instructions as :

Can we use range in switch statement in C 7?

The switch statement in C# 7 With C# 7, you can use range operators within a case statement. The structure of the above code looks cleaner than the old switch statement. More importantly, it's powerful for handling a range of values.

How to select data within a range of values in SQL?

Selecting Data Within a Range of Values with SQL BETWEEN Operator. Summary: in this tutorial, you will learn how to use SQL BETWEEN operator to select data within a range of values. The BETWEEN operator is used in the WHERE clause to select a value within a range of values. We often use the BETWEEN operator in the WHERE clause of the SELECT , ...

How to use range of numbers in case statement in C++?

You all are familiar with switch case in C/C++, but did you know you can use range of numbers instead of a single number or character in case statement. case 'A' ... 'Z': You need to Write spaces around the ellipses … . For example, write this: Time Complexity: O (n), where n is the size of array arr.


1 Answers

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/).

like image 152
Ankit Patel Avatar answered Oct 03 '22 06:10

Ankit Patel