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
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
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
> 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";
elseif (age <= 43) cout << "ur an adult";
elseif (age <= 78) cout << "ur old";
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.
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;
case19 ... 43:
std::cout << "ur an adult";
...
1 2 3 4 5 6 7 8
if (age <= 18)
{
cout << "you are young";
}
elseif (age <= 43)
{
cout << "ur an adult";
...
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.