Blitz:Shell Input/Output

From Amiga Coding
Jump to: navigation, search

Input and output using the shell is quite simple in Blitz Basic / AmiBlitz, and generally similar to most other BASIC dialects.

Shell Output

Print

The Print statement is used to print one or more items directly to the shell window. Each argument will be shown directly after the previous argument with no space in between so you need to separate your items yourself if needed. For example:

a = 6
Print "The value of a is", a

Will show the following in the shell window:

The value of a is6

Adding spaces to the messages fixes the output:

a = 6
Print "The value of a is ", a

Any combination of arguments is possible: string constants (which are surrounded by quote marks), string variables, constants, numerical variables, numerical expressions, or returned values of a function.

Print will leave the cursor exactly at the end of what ever is output to the console, so

Print "Message 1"
Print "Message 2"

will output

Message 1Message 2

NPrint

NPrint is almost identical to Print, except that it adds a newline at the end of its output. So

NPrint "Message 1"
NPrint "Message 2"

will output

Message 1
Message 2

Note: At least one argument is required for Print/NPrint statements. So, unlike other BASICs, to skip a line you need to provide something to print. In this case, a null string can be used. For example:

NPrint "Message 1"
NPrint ""
NPrint "Message 2"

will output

Message 1

Message 2

AmiBlitz added the ability to include escape characters in strings. See String Handling for more information.

Shell Input

Edit

Edit is used for inputting numbers via the shell window, and is similar to the Input command used in other BASIC dialects. Inputting strings using Edit will return a 0 (zero), read more about variable types.

Edit is a function, so returns the value input by the user. It halts program flow and waits for the user to enter a number, finishing their input with the Return or Enter key. Unlike other BASICs, it does not display a "?" character while waiting on input, and a prompt cannot be added as an argument so needs to be provided separately. For example:

Print "What number did you choose? "
a = Edit(5)
NPrint "Thank you. You chose ", a

In this example, the numbers typed by the user will appear after the end of the question, since the Print command doesn't add a newline.

Edit$

Edit$ is almost identical to Edit, except that it accepts text entered by the user and returns a string instead. For example:

Print "What is your name? "
a$ = Edit$(30)
NPrint "Nice to meet you, ", a$

Inputting numbers using Edit$ will require the returned string to be converted to a number type before it can be used as a value.

Note: Both Edit and Edit$ require one argument: the maximum number of characters to accept. If the user enters more characters than this number (5 in the example), the result will only contain the first 5 characters typed.