INPUT

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

The INPUT command accepts input into the program and assigns it to a variable.

  • When used in the mainwin, INPUT waits for input from the user.
  • When used with files, INPUT retrieves data from a file which has been opened for input.
(See LINE INPUT for more on inputting data from files.)

Usage

Getting User Input

In the mainwin, using INPUT will cause the program to wait for the user to type a response and press the enter key. An optional text string prompts the user for a specific response. The input or response is assigned to a variable.

Getting Input From Files

For getting input from files, use INPUT followed by the file's handle and a variable name (or list of variable names) in which to store the input.
To get a specific number of characters from a file, use INPUT$(#handle, n).
Variable types must be the proper type to accept the inputted data.

Syntax

Getting User Input

  1. INPUT var
  2. INPUT ["text string";] var
  3. INPUT$(1)

Getting Input From Files

  1. INPUT #handle, var
  2. INPUT #handle, var1, var2
  3. INPUT$(#handle, n)

Hints

It looks like with # INPUT$(#handle, n) you cannot jump over LOF(#handle).
It is sure normal, but because of LOF bug - it wraps over 4GB,
it ends up like this:
if you have file of 4GB+100 bytes, you will be able to read only first 100 bytes.
Undocumented(?) feature: you CAN input several values in a single INPUT operator in a mainwin
input "Input a, b and c: "; a, b, c
print a, b, c

But you have to press ENTER after each value. So you get something like this:

Input a, b and c: 1
??2
???3
1             2             3

Examples

Getting User Input

' Getting input from a user in the mainwin
    input var$             ' Program waits for input and continues
                           ' when "enter" key pressed.
                           ' The inputted data is assigned to "var$".
    print var$
    end
' Note: Using INPUT without a prompt will cause a "?" to be displayed.
' To suppress the ?, use an empty prompt:
    input ""; var$         'note the semi-colon (;).

' and using a prompt
    input "Enter your name "; var$
    print var$
    end
' a prompt may be a string variable;  try this:
    prompt$ = "Enter your name "
    input ""; prompt$; var$
    print var$
    end
' Getting a response from a user in the mainwin
    input "Press enter key to continue "; dummy$
' Program halts until user presses enter key...
    print "You may now continue."
    end

Wait for any key to be pressed

    print
    print "Hit any key to continue ...";

    ' wait for any key to be pressed
    dummy$ = input$(1)

    print
    print
    print "Continuing !!!"
    print
    print "Finished !!!"
    end

Getting Input From Files

' A file must be opened for input before a program can INPUT any data.
' When a file is to be opened, it is given a handle, in this case we'll use #myfile.
' If no file exists with the handle "#myfile", INPUT returns an error.

    open "MyFile.dat" for input as #myfile
    input #myfile, var$
    print var$
    close #myfile
    end
' Note: INPUT will stop reading data from a file at the first comma encountered.
' To read a full line of data up to the carriage return or end of the file,
' use LINE INPUT.

' Try this demo
    open "tryme.txt" for output as #1       'create a file
    print #1, "tryme, if you dare"          'print something to it
    close #1                                'close file

    open "tryme.txt" for input as #2        'open the file for input
    input #2, my$                           'input the data and assign it to my$
    print my$                               'notice only the first part has been
                                            'assigned to my$ because a comma was
                                            'encountered before the end of the line.
    close #2
    end
' Getting multiple variables from a file
    open "MyFile.dat" for input as #myfile
    input #myfile, var1$, var2
    print var1$                             'prints the string variable
    print var2                              'prints the number variable
    close #myfile
    end
' Getting specific number of characters from a file
    open "MyFile.dat" for input as #myfile
    txt$=input$(#myfile, 15)                     'inputs 15 characters from the file
                                            'including any embedded commas
    print txt$
    close #myfile
    end

Useful Procedures

' Place a useful function or sub using this keyword here