QB002 - Question/Answer Interaction With The User In QBasic

Languages : QBasic
Tools : QBASIC.EXE
Statements : Print, Line Input, Dim
Prerequisites : Install QBasic (from Tools CD #3 \ Microsoft Tools (part 2 of 2) \ QBasic - simply copy it onto your hard disk)
Estimated time : 5 to 10 minutes if understood first time - not including "For Fun".

Input + Memory + Recipe = Output

Programming is all about input and output.  You take a command or information from the user, you do something with it, and you display some (hopefully helpful) result to the user.

Programming is goal-oriented.  A bit like cooking, you usually set out with some idea of what you're hoping to create!  The question then becomes how to create it.  The lines of code are a bit like a recipe.  The ingredients (eggs, flour, etc) are comparable to the input that nearly every program requires and the cake (or biscuits, or whatever) are comparable to the output.  (Of course, you can sit down and just decide to write something, with no idea in advance what that will be.  Like some friends of mine who occassionally go into the kitchen to cook something, but have no idea in advance whether they'll even make something sweet or whether they'll make something savoury, let alone what exactly that will be!  :o)  )

Also, computers have a terrible memory!  Unless you tell the computer to remember a specific piece of information, it will forget it almost immediately, and once forgotten, there's usually no retrieving.  The flip side is that, when you do tell a computer to remember a piece of information, it can remember it flawlessly for even years and decades to come.  (If I asked you to add 3 and 5 and tell me the result, you could easily tell me that the answer is 8.  So could a computer.  But if I then asked you what numbers I had just asked you to add, you would probably be able to tell me, at least for a short time afterwards.  In contrast, a computer would not even remember that it had just added two numbers!  All that a computer knows about is right now, and by right now, we mean this very nanosecond.  The only memory it has of things it did in the past is whatever you as the programmer told it to record along the way.)

My First Kitchen Program

Ingredients ("input") : eggs, sugar, flour, water, salt

Recipe ("program") :

  1. Beat eggs.

  2. Mix sugar in with eggs.

  3. Mix flour, water and salt to make pastry.

  4. Spread pastry on bottom of baking tray.

  5. Pour egg and sugar mixture on pastry.

  6. Cook for 3 hours on 180 degrees celsius.

Hopeful "output" : scrumptious gooey sweet slice.

(Legal disclaimer - don't actually try this recipe!  You could burn the house down at step 6...  :o)  )

Consider this : what would happen if you had input and a recipe (i.e. a program), but no memory?  It'd be like cooking without a mixing bowl (or saucepan, or other receptacle).  You'd get up to the bit that says "mix eggs with sugar" and you'd say to yourself "where did those eggs go?  and where is that sugar?  I had them only a minute ago!".  The only thing is, computers are literally so forgetful that it would be asking "What eggs?  What sugar?!?".

Think about it!  If you had no memory, and you tried to follow the above recipe, you'd say "what eggs?!?" at instruction 1, "what sugar?!?  what eggs?!?" on instruction two, and so on, and so forth!  So the four essential aspects of a typical program are 1) input; 2) a series of instructions (the "recipe"); 3) a list of things that the computer must remember (or else it'll just forget everything); and finally 4) a desired outcome.

SO, how do all these pieces fit together when writing programs?

Consider the following program :

Dim PlayerName As String
Line Input "What is your name? ", PlayerName
Print "You have a really nice name, " + PlayerName + "."

