Search code examples
ti-basic

Trouble on "If" statement on TI-84 Plus C Silver Edition


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.


Solution

  • 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