2

I've got a small code in TI BASIC on my TI-84 Plus C Silver Edition calculator that will determine correct dosage of drugs based on the patient's weight. For example, if aspirin is given at 5 mg per kg of patient weight (it isn't), then the code should tell me to give a 100kg patient 500mg of aspirin. However, the code is solving for every possible drug. Here it is:

PROGRAM:DRUG1
:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",A
:If A=IPPI
:Disp "DOSAGE",W*2
:If A=NEVO
:Disp "DOSAGE", W*0.5

So in this case, the two drugs are IPPI and NEVO. If I give a patient weight of 100kg, and choose IPPI, then I would expect to see

DOSAGE                 200

However, what I do see is

DOSAGE              200
DOSAGE               50

so apparently both "if" statements are running, even though I've given a only one value (IPPI). [The same error occurs when I set A as NEVO].

I've tried enclosing both If statements within Then...End as well, so the code would look like:

PROGRAM:DRUG1
:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",A
:If A=IPPI
:Then
:Disp "DOSAGE",W*2
:End
:If A=NEVO
:Then
:Disp "DOSAGE", W*0.5
:End

but that changes nothing. I'm pretty new to BASIC, so I'm sure there's a simple error that I can't see, but I'm stumped at the moment.

Mast
  • 1,788
  • 4
  • 29
  • 46
Fred Barclay
  • 834
  • 1
  • 13
  • 24

3 Answers3

6

You need to change the second Input command so the information is stored to a string instead of the numeric variable A. TI-84 series calculators have ten string variables in the [VARS][7] menu for this purpose.

Note also that you must compare the string against the string "IPPI" rather than the sequence of letters (numeric variables) IPPI. So your code could be:

:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",Str1
:If Str1="IPPI"
:Disp "DOSAGE: ",W*2
:If Str1="NEVO"
:Disp "DOSAGE: ",W*0.5

or more concisely:

:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",Str1
:Disp "DOSAGE:"
:If Str1="IPPI"
:Disp 2W
:If Str1="NEVO"
:Disp .5W
lirtosiast
  • 592
  • 4
  • 15
  • Thank you! I also added a line at the beginning "DelVar Str0" (I used Str0 instead of Str1), so that a previous value for Str0 wouldn't be carried over--just in case. – Fred Barclay May 12 '15 at 22:20
  • 2
    The DelVar is not necessary, because it is only possible to skip the Input command with the ON button, which terminates the program. – lirtosiast May 12 '15 at 22:44
3

You're trying to use variable names as strings.

:If A=IPPI

This isn't comparing a string to "IPPI", it's comparing a numeric variable A to the numeric value I*P*P*I, which I'm guessing results in 0 in your case.

Similarly, when you take input, if you enter IPPI, it's going to multiply those variables and assign A to be that product.

You'll need to use a string variable and quotes.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23
  • Thank you, that seems to be my problem. :) I've looked up using a string variable, and all I can seem to find is some thing on Equ->String that I can't understand, so if you could please give me an example, I'd really appreciate it! I just need to see once what I need to do. – Fred Barclay May 12 '15 at 02:42
  • @FredBarclay if you use STR1 instead of A, you don't need to put in quotes – Moshe Goldberg Jul 10 '15 at 11:22
2

The main problem with your program is that you aware assigning a string to a variable that only supports numbers. That leaves the new value of the variable the Boolean value of the string, True, which in the case of TI-BASIC is the value, 1. To fix this you need to assign it to a variable which supports characters in a string, in this case you can use STR1.

Private Caller
  • 121
  • 1
  • 5
  • This is wrong. Strings are not converted to booleans. If you enter `IPPI` at the `Input A` prompt, the numeric variable A will contain I*P*P*I, as MattPutnam's answer states. – lirtosiast Aug 26 '15 at 00:25
  • @lirtosiast yes, but even if he fixes it and does it as MattPutnam's answer stated, there would still be this issue – Private Caller Dec 07 '16 at 22:57