The first line is all about memory.  It tells the computer, "Hey, I'm going to want you to remember a piece of information that's called 'PlayerName'."  Note two things (that we're going to elaborate on further in later lessons) : 1) any time we want the computer to remember something, we must give a name to the thing we want it to remember.  2) the computer can remember different types of information - for example, it can remember plain old boring numbers, or it can remember series of characters such as those you can type on a keyboard or save in a text file.  The latter are known as Strings.  So in the example program above, we've said "Hey computer, I want you to remember a variable-length series of characters, and we'll call that series of characters 'PlayerName'.

FYI : These named memory items are known as "variables".

The second line is all about input.  Actually, we've used a joint input + output instruction here.  First it outputs by displaying the text "What is your name?" on the screen, and then it waits for the user to type anything they want.  Whatever they type gets stored in the memory space we called PlayerName.  Input is terminated by the user pressing the Enter key, regardless of whether that's the first key they press or the two hundred and first.

The third line is all about output.  There we tell the user how nice their name is.  Notice how we build one long "string" out of three smaller "strings".  The first string is between the first two quote marks, i.e. "You have a really nice name, ".  The third string is between the final two quote marks, i.e. "."  The second string recalls from memory the thing we've called "PlayerName", which happens to be whatever the user typed in the previous instruction.

Now that's perhaps the most detailed, laboured description you'll ever read on such a simple program, but the principles apply immediately to programs of vastly greater size.  Make sure you understand them well because from now on we'll largely take them for granted.

For Fun

What do you think would happen with the following program?

Dim PlayerName As String
Line Input "What is your name? ", PlayerName
Print "You have a really nice name, " + PlyrNm + "."

Here we show one way to use number memory (as opposed to generic string memory that we've been using up until now) :

Dim PlayerName As String
Dim PlayerAgeString As String
Dim PlayerAge As Integer
Line Input "Name? ", PlayerName
Line Input "Age? ", PlayerAgeString
PlayerAge = Val(PlayerAgeString)
Print "Wow " + PlayerName + ", soon you will be " + Str$(PlayerAge + 1) + "!"


OK, things get technical here.  We're getting into future lessons but see if you can follow along (and don't worry if you can't).  Any time we read from the keyboard, we HAVE to read into a string variable not a number variable.  This is because the user could type anything.  So the sixth line takes the string that the user typed and tries to convert it into a plain old boring number.  Notice what happens when you run the program if you type something other than a number!  Welcome to your first "run-time error"!!!  :o)  Also note the use of "STR$" in the final line.  We're trying to concatenate several different strings into one long string.  Unfortunately, QBasic doesn't know how to "implicitly" convert a plain old boring number into a string.  (You'd think it would be simple, but QBasic won't do it.)  By saying "STR$" the way we do, we're "explicitly" telling QBasic to convert that number into a string.  (You can find out more about STR$ by putting the text cursor on the word STR and pressing F1.  This will take you into the help and tell you more than you need to know.)  Also notice that the output is a little bit ugly, because two spaces appear between "you are" and the number, not one.  Why is this so?  It is a result of the STR$ function, which for some reason, insists on inserting a space at the beginning of the string that it returns.  That space, plus the space we already have in the string ", you are ", results in a double space.  There are several different ways you could fix the double-spacing issue.  The first is dead obvious, but not so nice, being to remove the space from the string ", you are ".  A second, slightly nicer way, is to use the LTrim$ function.  To do this, you would replace "STR$(PlayerAge)" with "LTRIM$(STR$(PlayerAge))".  Voila!

Would it confuse the computer if we tried to have two named memory items with the same name?  e.g. :

Dim Info1 As String
Dim Info1 As Integer
Line Input "Name? ", Info1
Line Input "Age? ", Info1

If so, why would it confuse the computer?

Are there any restrictions on names you can give to things the computer must remember?  Yes - "variables" as these are known, cannot have any spaces or punctuation in their name.  You are limited strictly to letters and digits.  Incidentally, in geek-speak, letters are known as "alpha characters", digits as "numeric characters", and a combination of letters and digits is known as "alphanumeric characters".  Hence, we would say that QBasic variables (i.e. named memory locations in QBasic) must have alphanumeric names.  (Also incidentally, most other languages allow the use of the underscore character - i.e. "_" - as well as alphanumeric characters, in variable names, but QBasic does not.)

By the way, have you noticed how limited this kind of programming is, just asking questions and regurgitating the answers?  As of next lesson we're going to start looking at loops and control structures that bring in the real power of programming.  Hang tight 'til then!

Copyright (C) Jonathan Field 2004
Version 20041013WedB
Comments / Suggestions