Duplicate Case error for switch command

Hi I am trying to parse some incoming data with a switch case command with the following code:

void parseReceivedText()
{
  switch (textBuff[8])
  {
    case 'printf_ip' : IPPrint();                             break;
    case 'poe_resta' : PoeRestart();                          break;
    case 'cha_dname' : ChangeDeviceName();                    break;
    case 'change_ip' : ChangeIP();                            break;
    case 'change_gw' : ChangeGW();                            break;
    case 'change_sm' : ChangeSM();                            break;
    case 'rfinished' : ReadyToContinue();                     break;
    case 'adminlock' : AdminLockdown();                       break;
    case 'adminover' : AdminOverride();                       break;
    case 'term_teln' : telnet_session.stop();                 break;
    case 0x0d :                                               break;
    default   : telnet_session.println("invalid command");    break;
  }
}

But I get a duplicate case error and since i have no idea what the error means i don't even know what to look for. anybody have ideas that could help with this?

thanks

Duplicate Case Error means you have defined two cases with the same value in the switch statement. You are probably looking at your code thinking "but they are all different." To you they are different. To the complier they look much different.

You have defined case statements using the character notation. Single quotes are meant for characters, not strings. Without seeing how you defined textBuff[], I could be wrong about the following: you are probably only comparing one character from textBuff[] (whatever character is at element #8) to whatever constant value the complier generates for the "junk" inside of the single quotes.

Read up on strings.

http://arduino.cc/en/Reference/String

To add to the above. Its hard to compare strings in a switch case statement. the only way to do it that I know of is harder and messier than just using if else.

oh i see, so if i had a different character at the end of each string then there won't be an duplicate case errors

i changed some stuff in my code (made sure the 9th character was different for each case) and all is well now, thanks alot guys. i learn something new everyday from this forum

so if i had a different character at the end of each string then there won't be an duplicate case errors

The point that you seemed to have missed is that cases needed to be constant integer or character values, NOT string values.

do they have to be though? it seems that with string values the commands can make more sense to the user

do they have to be though?

Yes. It's not my rule, but it is THE rule.

if(strcmp(textBuff, "printf_ip") == 0) IPPrint();
else if(strcmp(textBuff, "poe_resta") == 0) PoeRestart();
else if(strcmp(textBuff, "cha_dname") == 0) ChangeDeviceName();

This is really not any more difficult than a switch statement, and takes no more room, and isn't much more difficult to read.

it seems that with string values the commands can make more sense to the user

Depends on what you mean by user.

The Arduino isn't human.

Using integers, #defines, constants, comments, etc you can make the code more readable. At the same time, you can use those same elements to create a user interface that makes sense to both the user and the Arduino.

This is really not any more difficult than a switch statement, and takes no more room, and isn't much more difficult to read.

I think the following would be "equivalent" (haven't tried compiling it, tho):

switch (0) {
  case strcmp(textBuff, "printf_ip"):
    IPPrint();
    break;
  case strcmp(textBuff, "poe_resta"):
    PoeRestart();
    break;
  case strcmp(textBuff, "cha_dname"):
    ChangeDeviceName(); 
    break;
  default:
}

It could probably be simplified/cleaned up with a #define or some other manner...

:-?

well I will try all those options after classes tomorrow and post how it goes

also, will the whole command typed it be put into the textbuff then?

thanks alot

@cr0sh, You can't make function calls for a case statement. The complier is expecting constants.

@cr0sh, You can't make function calls for a case statement. The complier is expecting constants.

Ah, you're right; just tried it in the IDE. It's too bad that wouldn't work (I think it would work in PHP - not sure). The logic I think is sound, at least...

;D

Oh well...

:-/

It's too bad that wouldn't work

Even if it had "worked", strcmp returns either -1, 0, or +1, depending on the lexigraphical order of the strings. So, you'd be limited to 3 cases.

You can use the Enum type in a switch as it is an integer type under the hood - Arduino Playground - Enum Resource

enum country { FRANCE, ENGLAND, ITALY, UNKNOWN }

country_var = FRANCE;

switch (country_var)
{
case FRANCE: ...
break
case ENGLAND: ...
etc

strcmp returns zero if the strings are equal, or a negative or positive integer; comparing the return value against +1 or -1 as suggested by PaulS may not get you the results you're looking for either!