Console Input & Output

8.1 CLS, LOCATE, COLOR

The 16 text colors (SCREEN 0):

Foreground colors 16–31 blink in SCREEN 0. CSRLIN returns the current row and POS(0) the current column. WIDTH 80, 50 changes the text screen to 50 rows.

CLS                          ' clear screen, cursor to top-left
LOCATE 10, 30                ' row 10, column 30 (1-based; text screen is 25x80 by default)
PRINT "Centered-ish text"
COLOR 14, 1                  ' yellow on blue (foreground, background)
PRINT " Warning! "
COLOR 7, 0                   ' back to light gray on black
LOCATE , , 0                 ' hide the cursor (third argument)

8.2 PRINT USING — formatted output

, (before the decimal point) floating dollar sign fill leading spaces with * show sign (start) / trailing minus (end) first character of string fixed-width string field print the next character literally

If a number is too wide for the field, it is printed with a leading %.

PRINT USING "###.##"; 3.14159           '   3.14
PRINT USING "#####"; 42                 '    42
PRINT USING "$$#,###.##"; 1234.5        '  $1,234.50
PRINT USING "**#####.##"; 12.5          ' ****12.50  (asterisk fill)
PRINT USING "+##.##"; -2.5              ' -2.50
PRINT USING "#.##^^^^"; 12345.678       ' 1.23E+04   (scientific)
PRINT USING "Name: &  Score: ###"; "Ada"; 97
PRINT USING "!"; "Yes"                  ' Y   (first character only)
PRINT USING "\   \"; "Programming"      ' Progr (field of 2 + number of spaces characters)

8.3 INPUT and LINE INPUT

INPUT re-prompts with "Redo from start" if the user types text where a number is expected.

LINE INPUT reads one string variable — the entire line exactly as typed.

INPUT$(n) waits for exactly n keypresses without echo: k$ = INPUT$(1).

INPUT "Enter two numbers separated by a comma: ", a, b
PRINT "Sum:"; a + b

INPUT "Your city"; city$            ' commas in the input would split it!
LINE INPUT "Full address: "; addr$  ' reads the whole line, including commas and quotes
PRINT addr$

8.4 Non-blocking keyboard checks (preview)

More in Chapter 25.

PRINT "Press any key..."
DO: k$ = INKEY$: LOOP UNTIL k$ <> ""
PRINT "You pressed: "; k$
SLEEP                    ' alternatively: wait until a key is pressed

8.5 Other output helpers

Classic BASIC: Commodore BASIC had no LOCATE; programmers printed cursor-control characters (CHR$(19) home, CHR$(147) clear). Applesoft used HOME, HTAB, VTAB. PRINT USING existed in Microsoft disk BASICs but not in most 8-bit ROM BASICs. FreeBASIC: Cls, Locate, Color, Print Using, Input, Line Input, Inkey (no $ needed in -lang fb), Sleep — nearly identical. VB.NET: Console.Write, Console.WriteLine, Console.ReadLine(), Console.ReadKey(), Console.SetCursorPosition(col, row), Console.ForegroundColor = ConsoleColor.Yellow, formatting with String.Format("{0,8:F2}", x), x.ToString("N2"), or interpolation $"{x:F2}".

PRINT TAB(20); "column 20"
PRINT "A"; SPC(10); "B"          ' 10 spaces between
PRINT STRING$(40, "=")           ' a line of 40 equals signs
WRITE "Ada", 36, 1.5             ' "Ada",36,1.5  (quoted, comma-separated)
BEEP                             ' system beep