Can Select...Case command allow us to compare against words??
Vincenzo1309
Posts: 76
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
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
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".
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
Like using IF....THEN?? I think I have tried it, and it doesn't work.
Kindly advise.
Ask if there is anything you don't understand.
Jeff T.
Kindly advise.
There is no place for me to key in the numbers?
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andy Lindsay
Education Department
Parallax, Inc.