INPUT statement (from keyboard)

Purpose: INPUT gets keyboard input from the user.


Syntax 1:

 INPUT Variable1, Variable2, ... 

Parameters:

  • Variable1, Variable2, ... Parameter list of comma separated variables which will receive the corresponding values from the keyboard input. The data type of these variables can be any of string, integer or floating point data types. Multi-dimensional arrays are also allowed as Variables in INPUT.

Syntax 2:

 INPUT "QuotedPrompt",  Variable1, Variable2, ...

Parameters:

  • "QuotedPrompt" [OPTIONAL] string literal prompt. A string variable cannot be used.
  • Variable1, Variable2, ... Parameter list of comma separated variables which will receive the corresponding values from the keyboard input. The data type of these variables can be any of string, integer or floating point data types. Multi-dimensional arrays are also allowed as Variables in INPUT.

Remarks:

See also the LINE INPUT statement which allows keyboard input of text containing comma or quotation mark.

INPUT statement (from file)

Purpose: INPUT will input, from a file, lines of comma separated values into a parameter list of comma separated variables in a manner similar to the QBasic INPUT # function. The values in the file can be any of string, integer or floating point data types.


Syntax:

 INPUT #FileHandle, Variable1, Variable2, ... 

Parameters:

  • #FileHandle The handle of file from which variable values will be retrieved.
  • Variable1, Variable2, ... Parameter list of comma separated variables which will receive the corresponding values from the keyboard input. The data type of these variables can be any of string, integer or floating point data types. Multi-dimensional arrays are also allowed as Variables in INPUT.

Example:


 DIM P, N!, E#, L!, D$, j, random!
 OPEN "TEST.TXT" FOR OUTPUT AS #1
 FOR j = 1 TO 10
   random! = j * 100.0 * RND
   PRINT #1, j , ",", random!, ",", 12356789.012345#, ",", j + 10, ",", "This string has spaces"
 NEXT
 CLOSE #1
 OPEN "TEST.TXT" FOR INPUT AS #1
 WHILE NOT EOF(#1)
   INPUT #1, P, N, E, L, D
   PRINT P, " ", N, " ", E, " ", L, " ", D
 WEND
 CLOSE #1

Remarks:

See the FINPUT statement which also will input, from a file, lines of comma separated values.

SCANERROR variable

INPUT returns an error code value to a BCX internal variable named SCANERROR.

Example 1: INPUT without a prompt


 DIM a!,b#,c$,d%,e$[10],f$

 PRINT "Input 1.2,123456.78901234,string,12345,arraystring,this is a string"
 INPUT a!,b#,c$,d%,e$[5],f$
 PRINT "single! ",a!
 PRINT "double# ",b#
 PRINT "string$ ",c$
 PRINT "integer ",d%
 PRINT "array string ",e$[5]
 PRINT "string with spaces",f$

Result

 
 single!  1.2
 double#  123456.78901234
 string$  string
 integer  12345
 array    arraystring
 sstring  this is a string
 

Example 2: INPUT with a prompt


 INPUT "Input 1.2,123456.78901234,string,12345,arraystring,this is a string ",a!,b#,c$,d,e$[5],f$
 PRINT "single! ",a!
 PRINT "double# ",b#
 PRINT "string$ ",c$
 PRINT "integer ",d%
 PRINT "array string ",e$[5]
 PRINT "string with spaces ",f$

Result


 single!  1.2
 double#  123456.78901234
 string$  string
 integer  12345
 array    arraystring
 sstring  this is a string
 

Example 3:


 CLS

 DIM a%
 DIM b!
 DIM c#
 INPUT "Type '1' then press ENTER  ",a%, b!, c#
 ? "====================="
 IF SCANERROR = -1 THEN PRINT "You did not enter enough data"
 ? "====================="
 ? a%
 ? b!
 ? c#
 ?

 INPUT "Now Type '1,2,3,4' then press ENTER  ",a%, b!, c#

 ? "====================="
 IF SCANERROR = 1 THEN PRINT "You entered too much data"
 ? "====================="
 ? a%
 ? b!
 ? c#

 INPUT "Finally, Type '1,2,3' then press ENTER  ",a%, b!, c#

 ? "====================="
 IF SCANERROR = 0 THEN PRINT "No data entry errors detected"
 ? "====================="
 ? a%
 ? b!
 ? c#

Result


 Type '1' then press ENTER  1
 =====================
 You did not enter enough data
 =====================
  1
  0
  0

 Now Type '1,2,3,4' then press ENTER  1,2,3,4
 =====================
 You entered too much data
 =====================
  1
  2
  3
 Finally, Type '1,2,3' then press ENTER  1,2,3
 =====================
 No data entry errors detected
 =====================
  1
  2
  3
 

BCX Console Sample Programs using INPUT statement.

S00.bas, S04.bas, S13.bas, S25.bas, S28.bas, S35.bas, S38.bas, S64.bas, S77.bas, S102.bas, S110.bas, S127.bas, S134.bas