Using an array in a switch case

Hi there!

Is it possible to use an array in a switch case?

Like this?

char states[4][6] = {"Dud","Win","Lose","Accel"};

switch(states[0]) {
case "Dud":
//do nothing
break;

case "Win":
// user wins
break;

case "Lose":
//user loses
break;

case "Accel":
//timer accelerates
break;
}

the array values are shuffled every time the Arduino turns on. That's what the switch case is for. I've tried using if....else if statements, but they don't seem to be working.

Thanks!

No but you can do this.

#define Dud 0
#define Win 1
#define Lose 2
#define Accel 3

byte states[4] = {
  Dud,Win,Lose,Accel};

void setup()
{
  Serial.begin(115200);

  switch(states[0]) 
  {
  case Dud:
    //do nothing
    break;

  case Win:
    // user wins
    break;

  case Lose:
    //user loses
    break;

  case Accel:
    //timer accelerates
    break;
  }
}

void loop() 
{

}

The expression controlling a switch must resolve to an integral value. The code you posted wouldn't work because you are using string data which do not resolve to an integral data type. You could use:

  char states[4][6] = {"Dud", "Win", "Lose", "Accel"};
  int index;
  // some code that determines the proper index to use...

  char val = states[index][0];

  switch (val) {
    case 'D':
      //do nothing
      break;
    case 'W':
      // user wins
      break;
    case 'L':
      //user loses
      break;
    case 'A':
      //timer accelerates
      break;
    default:
      Serial.println("I shouldn't be here.");
      break;
  }

I also find it useful to always have a default case in a switch expression.

Thanks guys! I will try both of those and let you know hot it goes!

If the strings have the same char on a single spot you could hash the strings to see if you get a unique value.

e.g
int hash(str) { return str[0]*20 + strlen(str); } // uses first and length to have a unique value
silently assumes no string is longer than 20

deriving a good hash function can be difficult, so sometimes the names is preceded with a unique char.
(make the problem part of the solution :wink:

Alright I'm back with some results.

HazardsMind:
No but you can do this.

#define Dud 0

#define Win 1
#define Lose 2
#define Accel 3

byte states[4] = {
  Dud,Win,Lose,Accel};

void setup()
{
  Serial.begin(115200);

switch(states[0])
  {
  case Dud:
    //do nothing
    break;

case Win:
    // user wins
    break;

case Lose:
    //user loses
    break;

case Accel:
    //timer accelerates
    break;
  }
}

void loop()
{

}

Can you clarify this code a bit? For one, the array needs to be 2D (char states[4][6], not char states[4]) to work with my previously written code.

econjack:
The expression controlling a switch must resolve to an integral value. The code you posted wouldn't work because you are using string data which do not resolve to an integral data type. You could use:

  char states[4][6] = {"Dud", "Win", "Lose", "Accel"};

int index;
  // some code that determines the proper index to use...

char val = states[index][0];

switch (val) {
    case 'D':
      //do nothing
      break;
    case 'W':
      // user wins
      break;
    case 'L':
      //user loses
      break;
    case 'A':
      //timer accelerates
      break;
    default:
      Serial.println("I shouldn't be here.");
      break;
  }




I also find it useful to always have a *default* case in a *switch* expression.

This works perfectly! Thanks for the help!

For one, the array needs to be 2D (char states[4][6], not char states[4]) to work with my previously written code.

If you had posted the previous code and said you needed them to work together, I would have given you exactly what econjack gave you.