Introduction to the BASIC Input Command
In this post, we’ll cover the BASIC Input Command. In our last post, we discussed the FOR NEXT loop. This allowed us to calculate our salary for the next 10 years. Our variables were coded into the program. Obviously, though, we would want to ask the user some questions. For example, we need to know the starting year. Additionally, we need to know the starting salary, and what the cost of living increase he expects each year.
The BASIC Input Commands allows for user input. In this case, we’ll modify our program to allow the user to answer some questions we need to know. That way, they will not have to modify the code to calculate their pay raises.
For this example, I’m using Altair Hard Disk BASIC, but it’s very similar in most any version. If you are using the Altair-Duino, you can easily boot to Hard disk basic. With switches 13 and 12 up, raise switch 0. Press AUX 2 Down to mount the disk. Then turn on switches 3, 2, and 1 (only). Press AUX 1 down to boot to Altair Hard Disk BASIC.
Our Previous Project
Before we begin, let’s take a look at our last program.
10 LET RAISE = 3 20 LET NUMYEARS = 10 30 LET YEAR = 1975 40 LET SALARY = 13720 50 LET X = 0 60 FOR X = YEAR TO YEAR + NUMYEARS STEP 1 70 PRINT "IN "; : PRINT X; : PRINT " YOU WILL MAKE "; : PRINT SALARY 80 SALARY = SALARY * (1 + (RAISE / 100)) 90 NEXT X
Basically, we hard coded the raise, number of years, starting year, and starting salary. We created a FOR NEXT loop to go through each year, and print the result to the display. The variable “SALARY” updates each time the loop is run. At this time, we will modify the code. We want the user to enter this data instead.
Adding the INPUT command
First, let’s modify line #10. Instead of the raise always being 3%, we’ll ask the user to enter the yearly raise. We’ll print to the screen telling the user to enter the raise amount. After the text to display, we user a semicolon. This tells the BASIC interpreter not to perform a line feed. In other words, whatever follows will be on the same line. After that, we use a colon to separate commands which are on line #10. The input prompt will be a question mark. For this reason, we want the text to be in a question form. The variable “RAISE” is set to whatever value the user enters.
10 PRINT "WHAT IS YOUR YEARLY RAISE AMOUNT IN PERCENT"; : INPUT RAISE
At this point, we’ll follow through on the same format with the other questions.
20 PRINT "HOW MANY YEARS SHOULD I CALCULATE";: INPUT NUMYEARS 30 PRINT "WHAT YEAR SHOULD I START ON";: INPUT YEAR 40 PRINT "WHAT IS YOUR STARTING SALARY THIS YEAR:";: INPUT SALARY
Your final program should look similar to this:
Test your work!
At this point, you are ready to test your work. Type RUN to run your program.
As you can see, our program is up and running!
Don’t forget to save your work. Make sure your disk is mounted. In Altair Hard Disk Basic, I simply type SAVE “SALARY”. You can load the program any time by typing LOAD “SALARY” . Try to experiment with various salaries and starting years. Additionally, you can modify the program for practice. Try adding a 5% raise every 5 years in addition to the cost of living adjustment!
Summary for the BASIC Input Command
In Short, the INPUT commands allows your program to accept user input. In this case, our variables were numbers. You can also use this with strings, which we will get into later.
— Ricky Bryce