GFAsimpleio

From Atari Wiki
Revision as of 12:25, 13 October 2011 by Admin (talk | contribs)
Jump to navigation Jump to search

Simple I/O

Great you're still there. Today we will learn how to do simple input and output operations on screen. This will allow you to make your programs interactive for the first time.

Printing data to screen

You actually already know a GFABASIC command to write data on screen. This command is PRINT. It is very versatile and you can extend it in various ways.

Syntax is simple, PRINT where is whatever sort of data you want to print on screen. That can be variables, numbers, the result of a calculation, a string or a mix of them all. You can even special commands to your PRINT statement for screen control such as cursor positioning. A few examples for the PRINT command can be found here:

PRINT 10+5
PRINT x%
PRINT 10;20;30
PRINT 10,20,30
PRINT "Hello!"
PRINT "y= ";y
PRINT "x=";x;" y=";y;" z=";z
PRINT "Your name is ";nam$
PRINT AT(5,5);"AT() is one of my favorites"

These are the most simple variations of the PRINT command. Try them out on your own to see their effects. Try with defining a few sample variables.

Now why do you write PRINT "y =";y instead of PRINT "y =",y, well that is quite simple. Using ; will add the following data directly behind your text without altering the cursor position while the , will advance the cursor to the next vertical tabular position. You can use that to align your data in tables on screen. In short, if you want to write data directly to some sort of prompt or behind some text, use the ; notation. Put a ; as the last data on your PRINT statement to let the cursor stay on the current line. You can use this to prevent a scrolling on the last line of the screen or if you simply want to split writing of prompt and data into two lines of code. Technically speaking giving the ; last will suppress a carriage return and linefeed.

If you want to write direct text onto screen which is not contained in a string variable, just enclose it in double quotes " like PRINT "Hello!" which will write Hello! on screen.

Screen control

Now that you know how to write your data on screen, you will also want to know how to handle screen output in detail. How do I leave a line of text blank might you ask? Write simply PRINT without any data behind to output a blank line on screen. Try this 3 lines program:

PRINT "Hello!"
PRINT
PRINT "This is the first example for screen control!"

As you see it prints the greeting and the other line with an empty line between. Quite easy don't you think?

A very important thing is how to clear the screen. For obvious reasons, you'll sometimes prepare a screen layout that requires you not to have other text or old data on screen. You'll simply clear the screen with the following command. Please note that this will also erase any other graphics on screen as the Atari ST does not distinguish between text and pixel graphics.

CLS

A neat thing is to write on screen exactly on a position where you want and not following the listed flow of ordinary PRINT statements. If you did try the first example in this chapter, you have noticed the AT() command. This special addition for PRINT allows you to position the cursor freely on screen so you can write your data where you want. Let's try the following example program:

CLS
PRINT AT(1,1);"Top left"
PRINT AT(5,13);"Middle line, text indented 5 chars"
PRINT AT(20,25);"bottom line";
INPUT "",dummy$

Press the RETURN key to terminate the program. You'll learn soon about the INPUT statement.

Syntax for PRINT AT(); is PRINT AT(column, row);, where row 1 is on top of the screen and column 1 on the left end. Column and row can be variables, expressions or simply a plain number. Valid PRINT AT() commands are:

PRINT AT(1,5);"Hello"
PRINT AT(5+x%,10);"x"
PRINT AT(4+8,y%);"y = "

How many character positions do I have at my hand you may ask. This depends on the current screen resolution. You have always 25 lines of text regardless of the current screen resolution. You have 40 columns of text in ST-LOW and 80 columns in ST-MED and ST-HIGH. A ST-MED or ST-HIGH screen shows the double amount of text compared to ST-LOW. I recommend running applications that do lots of screen output in these resolutions.

Be careful with line 25. If you don't use a ; last on your PRINT statements in this line, the screen will scroll up. Keep that in mind if you want to use line 25.

There are more commands you can use with PRINT like SPC() and TAB(). Refer to your GFABASIC manual or command reference on them.

Formatting output with PRINT USING

There are built in commands for formatting data on output. I will only explain how to use that for numeric data. Refer to your command reference to learn how to apply it for strings.

Generic syntax is PRINT USING "<format string>",expression. The format string defines how you want your data to be formatted on screen while expression is what you want to write with this format. Expression can be a number, a variable or a full expression.

Build the format string from the following characters.

PRINT USING format string
Character in

format string

usage
#
The hash mark will stand for a single digit in  your

output data.

.
A . will mark the position of the decimal point in your

output.

+/-
A + or - will mark the position of the sign. In case you use

the +, a positive sign will be printed as well while it is suppressed for positive numbers if you use -.

,
Use a , if you need delimiters for thousands.

See the following examples on how to use them.

PRINT USING "+###.##",150.467
PRINT USING "-#,###.##",-1500
PRINT USING "##.#",x

Experiment a bit with them, it's quite easy. For more options of PRINT USING consult your GFABASIC manual or command reference.

Gathering user input

Finally you can make your program interactive and allow the user to enter data into the computer. To understand this sub chapter, you have to fully understand variables as explained in chapter 2 because all user input is stored in variables.

To enter numbers into the computer, use the INPUT statement in one of the following ways. Your entered data is stored in the named variable. If you give a string variable, you can enter text while you can only enter numeric data if you use a numeric variable. A minus sign and optional decimal point are allowed for numeric input.

INPUT "x= ",x
INPUT "What is your name? ",your_name$

This will prompt the user to enter a value for x which will be stored into a float variable. You can then use this variable in your program as normal, doing calculations with it. Your program will stop until the RETURN key has been pressed to terminate the input.

You can read more than one variable with one INPUT statement, just list your variables where you want your input to go to with separating commata.

PRINT "enter 3 values, separated with commata (eq 3,4,5):"
INPUT x%,y%,z%

The user has then to enter commata at the appropriate places to tell which input goes to which variable. To the example above the user would respond with 5,6,7. As always you can then reuse the entered values in expressions and calculations.

CLS
INPUT "Enter a value for x:",x
PRINT "x = ";x
INPUT "What is your name?",your_name$
PRINT "Your name is ";your_name$;"."
PRINT "Bye, ";your_name$;"!" 

While entering strings you may have already noticed that GFABASIC will treat entering a comma again as a delimiter, effectively cutting your string at that comma. Use the command LINE INPUT instead of INPUT to read strings.

LINE INPUT ">",txt$

You can now enter strings with a comma in and it will be saved to the string variable as well. You can read multiple strings with LINE INPUT as well but the user has to press the RETURN key terminating each string to be entered.

An INPUT statement will erase the former contents of a variable in any case and will place the entered values in them.

Progress check

Like in the previous chapter, answer these little questions to verify your advancement in learning GFABASIC.

  1. I want to output a blank line. I use the command: ...
  2. I want to output my data on the third line from top and indented by 10 columns. I use the command: ...
  3. I want to output the value of variable x%. I use the command: ...
  4. I want to prompt the user to enter a float variable and put the input in a variable called z. I use the command: ...
  5. I want to output the value of this variable z with optional sign, 3 digits before the decimal point and 4 digits behind the decimal point. I use the command: ...
  6. Write a program that prompts the user for 2 numbers, adds these numbers and outputs the result nicely formatted on screen.

If you still make mistakes when answering these questions, I suggest to read the chapter again.

back to GFA Tutorial