User prompt input problem.

Blitz3D Forums/Blitz3D Beginners Area/User prompt input problem.

Galdy(Posted 2011) [#1]
How do I get user input? I want to enter a number at a prompt and of course branch based on that input. As an excersise, i tried the following. No matter what number I enter at the prompt I get a zero from "print a." What am I doing wrong?

Print "Enter number"
Input a

Print a


While Not KeyDown(1)

Wend End


Yasha(Posted 2011) [#2]
Two problems here:

1) You need to declare a as a string if you want it to hold a string.

2) The parameter to "Input" is the prompt, not the place to store the result (all builtin Blitz3D functions return their result as the function return value).

Try this:

Local a$    ;Declare a as a string variable
a = Input("Enter number")    ;Get the input and store in a
Print a

While Not KeyDown(1)
Wend
End


As you've already found out, you don't need to formally declare local variables in Blitz3D, but you do need to supply a type (%, #, $ for integer, float, string respectively; or a custom name for reference types) the first time the variable is used (supplying the type a second time doesn't do anything), or the compiler will assume it's an integer, which is why it displayed "0".


Galdy(Posted 2011) [#3]
Thanks Yasha. I guess you just put the String variable as an example uh? It worked fine for entering an interger value since that is like you said, the default. Anyway, now i know how to use input. :)


jfk EO-11110(Posted 2011) [#4]
Actually, it's

a=input$("please enter something ")

The $ indicates it will return a string. Using "a" as an integer here will cause an automatic type conversion. In many other languages this would result in an "Type Error" - it's good to have Blitz :). Converting a String to an Int will use the left characters up to a letter that is not numeric. "Hello" would be zero, "123b321" would be 123.

Last edited 2011


Yasha(Posted 2011) [#5]
Jfk, I hate to be a pain, but nope, that's totally wrong.

Input always returns a string, regardless of whether you append a sigil - in fact, using type sigils on function calls never has any effect at all (at least, I can't remember if the compiler notices if you actually get the sigil wrong.. it probably complains).

The type conversion is done on assignment of the result to the variable, depending on the type of the variable (assume it's done by = ), so the rest of what you said is correct. However, it's defined by the variable, not by Input - Blitz doesn't allow you to change the type signature of functions.

It's worth pointing out that a lot of people don't like automatic type conversion, because it can lead to unexpected bugs. (Personally I think that this should be addressed by redesigning the program to not move variables to unexpected places, but there is at least a good reason why type errors were invented.)


jfk EO-11110(Posted 2011) [#6]
Don't get me wrong, but I said exactly the same. I didn't mean you can control the output type of "Intput$", using the "$". It might be omitted (and won't crash), but basicly it's part of the correct syntax, and I really meant the same: return type is always a string. The automatic type conversion is done "behind the curtain" since you don't need to store the return string of Input$, Blitz does that for you.

It's really good that I didn't write the docs - they would be rather confusing :) Then again Confuzius' Wisdom may be useful from time to time. Eg:

Man standing in water, feet get wet. <:-|