Basic Input and Output
Input and Output, commonly called I/O for short, is one of the most rudimentary things that any programmer must learn to know. Without it software can't get any information into itself to process and it can't indicate anything to the end user. So lets learn I/O!
What is input? Input is any and all data entered into the program from the keyboard by the user. (later on you will learn there are more types of input than just the keyboard but for now this definition will work just fine!)
First thing to remember is get your program organized. Plan ahead enough so that when you need input you can get it all at once. It's much easier for the user to enter in several items at once then one at a time as the program actually needs them. So say that you have a program that organizes addresses and prints out labels. Ask for all the information; apartment number, street, city, state, zip, etc in the beginning even if you don't need the zip code till the end of the program when you print out the label. It would just be too confusing to the user.
Now that you're keeping that in mind how do you actually get something in? Use the input command silly!
Where variable is the type of variable that you want to get input of. String - variable$, single precision number - variable
Simple enough huh? Of course this is a rather simple and not of much use. If you run the code just like that all that will show up is a "?" and the computer will wait till the user types in something and presses the enter/return key. It works but how will the user know what kind of information they should type in? A date? A name? Their salary? They need some sort of prompt to indicate what to enter.
There are two ways to prompt a user. The first way is to simple print a message telling what to enter.
PRINT "Hello, World!"
The result of this line will be that the text in the quotes will be displayed on the screen.
So now you can do this:
PRINT "Enter your name: "
This works well enough but it seems like there should be a more logical way to do this. There is! The INPUT command also allows you to place your prompt right in the command. Just like this:
INPUT "Enter your name: ", name$
Run this code and on the screen it will display the text in quotes and wait for the user to into a string which will be stored in the name$ variable. Now depending on what you want there are two options you have. After entering the quoted text if you place a comma then you will not see a "?" after it. If you put a semicolon after the quoted text then you will see a "?" after it...
Well as much as people like to say BASIC is a simple language you'll find it's got plenty of tricks up it's sleeves...
What if you want to get multiple pieces of data. Well you could just use several input statements but why do that when you can use the abilities of BASIC to save you space and time! by simply putting a comma between several variables the user can enter them all in on the same line.
INPUT "Enter street address, city, state, zip code: ", street$, city$, state$, zip$
Now if the user was to enter say:
12 Evergreen Terrace, Springfield, Illinois, 12345
The computer would automatically put each of the parts into the appropriate variable for you. Neat huh? Yea but there are some issues that should be brought up. Most times as neat as this is it ends up causing more problems then it solves. If the programs just for you then you would probably know to make sure to format the data so it'll work. But what if your friend or family is entering the data? They might out of habit not put the comma after Illinois to separate the state and zip. As a result the computer will think the state is "Illinois 12345"! Something we don't want... So remember that if you must use multiple inputs make sure to clearly specify exactly how you want the information entered in.
You learned that using commas to separate pieces of data allows you to have multiple inputs on a single line. So what if you want to have some information entered that includes a comma? Looks like we have a problem!
Well luckily those boys that made BASIC were smart enough to think of this!
LINE INPUT "Enter a sentence with a comma in it: ", sentence$
Adding the word LINE to the beginning indicates that you want to accept any information regardless of punctuation.
Now you can safely have entire sentences entered without fear of the program trying to chop up the text and put it into different varialbes. It also lets you do things like ask for an address like "12 Evergreen Terrace, Springfield, Illinois 12345" to be kept in just a address$ variable. How handy little word can be...
What is output? Output is anything that is displayed on the computer screen. (Again this is a rather simplified deffintion as there are several other forms of output but it'll do good enough for here)
Well we already saw how to use the PRINT command to display some text, but as in BASIC style you aren't limited to what I showed you!
Will cause whatever data is in the variable to be displayed on the screen.
Once again this can be any type of variable you want; string, integer, single precision, etc. So now you can do something like this:
INPUT "Enter your name: ", name$
Ok so not that neat but at least you finally got the user to enter their name and then display the name back to them. You should be proud of yourself!
More Complex Printing
Well as great as the last bit of code was we all need to do more than just print one quoted piece of text or a variable. We need to print them all together. You could just use a bunch of PRINT commands but that's just wasteful and silly!
PRINT "Hello, "; name$; "! I hope you are feeling fine today."
Wow now that's just a little more like it isn't it! If you were to run this bit of code this is what would get displayed on the screen: (assume that the variable name$ equals Dexter)
Hello, Dexter! I hope you are feeling fine today.
You can make this lines just about as complex as you so desire. Just remember that it's ok to split up the output with multiple PRINT statements. Besides making it easier for you to organize it'll allow others that look at your code to know what your are doing! Nothing drives me crazier than seeing someone use a single PRINT command to display 4 lines worth of text.
Organizing In Tables
Ok so lets say we want to to print out a block of data in rows and columns. How are we going to do that?
Well we can just use the built in table abilities of BASIC. by simply putting commas between pieces of data there will automatically spaced out in predefined print zones. For example:
PRINT 1, 2, 3, 4, 5
Each zone is a 14 character wide column on the screen. So with a screen resolution of 80characters there can be 5 print zones. If any piece of data runs over the length of a zone then the next bit of data will be shift over to the next zone. Remember this when printing things like names and addresses.
Sometimes you need a little more control over how a variable is displayed. Say you just did a calculation of two numbers and want to display it as dollars and cents. If you just do a PRINT of the variable you might end up with it displaying several decimal places. It just doesn't look good does it!
To combat this problem we use the PRINT USING command.
PRINT USING "format identifiers"; variable
The following format identifiers are available
|#||used as a digit placeholder|
|.||used to indicate decimal placement|
|,||used to indicate a comma placement|
|$$||adds a floating dollar sign to the number|
The following example would be useful to display a numeric variable as a price:
PRINT USING "$$#,###.##"; price
Assuming that the price variable stored "123.456" in itself this is what would get displayed:
And best of all you aren't limited to just printing one variable out. You can add as many number blocks to it as needed. The following will work just as good as the first one.
PRINT USING "$$#,###.## $$#,###.##"; price1; price2
Well there you go folks! Hopefully by now you've learned a few ways to get user to feed your program data. As well you've figured out a few ways to display things and even format them so they look more professional. Good luck and happy programming!
Alan "Dexter" Barber
Webmaster, Basic Users Network
Copyright © 2002 John M. Spilling and BasicUsers.Net,
owned by Developers-United.net.
All rights reserved.