Input and Output Tutorial I

This tutorial will teach you the fundamentals of input and output using the thinBasic Console module. If you are a complete beginner, this tutorial is a must read.

thinBasic input and output tutorial

In the last tutorial you learned a bit about how to use the Console module to display text in the console window. Since we're going to be using the Console module throughout the entire series of core programming tutorials, this tutorial and the next one will give you an introduction to the most essential ways of getting input and sending output.

Basic output

You already know how to tell thinBasic that you are going to use the Console Module. In case you forgot, whenver you want to use a module, you must tell thinBasic that you are going to use it first. You do this by the USES command. The code below tells thinBasic that we are going to use the Console module.

USES "Console"

CONSOLE_WRITELINE

You already know CONSOLE_WRITELINE. You used it in the last tutorial to display "Hello world" in the console window. This is output. Anything that is "sent out" of your game is output. Since the message "Hello World" is sent out of your game and into the console window, it's output.

In this tutorial we will try to say hello to the person who runs the script:

USES "Console"

CONSOLE_WRITELINE ("Hello user!")
CONSOLE_WAITKEY

Hmm, not very exciting… What about asking user for its name and then greet him properly? Sounds a good plan, doesn't it :)

Basic input

To read input from the user, we will use following command:

CONSOLE_READLINE

It will allow user to enter any text, and after pressing ENTER it will pass it to our script.

We need to store the name somewhere so we will create variable name, store input there, and then print it to screen:

USES "Console"

' -- Declare new string variable, to hold text
DIM name AS STRING

' -- First we will kindly ask
CONSOLE_WRITELINE ("Enter your name, please:")

' -- Then read the name
name = CONSOLE_READLINE

' -- And finally say hello!
CONSOLE_WRITELINE ("Hello "+name)

CONSOLE_WAITKEY
page_revision: 3, last_edited: 1206478546|%e %b %Y, %H:%M %Z (%O ago)