SWITCH statment with CHAR array

  • Hi


    This one might be easy for some of you but not for me. And that's the reason I'm asking it here.


    KRC4 V8.3


    I want to use and CHAR array in an SWITCH statement. And I read the manual and it should be possible to use CHAR with and SWITCH statement.
    So a wrote an basic code like this just for testing it out.


    DECL cStatus[80]
    cStatus[]="full"


    SWITCH cStatus[]


    CASE "full"
    ;
    ; bla bla bla
    ;
    CASE "empty"
    ;
    ; yea yea yea
    ;
    CASE "done"
    ;
    ;
    ;
    DEFAULT
    ;
    ;
    ENDSWITCH


    When I load the code to robot it keeps complaining about syntax errors... Changed code according to the errors but when I got some errors to disappear, new ones occurs.
    Did an other test using integers and it worked fine. No problems...


    Can anyone give me some working examples? Or at lest tell me what I'm doing wrong.


    I'll could use normal IF statement but it would be much more conveniently to use SWITCH.


    -zicki

  • What "syntax errors," exactly? The specific error messages are important, as well as the actual line of code each specific error is associated to.


    I've never seen this tried, and I don't have a KRC handy to experiment with. But I have a hunch it may not be legal under KRL. Strings (CHAR arrays) always get handled a little bit oddly, compared to other variable types.


    IIRC, you can't use a simple "==" equivalence test on a String, and I suspect that's what the SWITCH/CASE statement is doing "under the hood," so to speak. To compare strings, you need to use the STRCOMP system function (which can't be shoehorned into a SWITCH CASE), or an iterative character-by-character comparison (ditto).

  • CHAR element and CHAR array are not the same things. you can do what you want by reducing array to a single CHAR.
    better way is to use ENUM



    you learn about this in Programming2

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2


  • [size=2px] [/size]

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

Advertising from our partners