String$ - variable conversion?

Hey once again,

So as part of my computing project I have a system that asks the user how much they wish to bet on a specific number. To do this I used the function:

CALL TD_Input("How much would you like to bet?", "Ok", betamount$, 1, result)

Where betamount$ is the amount the user wishes to bet.

However, I have to use 'betamount$' in calculations which I cannot do as it is a string. Is there any way for me to convert the value of 'betamount$' into a numerical variable?

(Note, the variable name has to be a string in the function, otherwise it returns an error saying 'Incorrect Type' or of a similar sort)

Many thanks,

Dillan

Comments

Hi, Try the VAL function,

Hi,

Try the VAL function, e.g.
LET bet=VAL(betamount$)

The function STR$ does it the other way around, e.g.

LET value$=str$(value)

Regards
Big John

It didn't work

I compared it to your solution, and it didn't work, this is my line of program:
PRINT "The colors are: red and black. What color do you want to be";
INPUT COLORA$
LET COLOR=VAL(COLORA$)
PRINT COLOR
END

Thanks John, is there anyway

Thanks John, is there anyway I can verify that the string to be converted contains only a number and not any letters? I've noticed that if I accidentally type in, say, a square bracket with a number the program will crash.

Many thanks

VAL function

Hi,

Try this defined function. It works the same way as VAL but doesn't crash when it meets a string character so it will evaluate 12345ABCDE for example.

DEF VALSTR(string$) !OK

LOCAL lens, temp$, hash, letter, negative, neg
LET lens = len(string$)
LET temp$ = ucase$(string$)
LET hash = cpos (temp$,"#")
IF hash > 0 then
LET temp$[hash:hash] = ""

END IF

LET letter = ncpos(temp$," 0123456789.-")

IF letter>1 then
LET temp$=temp$[1:letter-1]
LET letter=0
END IF

IF letter = 0 then
LET negative = 1
LET neg = cpos(temp$,"-")
IF neg > 0 then
LET negative = -1
LET temp$ = temp$[neg+1:lens]

END IF

LET temp$ = trim$(temp$)
IF len(temp$) = 0 then
LET valstr = 0
ELSE
LET valstr = val(temp$)*negative
END IF

ELSE
LET valstr = 0
END IF

END DEF

Regards
Big John