Shop OBEX P1 Docs P2 Docs Learn Events
Can Select...Case command allow us to compare against words?? — Parallax Forums

Can Select...Case command allow us to compare against words??

Vincenzo1309Vincenzo1309 Posts: 76
edited 2009-01-14 16:42 in Learn with BlocklyProp
Dear all concerned,

I have been trying to write a program that can function as a calculator. Whenever I tried to key in the operation type (eg. add), the program have no response.
Is it because Select....Case commands cannot allow us to compare words? If not, what can I do?
The program is written as below, kindly advise.........

' {$STAMP BS2}
' {$PBASIC 2.5}

firstNum VAR Byte
secondNum VAR Byte
Operation VAR Word
Ans VAR Byte



DEBUG "Enter first number: "
DEBUGIN DEC firstNum

DEBUG "Enter second number: "
DEBUGIN DEC secondNum

DEBUG "Choose the arithmetic operation that u want to perform: "
DEBUGIN STR Operation\8

SELECT Operation
CASE "add"
Ans = firstNum + secondNum
DEBUG "The answer is : "
DEBUG DEC Ans


CASE "subtract"
Ans = firstNum - secondNum
DEBUG "The answer is : "
DEBUG DEC Ans


CASE Operation = "multiply"
Ans = firstNum * secondNum
DEBUG "The answer is : "
DEBUG DEC Ans


CASE Operation = "divide"
Ans = firstNum / secondNum
DEBUG "The answer is : "
DEBUG DEC Ans


ENDSELECT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-11 17:35
    The compare operator "=" only works on 16-bit numeric values which are limited to 0-65535. The compiler will translate single characters to their equivalent numeric value for convenience and you can use two characters in an expression since that fits in 16 bits.

    You will get into trouble trying to store 8 bytes ("Operation\8") in a 2 byte variable (Operation is a word).

    You could declare Operation as an 8 byte array like Operation var byte(8). You could use the 1st two characters as a numeric value with the expression Operation(0) << 8 + Operation(1). You could then compare it to a two character constant like "s" << 8 + "u" or "m" << 8 + "u".
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-01-11 21:41
    Hi Vincenzo, if you are not too concerned about using the variable Operator as a string then your program will actually work using·a single·character representing·standard calculator symbols + - / *

    I tried your code with these minor modifications

    Operation VAR Word change to Operation VAR Byte

    DEBUGIN STR Operation\8 change to DEBUGIN Operation

    CASE "add" change to CASE "+"· CASE "subtract" change to· CASE "-"· CASE Operation = "multiply"· change to CASE "*"· CASE Operation = "divide" change to CASE"/"


    Jeff T.

    EDIT·· almost forgot to mention that if you change the variable Ans from a byte to a word it·will give you a little more scope.







    Post Edited (Unsoundcode) : 1/11/2009 9:49:32 PM GMT
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-01-13 14:04
    Hi, what if I really want the variable Operator as a string, is there anything that I can do to make it happen?

    Like using IF....THEN?? I think I have tried it, and it doesn't work.

    Kindly advise.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-01-14 05:12
    Hi Vincenzo, there's usually always a way and usually always more than one way. I attach what I thought was the least complex, I like simple.

    Ask if there is anything you don't understand.

    Jeff T.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-01-14 14:30
    Hi, I am sorry, as I am a novice in programming. I do not understand the program. All I know is that when I type "addition" in the transmit window, the "Additional code here" appears.

    Kindly advise.

    There is no place for me to key in the numbers?
  • edited 2009-01-14 16:40
    SELECT...CASE is explained in the BASIC Stamp Manual (see Table of Contents). There's also an example you can try in What's a Microcontroller, Chapter 8, Activity #5 (pages 245-250). (I found that one using the book's Index.)

    To download the BASIC Stamp Manaul, go to www.parallax.com -> Downloads -> BASIC Stamp Documentation

    To download the What's a Microcontroller text, go to www.parallax.com -> Downloads -> Stamps in Class Downloads. You can also find it in the Downloads sections of kit pages that feature the What's a Microcontroller text. Example:

    BASIC Stamp Activity Kit
    http://www.parallax.com/tabid/134/ProductID/313/Default.aspx

    Andy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • edited 2009-01-14 16:42
    Also, there's a DEBUGIN command that allows you to enter numbers into the Debug Terminal. For an example of how this works, see pages 120-123 in What's a Microcontroller, and also check DEBUGIN in the BASIC Stamp Manual.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
Sign In or Register to comment.