1

so my problem is that I'm trying to write a program that uses a switch case to tell a function which case to do. I tried to include an or statement into the switch cases so that I could have a range of values for each case but it seems to be presenting me with an error. Is there a fix to this problem using the switch case or am I simply misusing the switch case and should try a different method? Thanks.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int Random(int);
void Movement(void);
int main()
{
int healthratio=5;
switch(healthratio){
case healthratio>0 || healthratio<=10:
printf("%d.\n",healthratio);
break;
case healthratio>10 || healthratio<=20:
printf("%d.\n",healthratio);
break;
case healthratio>20:
printf("%d.\n",healthratio);
break;
}
}

When I run this code I get this error "error: case label does not reduce to an integer constant"

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 5
    You need to use if-else statements instead of the switch statement. – Vlad from Moscow Nov 03 '20 at 16:27
  • 1
    `switch/case` is used when you have an expression and want to test it against set of known constants, not conditions. – Eugene Sh. Nov 03 '20 at 16:28
  • 2
    You want to use `&&` for comparisons like this: `healthratio>0 || healthratio<=10`. As written it is always true. You still can't use this in a switch/case, but just a general note since it would be a bug in an if statement. – Retired Ninja Nov 03 '20 at 16:28

2 Answers2

3
case healthratio>0 || healthratio<=10

This is not valid in standard C, some compilers allows the use of case ranges, i.e. gcc and clang:

case 0 ... 10:
    printf("%d.\n",healthratio);
    break;

But if you can not use them then you are forced to define all cases:

case 0:
case 1:
case 2:
...
case 10:
    printf("%d.\n",healthratio);
    break;

or use if statements: if (x >= 0 && x <= 10) ...

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

According to the C Standard (6.8.4.2 The switch statement)

3 The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion. There may be at most one default label in a switch statement. (Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.)

However in labels like this

case healthratio>0 || healthratio<=10:

the used expression is not a constant expression. Moreover if you used a constant operands of the expression healthratio>0 || healthratio<=10 then depending on whether the expression is true or false you had a label either

case 0:

or

case 1:

That is it is not what you are expecting.

Instead of the switch statement you should use if-else statements like

if ( healthratio>0 && healthratio<=10 )
{
    printf("%d.\n",healthratio);
}
else if ( healthratio>10 && healthratio<=20 )
{
    printf("%d.\n",healthratio);
}
else
{
    printf("%d.\n",healthratio);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335