INPUT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "INPUT" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 7: Line 7:




{{Parameters}}
{{PageParameters}}
* A [[semicolon]] after the [[INPUT]] keyword keeps the entry on the same row after enter is pressed  and prevents the screen contents from rolling up.
* A [[semicolon]] after the [[INPUT]] keyword keeps the entry on the same row after enter is pressed  and prevents the screen contents from rolling up.
* The optional prompt "Question or statement text" must be a literal predefined [[STRING|string]]. '''The prompt cannot use a variable.'''
* The optional prompt "Question or statement text" must be a literal predefined [[STRING|string]]. '''The prompt cannot use a variable.'''
Line 24: Line 24:
** [[STRING|String]] and numerical variables can both be used in multiple entry requests separated by commas.
** [[STRING|String]] and numerical variables can both be used in multiple entry requests separated by commas.
** '''QB64'''  allows comma separated entries to be skipped by the user without generating an error.
** '''QB64'''  allows comma separated entries to be skipped by the user without generating an error.
* '''Use [[LINE INPUT]] for text input entries that may contain commas such as address or name entries.'''
* '''Use [[LINE INPUT]] for text input entries that may contain commas such as address or name entries.'''
* The user must press enter for the INPUT procedure to end. <!-- redundant: Multiple entries can be skipped. -->
* The user must press enter for the INPUT procedure to end.
* [[INPUT]] accepts the [[scientific notation]] letters D or E in [[SINGLE]] or [[DOUBLE]] numerical values.
* [[INPUT]] accepts the [[scientific notation]] letters D or E in [[SINGLE]] or [[DOUBLE]] numerical values.
* Numerical values starting with [[&H]], [[&O]] and [[&B]] can also be entered.
* Numerical values starting with [[&H]], [[&O]] and [[&B]] can also be entered.
<!-- not valid for QB64, not worth mentioning then denying: * INPUT removes all leading or trailing spaces in a string value entry. '''QB64 does NOT remove those spaces!''' -->
* The statement halts a program until enter is pressed, which may not be desired in programs using mouse input (see [[INKEY$]] loops).
* The statement halts a program until enter is pressed, which may not be desired in programs using mouse input (see [[INKEY$]] loops).
* Use [[_DEST]] [[_CONSOLE]] before INPUT statements to receive input from a [[$CONSOLE|console]] window.
* Use [[_DEST]] [[_CONSOLE]] before INPUT statements to receive input from a [[$CONSOLE|console]] window.
Line 35: Line 34:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Using a variable in an input text message using PRINT. INPUT prompts cannot use variables.
''Example 1:'' Using a variable in an input text message using PRINT. INPUT prompts cannot use variables.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|INPUT}} "Enter your name: ", name$
{{Cl|INPUT}} "Enter your name: ", name$
{{Cl|PRINT}} name$ + " please enter your age: ";: {{Cl|INPUT}} "", age% 'empty string with comma
{{Cl|PRINT}} name$ + " please enter your age: ";: {{Cl|INPUT}} "", age% 'empty string with comma
{{Cl|PRINT}} name$ + " how much do you weigh"; : {{Cl|INPUT}} weight%  'no text adds ? '' ''
{{Cl|PRINT}} name$ + " how much do you weigh"; : {{Cl|INPUT}} weight%  'no text adds ?
{{CodeEnd}}
{{CodeEnd}}
:''Explanation:'' Use an empty string with a comma to eliminate the question mark that would appear without the string.
:''Explanation:'' Use an empty string with a comma to eliminate the question mark that would appear without the string.
Line 44: Line 43:


