2

Is it possible to output the value of a variable with the Menu( Command in TI-Basic? Although the Menu( command is very rudimentary, I believe it best allows for user simplicity within the specific confines of my program. In the code, I have a value stored for a variable, and then want to output this number within a menu. For example:

    5->A
    Menu("SELECT AN OPTION","YOU HAVE A",1)

However, within quotation marks, the letter A is the output instead. Is there a way to use different syntax to achieve this? If not, what is the best alternative to a menu-type interface?

AstroCB
  • 12,337
  • 20
  • 57
  • 73

2 Answers2

2

Yes, it is possible. The problem is TI-Basic does not really support merging of numbers into strings (most scripting languages have pretty good string parsing). You can do this:

5->A
Menu("SELECT AN OPTION","YOU HAVE"+"2 COOKIES",1)

Which would combine the strings "YOU HAVE" and "2 COOKIES" into "YOU HAVE 2 COOKIES", but if you try "NUM"+5 it will give you some sort of type error because String and number cannot be combined.

The workaround to this (which is really tedious to do in mass segments of code) is this:

"?                                //this is to store a string in the Ans variable
For(X,1,1+int(log(N               //loop for the number of digits of the number
sub("0123456789",iPart(10fPart(N/10^X))+1,1)+Ans //this line pretty much converts
the digit in question of the number (X) to another number, and then sub allows it
to select that number from a string (e.g. sub("0123",3,1) will output 2). Really
I have no idea about the math behind actually getting the digit in question.
End
sub(Ans,1,length(Ans)-1→Str1      //output all except the first character of the
string because it is a placeholder "?" from the frist line.

[please note this is definitely not the only way to convert a number to a string, but I feel its the fastest and cleanest ever discovered. Also, this method only works for int numbers (non-decimal), and to make it work with negitives you just have to change 1+int(log(A to 1+int(log(abs(A]

Like I said, I don't know how iPart(10fPart(N/10^X))+1 actually works, but as I said it just outputs 1 higher than the value of the digit in question. (1 higher because the 0 takes a spot in the string as item 1).

Anyways, the full code for you would be something like:

5->A
"?
For(X,1,1+int(log(A
sub("0123456789",iPart(10fPart(N/10^X))+1,1)+Ans
End
Menu("SELECT AN OPTION","YOU HAVE "+sub(Ans,1,length(Ans)-1),1)

TI-Basic is quite weak for small flaws like this (others being lack of functions, etc), but regardless its a fun language to code in.

Edit: I also saw you asked for alternate methods, so heres some. You could always use the Output( or Text( commands to draw to the homescreen/graphscreen respectively, but this requires that you write a whole menu program. One small advantage to this is that you can do stuff like:

5->A
Output(1,1,A

In whatever position you like, without having to do that whole mess of conversion to a string. If the number is alone, it is displayed as a "number", but the problem comes in where you try to merge it with a string. (like the string "YOU HAVE") So even with a menu you could do:

5->A
Menu("SELECT AN OPTION",A,1)

But thats not too helpful because theres nothing describing what the value is.

Lemon Drop
  • 2,113
  • 2
  • 19
  • 34
1

If you have a TI-84+ CE, you can use the toString() command and then concatenate your strings, like this:

5->A
toString(A->Str1
Menu("SELECT AN OPTION","YOU HAVE "+Str1,1
Johnny Dollard
  • 708
  • 3
  • 11
  • 26