Case statement and strings in PAC

Discussion in 'C-Touch/HomeGate/SchedulePlus/PICED Software' started by Barrington, Nov 29, 2008.

  1. Barrington

    Barrington

    Joined:
    Nov 29, 2008
    Messages:
    3
    Likes Received:
    0
    Hi all,
    I've been writing some code (logic) for serial comms, and ran into a problem with PICED that tells me it can't compile a case statement using a string.
    I tried the sample code in the Help file, and it wouldn't work either!
    Code:
    {var}
    Operator: string;
    number1: integer;
    number2: integer;
    result: integer;
    
    case  Operator  of
         '*' : result:= number1 * number2;
         '/' : result:= number1 / number2;
         '+' : result:= number1 + number2;
         '-' : result:= number1 - number2;
    end;
    and get
    Code:
    Error C144 at line 592:17 - Illegal type of expression 
        [COLOR="RoyalBlue"]  for line [/COLOR][COLOR="Navy"]case Operator of[/COLOR]
    and interestingly
    Code:
    Error C129 at line 594:38 - Type conflict of operands
        [COLOR="RoyalBlue"]for line [/COLOR][COLOR="Navy"]'/' : result:= number1 / number2;[/COLOR]
    I have tried an equivalent code structure where Operator is an integer and there is no problem. Why just with strings and why wouldn't a published example work!
    Anyone else run into this? Or am I doing something dumb....
     
    Barrington, Nov 29, 2008
    #1
  2. Barrington

    ashleigh Moderator

    Joined:
    Aug 4, 2004
    Messages:
    2,391
    Likes Received:
    24
    Location:
    Adelaide, South Australia
    You probably want that "operator" to be a char instead of a string.

    String operations (on strings of arbitrary length) are not common in 9 out of 10 programming languages. Character and integer on the other hand...
     
    ashleigh, Nov 29, 2008
    #2
  3. Barrington

    Barrington

    Joined:
    Nov 29, 2008
    Messages:
    3
    Likes Received:
    0
    Hi Ashleigh. While you are right, changing "operator" the example to a char, rather than a variable does allow it to compile, my issue is with strings, as I am trying to parse the string that comes from the PAC comm port for various (string) commands coming from another device.
     
    Barrington, Dec 1, 2008
    #3
  4. Barrington

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    See the logic help file topic "Serial IO Examples".
     
    Darren, Dec 2, 2008
    #4
  5. Barrington

    PeteC

    Joined:
    Feb 18, 2008
    Messages:
    1
    Likes Received:
    0
     
    PeteC, Dec 4, 2008
    #5
  6. Barrington

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    Actually dividing integers isn't a problem in Pascal (or most languages for that matter). It just gives you a truncated integer result. The problem here is the use of the / operator which is a floating point division operator. For integer division you need the 'div' operator. i.e. result:= number1 div number2;

    As for the case statement, as Ashleigh points out, in Pascal the expression can only be of integer or char types (char is actually a byte internally). If you want to use strings you will have to revert back to nested if then else statements.

    Alternatively, if you are only interested in a single character in a string, just extract it as an array index (e.g. operator[5] to get the fifth character), which returns a char result and will work in a case statement.

    Ashley
     
    Ashley, Dec 5, 2008
    #6
  7. Barrington

    Barrington

    Joined:
    Nov 29, 2008
    Messages:
    3
    Likes Received:
    0
    Thanks guys,
    Yes I've been using nested "if"s to get around the problem but its getting a bit cumbersome, not sure if it compiles any cleanly.

    Bummer that strings are excluded from a case statement in Pascal. So used to using case in other languages (Basic, C) to parse strings in serial interface code. I haven't written Pascal for 25 years (until now)!

    Thaks for the "div", "/" difference. I obviously didn't read the help closely enough.
     
    Barrington, Dec 7, 2008
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.