''Example 2:'' How QB64 avoids a ''Redo from start'' multiple entry error. Use commas between values.
''Example 2:'' How QB64 avoids a ''Redo from start'' multiple entry error. Use commas between values.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO}}
{{Cl|DO}}
   {{Cl|INPUT}} "What is your name, age, and sex(M/F)"; name$, age%, sex$
   {{Cl|INPUT}} "What is your name, age, and sex(M/F)"; name$, age%, sex$
{{Cl|LOOP}} {{Cl|UNTIL}} age%        'loop until age is not 0
{{Cl|LOOP}} {{Cl|UNTIL}} age%        'loop until age is not 0
{{Cl|IF}} age% >= 21 {{Cl|THEN}} {{Cl|PRINT}} "You can drink beer!" {{Cl|ELSE}} {{Cl|PRINT}} "You cannot drink beer yet!"
{{Cl|IF}} age% >= 21 {{Cl|THEN}} {{Cl|PRINT}} "You can drink beer!" {{Cl|ELSE}} {{Cl|PRINT}} "You cannot drink beer yet!"
{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 58: Line 57:




''Example 3:'' Preventing screen roll after an input entry on the bottom 2 screen rows.  
''Example 3:'' Preventing screen roll after an input entry on the bottom 2 screen rows.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12


{{Cl|COLOR}} 14: {{Cl|LOCATE}} 29, 2 '          place cursor at beginning of prompt line
{{Cl|COLOR}} 14: {{Cl|LOCATE}} 29, 2 '          place cursor at beginning of prompt line
{{Cl|PRINT}} "Enter a name to search for... "; 'print prompt on screen with input to follow
{{Cl|PRINT}} "Enter a name to search for... "; 'print prompt on screen with input to follow
{{Cl|COLOR}} 15: {{Cl|INPUT}} {{text|;|red}} "", name$ '      get search name from user
{{Cl|COLOR}} 15: {{Cl|INPUT}} {{Text|;|red}} "", name$ '      get search name from user
{{Cl|LOCATE}} 29, 2: {{Cl|PRINT}} {{Cl|SPC}}(78); '      erase previous prompt
{{Cl|LOCATE}} 29, 2: {{Cl|PRINT}} {{Cl|SPC}}(78); '      erase previous prompt
n$ = {{Cl|UCASE$}}(name$) '                convert search name to upper case
n$ = {{Cl|UCASE$}}(name$) '                convert search name to upper case
{{Cl|COLOR}} 14'                        change foreground color to yellow
{{Cl|COLOR}} 14'                        change foreground color to yellow
{{Cl|LOCATE}} 29, 2: {{Cl|PRINT}} "Searching..."; 'print message
{{Cl|LOCATE}} 29, 2: {{Cl|PRINT}} "Searching..."; 'print message
{{Cl|SLEEP}}  
{{Cl|SLEEP}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}{{text|Enter a name to search for...|#FFFF00}} █
{{OutputStart}}{{Text|Enter a name to search for...|#FFFF00}} █
{{OutputEnd}}
{{OutputEnd}}
: ''Explanation:'' The {{text|red|red}} [[semicolon]] after INPUT acts like a semicolon after a [[PRINT]], which keeps the print cursor on the same row.
: ''Explanation:'' The {{Text|red|red}} [[semicolon]] after INPUT acts like a semicolon after a [[PRINT]], which keeps the print cursor on the same row.




Line 79: Line 78:
* [[INPUT$]], [[INKEY$]]
* [[INPUT$]], [[INKEY$]]
* [[LINE INPUT]], [[LOCATE]]
* [[LINE INPUT]], [[LOCATE]]
* [[INPUT (file statement)|INPUT #]], [[LINE INPUT (file statement)|LINE INPUT #]] {{text|(file input)}}
* [[INPUT (file statement)|INPUT #]], [[LINE INPUT (file statement)|LINE INPUT #]] {{Text|(file input)}}
* [[_KEYHIT]], [[_KEYDOWN]]
* [[_KEYHIT]], [[_KEYDOWN]]
* [[Scancodes]]
* [[Scancodes]]

Latest revision as of 22:31, 11 February 2023

The INPUT statement requests a STRING or numerical keyboard entry from the user.


Syntax

INPUT [;] "[Question or statement text]"{,|;} variable[, ...]
INPUT ; variable[, ...]


Parameters

  • A semicolon after the INPUT keyword keeps the entry on the same row after enter is pressed and prevents the screen contents from rolling up.
  • The optional prompt "Question or statement text" must be a literal predefined string. The prompt cannot use a variable.
  • Quotation marks are required except when a semicolon follows INPUT. A question mark will appear before the cursor.
  • A semicolon immediately after the text statement will display a question mark with a space after it. Use a comma for input statements.


Description

  • QB64 does not return Redo from start errors like QBasic did, as user input is limited to the scope of the variable type used.
  • Text entries (with a STRING variable]] can receive any characters, including numerical. QB64 will ignore commas in single variable text entries.
  • The type of the variable used to store user input determines the valid numerical range for value entries in QB64, with non-numerical characters limited to D, E, &H, &O or &B.
    • For example, if you use an INTEGER variable, as in INPUT "Initial value: ", myValue%, the valid range is -32768 to 32767.
    • INTEGER, LONG, and _INTEGER64 entries will ignore decimal points entered and will use all numbers.
  • INPUT can be used to get more than one variable value from the user. Do so by separating input variables with commas in the code.
    • The program must inform the user that more than one variable is requested, in order to enter each value separated with a comma at runtime.
    • String and numerical variables can both be used in multiple entry requests separated by commas.
    • QB64 allows comma separated entries to be skipped by the user without generating an error.
  • Use LINE INPUT for text input entries that may contain commas such as address or name entries.
  • The user must press enter for the INPUT procedure to end.
  • INPUT accepts the scientific notation letters D or E in SINGLE or DOUBLE numerical values.
  • Numerical values starting with &H, &O and &B can also be entered.
  • The statement halts a program until enter is pressed, which may not be desired in programs using mouse input (see INKEY$ loops).
  • Use _DEST _CONSOLE before INPUT statements to receive input from a console window.


Examples

Example 1: Using a variable in an input text message using PRINT. INPUT prompts cannot use variables.

INPUT "Enter your name: ", name$
PRINT name$ + " please enter your age: ";: INPUT "", age% 'empty string with comma
PRINT name$ + " how much do you weigh"; : INPUT weight%   'no text adds ?
Explanation: Use an empty string with a comma to eliminate the question mark that would appear without the string.


Example 2: How QB64 avoids a Redo from start multiple entry error. Use commas between values.

DO
  INPUT "What is your name, age, and sex(M/F)"; name$, age%, sex$
LOOP UNTIL age%        'loop until age is not 0
IF age% >= 21 THEN PRINT "You can drink beer!" ELSE PRINT "You cannot drink beer yet!"
END
What is your name, age, and sex(M/F)? Tom,24,M
You can drink beer!
Explanation: Try to enter text for the age value and it will not work. E or D should be allowed as decimal point numerical entries.


Example 3: Preventing screen roll after an input entry on the bottom 2 screen rows.

SCREEN 12

COLOR 14: LOCATE 29, 2 '          place cursor at beginning of prompt line
PRINT "Enter a name to search for... "; 'print prompt on screen with input to follow
COLOR 15: INPUT ; "", name$ '       get search name from user
LOCATE 29, 2: PRINT SPC(78); '       erase previous prompt
n$ = UCASE$(name$) '                 convert search name to upper case
COLOR 14'                        change foreground color to yellow
LOCATE 29, 2: PRINT "Searching..."; 'print message
SLEEP
Enter a name to search for...
Explanation: The red semicolon after INPUT acts like a semicolon after a PRINT, which keeps the print cursor on the same row.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage