Pages: [1] 2   Go Down
Author Topic: Duplicate Case error for switch command  (Read 3885 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 06, 2011, 07:48:17 pm by cmiyc » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
do they have to be though?
Yes. It's not my rule, but it is THE rule.

Code:
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.
« Last Edit: January 06, 2011, 08:36:19 pm by PaulS » Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5551
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 :-?
« Last Edit: January 06, 2011, 09:45:45 pm by keeper63@cox.net » Logged

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5551
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 :-/
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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


Logged

Rob Tillaart

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

Pages: [1] 2   Go Up
Jump to: