ZetCode

FreeBasic Input Keyword

Last modified April 20, 2026

The FreeBasic Input keyword is used to read user input from the console. It pauses program execution and waits for the user to enter data followed by the Enter key.

Basic Definition

In FreeBasic, Input is a statement that reads data from the standard input (keyboard). It can read numeric values, strings, or multiple values in one statement.

The Input statement is essential for interactive console applications. It allows programs to receive dynamic input from users during runtime.

Basic String Input

This example demonstrates how to read a simple string input from the user.

input_string.bas
Dim fname As String
Print "Enter your name: ";
Input fname
Print "Hello, "; fname; "!"

Here we declare a string variable and use Input to read user input. The semicolon after the prompt keeps the cursor on the same line. The entered text is stored in the fname variable.

Numeric Input

The Input statement can also read numeric values directly.

input_numeric.bas
Dim age As Integer
Print "Enter your age: ";
Input age
Print "In 10 years you will be "; age + 10

This example reads an integer value from the user. FreeBasic automatically converts the input string to the numeric type. If non-numeric input is entered, a runtime error will occur.

Multiple Input Values

Input can read multiple values separated by commas in one statement.

input_multiple.bas
Dim x As Integer, y As Integer
Print "Enter two numbers separated by comma: ";
Input x, y
Print "Sum: "; x + y
Print "Product: "; x * y

This code reads two integers at once. The user must separate values with commas. The input is automatically split and assigned to the variables in order. Extra values are ignored.

Input with Prompt Message

The Input statement can include an optional prompt message.

input_prompt.bas
Dim color As String
Input "What is your favorite color? ", color
Print "Nice choice! "; color; " is a beautiful color."

Here the prompt message is part of the Input statement. This is more concise than separate Print and Input statements. The comma after the prompt adds a question mark and space.

Input with Line Input

For reading entire lines including commas, use Line Input.

input_line.bas
Dim address As String
Print "Enter your full address: ";
Line Input address
Print "Your address is: "; address

Line Input reads the entire line until Enter is pressed, including commas. This is useful when you need to read text that might contain delimiter characters.

Input with Validation

This example shows how to validate numeric input using a loop.

input_validation.bas
Dim number As Integer
Dim valid As Boolean = False
While Not valid
 Print "Enter a number between 1 and 100: ";
 Input number
 
 If number >= 1 And number <= 100 Then
 valid = True
 Else
 Print "Invalid input. Try again."
 End If
Wend
Print "You entered: "; number

This code repeatedly asks for input until a valid number is entered. The While loop continues until the valid flag is set to true. This pattern is common for robust input handling.

Input with Default Value

Here's how to implement input with a default value that can be overridden.

input_default.bas
Dim filename As String = "default.txt"
Dim temp As String
Print "Enter filename (default: "; filename; "): ";
Line Input temp
If Len(temp) > 0 Then
 filename = temp
End If
Print "Using file: "; filename

This example shows a filename input with a default value. If the user just presses Enter, the default is used. Otherwise, the input value is stored. The Len function checks for empty input.

Best Practices

  • Validation: Always validate user input to prevent errors.
  • Prompts: Make input prompts clear and descriptive.
  • Defaults: Provide default values when appropriate.
  • Types: Use the correct variable type for expected input.
  • Error Handling: Consider using error handling for numeric input.

This tutorial covered the FreeBasic Input keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.

AltStyle によって変換されたページ (->オリジナル) /