Number range switch statement

Hey guys, I just started c++ after doing a few years in python and java. I thought I'd give c++ a challenge as it is supposed to be difficult. I just want to know if I can get a range of numbers in a 'switch' statement instead of one single case number.for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    int age;
    Cin >> age;
    switch (age) {
        case 16:
            cout << "you are young";
            break;
        case 43:
            cout << "ur an adult";
            break;
        case 78:
            cout << "ur old";
            break;
        default:
            cout << "defualt";
    }

    return 0;
}


What I want to do here is I want to get an input from the user and if the person inputs a number from 0-16 it will output 'you are young' instead of you having to input 16 exactly to get it. Thank you guys :D

Last edited on
Nope. You can't put a range of numbers is a case clause.
Use an if / else if tree, or you can do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    switch (age) 
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:  //  Above cases all fall through to the following statements
            cout << "you are young";
            break;
    //  etc 

Hi,

> What I want to do here is I want to get an input from the user and if the person inputs a number from 0-16 it will output 'you are young' instead of you having to input 16 exactly to get it.

Then in this case the switch statement is not efficient. You have to use if and if else instead :
1
2
3
if (age <= 16) cout << "you are young";
else if (age <= 43) cout << "ur an adult";
else if (age <= 78) cout << "ur old";
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    int age;
    std::cin >> age;
    switch (age) {
        case 16 ... 42:
            std::cout << "you are young";
            break;
        case 43 ... 77:
            std::cout << "ur an adult";
            break;
        case 78 ... 200:
            std::cout << "ur old";
            break;
        default:
            std::cout << "default";
    }
    return 0;
}


http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement

Runs on the shell perfectly but elsewhere?
@kemort
+1
closed account (48T7M4Gy)
Cheers camanynumbers&letters, I learned something new and amazingly it works (with a warning but who cares) on my mbpro at least :)
closed account (48T7M4Gy)
FWIW, characters work too, as expected. All you need now is an application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    char age;
    std::cin >> age;
    switch (age) {
        case 'a' ... 'c':
            std::cout << "you are young";
            break;
        case 'd' ... 'q':
            std::cout << "ur an adult";
            break;
        default:
            std::cout << "default";
    }
    return 0;
}
Last edited on
Note that using ... in switch statements like that is not standard C++. It's a language extension that is available with some compilers, so it's probably better to use regular if statements for this.
Last edited on
closed account (48T7M4Gy)
so it's probably better to use regular if statements for this

Rubbish!

As long as programmers are aware of the issues/caveats which have already been stated then there is absolutely no reason not to use it.

If the only source was standard C++ then you can throw away Boost for a start.
Well, of course it's up to everyone to decide what features to use and if standard C++ is something worth aiming for. Personally I think standard C++ is important because it allows people to compile my code with whatever standard-compliant compiler they want, and it'll also make it easier for me to switch compiler in the future if I wanted to.

The C++ language was standardized for very good reasons. If we didn't care about standards we would be back to the days before C++98 when each compiler had it's own version of the language that was often incompatible with each other.

I have no objections to using boost. Using a non-standard library is a quite different compared to using a non-standard core language feature.

Standard or non-standard, I still think if statements are better for this particular problem because it's more readable (my opinion) and you don't need to use two numbers to describe the different ranges. For instance if you decide to change the "young" age from 16 to 18 you would have to modify the code in two places if you use switch compared to only one if you use if statements.

1
2
3
4
5
6
7
switch (age) {
	case 0 ... 18:
		std::cout << "you are young";
		break;
	case 19 ... 43:
		std::cout << "ur an adult";
...

1
2
3
4
5
6
7
8
if (age <= 18)
{
	cout << "you are young";
}
else if (age <= 43)
{
	cout << "ur an adult";
...
Last edited on
closed account (48T7M4Gy)
Sounds like a totally long-winded way of saying you just don't intend to use it when other people might. The responses here are to the OP and not meant for getting bogged down in your esoteric preferences and nit-picking.

A good compiler throws up a warning like
Use of GNU case range extension
If you decide to use cheap rubbish then that's no argument for not using the non-standard functionality. Further, Boost et al are arguably all about off-standard extension/expansion of C++ to accommodate improved usability and functionality rather than adherence to a blind dogma which, let's face it, was/is never intended.

The range limits can be easily replaced with const's from a table if you are concerned about maintainability. Which means the replacement would only have to be done in one place and probably not very often, just like standard switch statements.

It goes without saying, but you can still do what you like, just like everybody else. ;)
@kemort - When I saw your earlier response, I was intrigued. Had I missed a feature in the language? If so when was it added? I searched the following page and could not find the feature listed as being in any of the C++ standards.
http://en.cppreference.com/w/cpp/compiler_support

I often work in a non C/C++ based language that has the case range feature and I find it very useful when appropriate. However, I agree with Peter87 that since this is not a feature of any of the standards, one should use it only with the understanding that it is non-portable.
closed account (48T7M4Gy)
Keep up the good work :)
Topic archived. No new replies allowed.