INPUT$: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Removed protection from "INPUT$") |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
{{PageDescription}} | |||
* Keyboard input is limited to the [[INTEGER]] {{Parameter|numberOfBytes%}} (characters) designated by program. | * Keyboard input is limited to the [[INTEGER]] {{Parameter|numberOfBytes%}} (characters) designated by program. | ||
* The keyboard is the default device when a file or port number is omitted. The {{Parameter|numberOfBytes%}} is number of key presses to read. | * The keyboard is the default device when a file or port number is omitted. The {{Parameter|numberOfBytes%}} is number of key presses to read. | ||
Line 16: | Line 17: | ||
* Use [[_DEST]] [[_CONSOLE]] before INPUT$ is used to receive input from a [[$CONSOLE|console]] window. | * Use [[_DEST]] [[_CONSOLE]] before INPUT$ is used to receive input from a [[$CONSOLE|console]] window. | ||
=== QBasic/QuickBASIC === | |||
==QBasic/QuickBASIC== | * {{Parameter|numberOfBytes%}} could not exceed 32767 in [[BINARY]] files or a QBasic error would occur. | ||
* {{Parameter|numberOfBytes%}} could not exceed 32767 in [[BINARY]] files or a QBasic error would occur. | |||
* Ctrl + Break would not interrupt the QBasic program until there was a full INPUT$ key entry. In '''QB64''' Ctrl + Break will immediately exit a running program. | * Ctrl + Break would not interrupt the QBasic program until there was a full INPUT$ key entry. In '''QB64''' Ctrl + Break will immediately exit a running program. | ||
Line 24: | Line 24: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' A keyboard limited length entry can be made with a fixed blinking cursor. Entry must be completed before it can be shown. | ''Example 1:'' A keyboard limited length entry can be made with a fixed blinking cursor. Entry must be completed before it can be shown. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|LOCATE}} 10, 10, 1 'display fixed cursor at location | {{Cl|LOCATE}} 10, 10, 1 'display fixed cursor at location | ||
year$ = {{Cl|INPUT$}}(4) 'waits until all 4 digits are entered | year$ = {{Cl|INPUT$}}(4) 'waits until all 4 digits are entered | ||
PRINT year$ 'display the text entry | PRINT year$ 'display the text entry | ||
{{CodeEnd}} | {{CodeEnd}} | ||
''Example 2:'' Reading bytes from a text file for an 80 wide screen mode. | ''Example 2:'' Reading bytes from a text file for an 80 wide screen mode. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|LOCATE}} 5, 5, 1 'locate and display cursor | {{Cl|LOCATE}} 5, 5, 1 'locate and display cursor | ||
{{Cl|OPEN}} "Diary.txt" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1 'open existing text file | {{Cl|OPEN}} "Diary.txt" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1 'open existing text file | ||
text$ = {{Cl|INPUT$}}(70, 1) | text$ = {{Cl|INPUT$}}(70, 1) | ||
{{Cl|LOCATE}} 5, 6, 0: PRINT text$ 'print text and turn cursor off | {{Cl|LOCATE}} 5, 6, 0: PRINT text$ 'print text and turn cursor off | ||
{{CodeEnd}} | {{CodeEnd}} | ||
''Example 3:'' Getting the entire text file data as one string value. | ''Example 3:'' Getting the entire text file data as one string value. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|OPEN}} "Diary.txt FOR {{Cl|BINARY}} AS #1 'open an existing file up to 32767 bytes | {{Cl|OPEN}} "Diary.txt FOR {{Cl|BINARY}} AS #1 'open an existing file up to 32767 bytes | ||
IF {{Cl|LOF}}(1) <= 32767 THEN Text$ = {{Cl|INPUT$}}(LOF(1), 1) | IF {{Cl|LOF}}(1) <= 32767 THEN Text$ = {{Cl|INPUT$}}(LOF(1), 1) | ||
{{Cl|CLOSE}} #1 | {{Cl|CLOSE}} #1 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
:''Explanation:'' The IF statement gets the entire contents when the file size is less than 32768. The program can then work with the string by using [[MID$]] or [[INSTR]]. Note: A text file string will also have '''CrLf''' line break end characters [[CHR$]](13) + [[CHR$]](10). | :''Explanation:'' The IF statement gets the entire contents when the file size is less than 32768. The program can then work with the string by using [[MID$ (function)|MID$]] or [[INSTR]]. Note: A text file string will also have '''CrLf''' line break end characters [[CHR$]](13) + [[CHR$]](10). | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[INPUT]], [[LINE INPUT]] {{ | * [[INPUT]], [[LINE INPUT]] {{Text|(keyboard input)}} | ||
* [[INPUT (file mode)]], [[INPUT (file statement)|INPUT #]], [[LINE INPUT (file statement)|LINE INPUT #]] {{ | * [[INPUT (file mode)]], [[INPUT (file statement)|INPUT #]], [[LINE INPUT (file statement)|LINE INPUT #]] {{Text|(file input)}} | ||
* [[OPEN]], [[LOC]] {{ | * [[OPEN]], [[LOC]] {{Text|(file)}} | ||
* [[LOCATE]] {{ | * [[LOCATE]] {{Text|(cursor on/off)}} | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 00:36, 26 February 2023
The INPUT$ function is used to receive data from the user's keyboard, an open file or an open port.
Syntax
- result$ = INPUT$(numberOfBytes%[, fileOrPortNumber])
Description
- Keyboard input is limited to the INTEGER numberOfBytes% (characters) designated by program.
- The keyboard is the default device when a file or port number is omitted. The numberOfBytes% is number of key presses to read.
- INPUT$ will wait until the number of bytes are read from the keyboard or port. One byte per loop is recommended with ports.
- RANDOM opened file bytes can be up to the LEN = recordLength statement, or 128 if no statement is used.
- fileOrPortNumber is the number that was used in the OPEN AS statement.
- Returns STRING values including spaces or even extended ASCII characters.
- Backspace key results in the CHR$(8) character being added to an entry.
- Use LOCATE , , 1 to view the cursor entry. Turn the cursor off using LOCATE , , 0.
- Use _DEST _CONSOLE before INPUT$ is used to receive input from a console window.
QBasic/QuickBASIC
- numberOfBytes% could not exceed 32767 in BINARY files or a QBasic error would occur.
- Ctrl + Break would not interrupt the QBasic program until there was a full INPUT$ key entry. In QB64 Ctrl + Break will immediately exit a running program.
Examples
Example 1: A keyboard limited length entry can be made with a fixed blinking cursor. Entry must be completed before it can be shown.
LOCATE 10, 10, 1 'display fixed cursor at location year$ = INPUT$(4) 'waits until all 4 digits are entered PRINT year$ 'display the text entry |
Example 2: Reading bytes from a text file for an 80 wide screen mode.
LOCATE 5, 5, 1 'locate and display cursor OPEN "Diary.txt" FOR INPUT AS #1 'open existing text file text$ = INPUT$(70, 1) LOCATE 5, 6, 0: PRINT text$ 'print text and turn cursor off |
Example 3: Getting the entire text file data as one string value.
OPEN "Diary.txt FOR BINARY AS #1 'open an existing file up to 32767 bytes IF LOF(1) <= 32767 THEN Text$ = INPUT$(LOF(1), 1) CLOSE #1 |
- Explanation: The IF statement gets the entire contents when the file size is less than 32768. The program can then work with the string by using MID$ or INSTR. Note: A text file string will also have CrLf line break end characters CHR$(13) + CHR$(10).
See also
- INPUT, LINE INPUT (keyboard input)
- INPUT (file mode), INPUT #, LINE INPUT # (file input)
- OPEN, LOC (file)
- LOCATE (cursor on/off)