Go Down

Topic: Duplicate Case error for switch command (Read 4810 times) previous topic - next topic

phinsil6

Hi I am trying to parse some incoming data with a switch case command with the following code:
Code: [Select]
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

James C4S

#1
Jan 07, 2011, 01:47 am Last Edit: Jan 07, 2011, 01:48 am by cmiyc Reason: 1
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
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

mspguy

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.

phinsil6

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

phinsil6

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

PaulS

Quote
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.

phinsil6

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

PaulS

#7
Jan 07, 2011, 02:35 am Last Edit: Jan 07, 2011, 02:36 am by PaulS Reason: 1
Quote
do they have to be though?

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

Code: [Select]
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.

James C4S

Quote
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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

cr0sh

#9
Jan 07, 2011, 03:44 am Last Edit: Jan 07, 2011, 03:45 am by keeper63@cox.net Reason: 1
Quote
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):

Code: [Select]
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...

:-?
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

phinsil6

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

James C4S

@cr0sh, You can't make function calls for a case statement.  The complier is expecting constants.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

cr0sh

Quote
@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...

:-/
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

PaulS

Quote
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.

robtillaart


You can use the Enum type in a switch as it is an integer type under the hood - http://www.arduino.cc/playground/Code/Enum

enum country { FRANCE, ENGLAND, ITALY, UNKNOWN }

country_var = FRANCE;

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


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up