Strings in switch case Statement

Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"

Store.ino: In function 'void loop()':
Store:36: error: case label does not reduce to an integer constant
Store:41: error: case label does not reduce to an integer constant
Store:46: error: case label does not reduce to an integer constant
Store:51: error: case label does not reduce to an integer constant
Store:56: error: case label does not reduce to an integer constant
Store:61: error: case label does not reduce to an integer constant
Store:66: error: case label does not reduce to an integer constant
Store:72: error: case label does not reduce to an integer constant
Store:77: error: case label does not reduce to an integer constant
Store:82: error: case label does not reduce to an integer constant
Store:87: error: case label does not reduce to an integer constant
Store:92: error: case label does not reduce to an integer constant
Store:97: error: case label does not reduce to an integer constant
Store:102: error: case label does not reduce to an integer constant
Store:107: error: case label does not reduce to an integer constant
Store:112: error: case label does not reduce to an integer constant
Store:117: error: case label does not reduce to an integer constant
Store:122: error: case label does not reduce to an integer constant
Store:127: error: case label does not reduce to an integer constant
Store:132: error: case label does not reduce to an integer constant
Store:137: error: case label does not reduce to an integer constant
Store:142: error: case label does not reduce to an integer constant
Store:147: error: case label does not reduce to an integer constant
case label does not reduce to an integer constant

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

Show us your switch code, but I bet you are doing something like

case "somestring":

yes

void setup() {
Serial.begin(9600);
}

void loop()
{
if(Serial.available() > 0)
{
char rack = Serial.read();

switch(rack){
case "TOP":
Serial.println("TOP will be ON");
rack_TOP();
break;
}
}
}

so what can be the solution as i have tried for case'TOP'

'TOP' is an integer value.
"TOP" is not an integer value.
Neither is a single character.

'rack' is a variable capable of holding a single character, so only will contain T or O or P (for your example).

The switch statement will only work with a numeric variable (1, 2, 'a', 'b' etc).

So for top, you can simply send a 'T'

switch(rack)
{
  case 'T':
    ...
    ...
  break;
}

I would start reading Robin2's Serial Input Basics - updated if you want to make it more complicated than single character. You can make use of
strcmp in combination with if / else if

// make sure rack is large enough to hold your largets message plus one additional character
char rack[10];

//read characters into rack variable; up to you
...
...

// check and take action
if(strcmp(rack, "TOP") == 0)
{
  ...
  ...
}
else if(strcmp(rack, "BOTTOM") == 0)
{
  ...
  ...
}
else if 
etc
etc
etc

void loop()
{
if(Serial.available() > 0)
{
char rack = Serial.read();

switch (rack){

case 'TOP':
Serial.println("TOP will be ON");
rack_TOP();
break;

case 'N':
Serial.println("N will be ON");
rack_N();
break;
}
}
}

MAIN AGENDA is whenever i enter TOP on serial monitor then monitor should display "TOP will be ON" and rack_TOP(); should execute.

For N it work correctly by globally defining String rack

PROBLEM: 1) It does not take TOP as an input

Any suggestion

 char rack = Serial.read();
   
    switch (rack){

case 'TOP':

I think it has already been pointed out, but "rack" is a single "char", so it can never, ever equal 'TOP'.
Try a simple case 'T': instead.

This is the screenshot of condition which im facing currently

OK, I'll try another way.
It requires at least 21 bits to represent 'TOP'.
A "char" holds eight bits.

Do you see the problem now?

No matter how much you want to do it that way you CAN'T use a string in a switch statement in C, get over it an move on. I wanted to be an astronaut, I couldn't so I got over it, you need to do the same and find some valid C code to do what you want.

Either compromise and use a single character like 'T', or use the strcmp() function in an if{} as noted above.

Even if you could use a string like "TOP" you are only reading a single character, but you can't so forget I mentioned that. :slight_smile:

Thank you for your recommendation, i appreciate that u spared time for "THIS". @Graynomad

Desired result obtained through :

  1. Making an array of switching cases
  2. strcmp