Switch (case) statement with variable

Hi, i have a projet i'm doing some modification in the code and I'm not a big C++ programmer (more java and vb)

I currently have a switch case statement that is working great. Exemple:

switch (buf[0]){ 
            case 'A':                         
               //do something
               break;
            case 'B':                         
               //do something
               break;  
      }

The 'A' and 'B' change value depending of the project and wish to use a variable (global at the top of the code) to store those value because there is a lot of place to edit when programming another arduino, and often forgot some.

So I try this

char controllerID_1 = 'A';
char controllerID_2 = 'B';

...

switch (buf[0]){ 
            case controllerID_1:                         
               //do something
               break;
            case controllerID_2:                         
               //do something
               break;  
      }

And I got this error:

error: 'controllerID_1' cannot appear in a constant-expression

Is there a way to do what I want in a Switch statement or I will need to do some If Else Statement ?

You’ll need to use if else statements for that.

const char controllerID_1 = 'A';
const char controllerID_2 = 'B';

... and the compiler is happy.

wico2002: The 'A' and 'B' change value depending of the project and wish to use a variable (global at the top of the code) to store those value because there is a lot of place to edit when programming another arduino, and often forgot some. ...

Is there a way to do what I want in a Switch statement or I will need to do some If Else Statement ?

Actually, I think you may be able to do what you want, if you are talking about programming and not running... use const keyword

like this:

const char controllerID_1 = 'A';
const char controllerID_2 = 'B';

int buf[10];

void setup() {
  // put your setup code here, to run once:

}

void loop()
{
  switch (buf[0]) 
  {
    case controllerID_1:
      //do something
      break;
    case controllerID_2:
      //do something
      break;
  }
}

Isn't this the place for an enum?

enum Controller { controllerID_1 = 'A', controllerID_2 = 'B' };
r = controllerID_1;
switch(r)
{
    case controllerID_1  : /...
    case controllerID_2 : /...
 }

or am I missing the point?