Sheerpower Logo
P.2  Asking the user for Information (INPUT)

Asking the user for Information (INPUT)

The INPUT statement is highly valuable for console applications because it allows developers to interact directly with the user by prompting for and capturing input. Here's why it's particularly useful:

  • User Interaction: The INPUT statement enables console applications to interact with users by asking for information and then processing that data. This is essential for applications where the user's input is needed to proceed with operations or make decisions.
  • Flexibility: The INPUT statement offers various options to customize the input process, such as providing default values, setting timeouts, validating input, and formatting the input area. This flexibility allows developers to tailor the input process to specific needs, enhancing the user experience.
  • Data Validation: By using options like VALID, the INPUT statement can ensure the data entered by the user meets certain criteria before it is accepted. This helps prevent errors and ensures that the program receives valid and expected input.
  • Efficient Data Capture: The LINE INPUT and KEY INPUT variants of the INPUT statement allow for efficient capture of data. LINE INPUT reads entire lines, which is useful for capturing strings or commands, while KEY INPUT reads single keystrokes, which can be useful for navigation, choices, or gaming applications.
  • Enhanced User Experience: Features like default values, prompts, and formatted input areas make it easier for users to provide the necessary information. This can make console applications more intuitive and user-friendly.
  • Versatility: The INPUT statement can be used in various contexts, from simple data entry to complex, multi-step forms within a console application. It supports a wide range of scenarios, from basic prompts to sophisticated input dialogs.
  • Real-Time Feedback: Console applications can use the INPUT statement to provide immediate feedback based on the user's input. This can be useful for educational tools, quizzes, or interactive tutorials.
Summary: The INPUT statement is a fundamental tool in console applications, providing a bridge between the user and the program. It allows for dynamic and interactive applications that can respond to and process user input efficiently, making it indispensable in developing user-friendly console applications.

INPUT Statement

FORMAT:

[line] input var, var... 

[key] [line] input [  ['Prompt_text'] 
  [, prompt str_expr] [, erase] 
  [, at row, column] [, length num_expr] 
  [, default str_expr] [, VALID str_expr] 
  [, area num_expr, num_expr, num_expr, num_expr] 
  [, menu str_expr: str_var] :] var [,var. . .] 

EXAMPLE:

Example INPUT Statement

input 'Please enter your first name': first$ input 'Now, enter your last name': last$ line input 'What is your age': age print 'Hello '; first$; ' '; last$ print 'Age '; age end Please enter your first name? Sunny Now, enter your last name? Day Where do you live? San Diego, California Hello Sunny Day From San Diego, California

PURPOSE:

The INPUT statement is used to ask questions from the user and store the answers for use in a program.

DESCRIPTION:

The INPUT statement reads data typed by the user and assigns it to variables. var is a variable that the data is being assigned to. When Sheerpower executes an INPUT statement, it prints any prompt given and waits for the user's response. The user's response is then assigned to the variable(s) specified.

For information on INPUT from a text file, see File Handling

The user enters data in response to the INPUT statement. The input data must be the same data type as the variable, or Sheerpower generates an exception and reprompts. If, in response to the INPUT statement, the user presses the [Enter] key and:

  • the variable is numeric, Sheerpower assigns it a 0
  • the variable is a string, Sheerpower assigns it a null string ("")

Types of INPUT Statements

There are three types of INPUT statements:

  • INPUT: The INPUT statement reads one or more comma-separated data items.
  • LINE INPUT: The LINE INPUT statement reads an entire line of data.
  • KEY INPUT: The KEY INPUT statement reads a single keystroke.

INPUT Styles

There are three input styles:

Simple input

Example -- Simple Input Style

input 'Please enter your name': name$ print 'Hello '; name$ end Please enter your name? Toby Hello Toby

Free format multi-line text input

Example -- Free Format Multi-Line Text Input

line input area 5, 10, 8, 60: text$ print at 10, 1: 'Rewrapped text' wt$ = wrap$(text$, 1, 30) print wt$ end

This is an example of wrapped text. The text is wrapping. __________________________________________ ___________________________________________________ ___________________________________________________

Rewrapped text
This is an example of wrapped text. The text is wrapping.

Pop-up menus

Example -- Pop-up Menus

sample_menu$ = '%title "Options",' + 'Add, Change, Delete, Inquire' line input menu sample_menu$: selection$ print 'The menu option was '; selection$ end +--Options---+ | ADD | | CHANGE | | DELETE | | INQUIRE | +------------+ The menu option was CHANGE

Description of the KEY INPUT Statement

The KEY INPUT statement in Sheerpower is used to capture a single keystroke from the user. It is particularly useful in scenarios where quick, real-time input is required, such as in games, interactive prompts, or applications that require immediate user responses.

Example Code:
print 'See how quick you are.' key input prompt 'Press a key, quick!', & elapsed x: press$ print print 'You took'; x; 'seconds to Press '; press$; '.' end
Breakdown of the Example:
  1. Prompting the User: The program first prints the message "See how quick you are." to the screen.
  2. Capturing the Keystroke: The KEY INPUT statement is then used to prompt the user with the message "Press a key, quick!" and waits for the user to press any key. The time taken by the user to press the key is recorded in the variable x. The key pressed by the user is stored in the variable press$.
  3. Displaying the Result: After the user presses a key, the program prints the time taken (x seconds) and the key that was pressed (press$).
Key Features of KEY INPUT:
  • Prompting: You can provide a prompt to guide the user on what to do next.
  • Elapsed Time: The elapsed option allows you to measure how long it takes for the user to respond, which can be useful for timing-based applications.
  • Single Keystroke Capture: Unlike other input methods that capture entire lines or multiple characters, KEY INPUT is designed to capture just one keystroke, making it ideal for quick interactions.

This functionality makes the KEY INPUT statement a powerful tool in creating responsive and interactive console applications.


Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen