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?
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.
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.
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
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.
@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...
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!