17.01.2013 Views

AmstradCPC6128-hypertext-en-Sinewalker

AmstradCPC6128-hypertext-en-Sinewalker

AmstradCPC6128-hypertext-en-Sinewalker

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Fortunately, BASIC is a very forgiving language and will oft<strong>en</strong> help you by<br />

producing error messages on the scre<strong>en</strong> wh<strong>en</strong> you go wrong. BASIC also allows you to<br />

have 'afterthoughts', and new lines of program can be sandwiched in betwe<strong>en</strong> the<br />

existing lines with a minimum offuss.<br />

Writing a simple program<br />

OK let's get going. We'll write a program to keep a record of our fri<strong>en</strong>ds' names and<br />

telephone numbers. We'll call the program 'Telephone book'. Now let's apply the<br />

above rules: 'What should the program do?' and 'How should the results be<br />

pres<strong>en</strong>ted?'<br />

Well, the program should let you <strong>en</strong>ter up to say, 100 names and 'phone numbers for<br />

storage. Wh<strong>en</strong> you want a number, you should be able to type in the name and get<br />

back the number. In addition, if you're not sure how one of the names was originally<br />

<strong>en</strong>tered, you should be able to display a complete list of all the information on the<br />

scre<strong>en</strong>. Notice by the way, that we're automatically starting to consider the question<br />

of how the results ofthe program are to be pres<strong>en</strong>ted.<br />

Right, let's put finger to keyboard! We'll start off with the title at the beginning:<br />

10 REM teLephone book<br />

You don't have to put a title in a program, but wh<strong>en</strong> you start to accumulate quite a<br />

few programs, it helps to be able to know at a glance which is which.<br />

Next, we know that we want to be able to IN PUT (put in) a string of characters<br />

(somebody's name) into a variable; we'll call that variable N A M E $. The same applies<br />

to the telephone number, and we'll call that variable T E L $.<br />

Remember those example programs from the Foundation course? They used the<br />

IN PUT command for you to <strong>en</strong>ter the value ofthe variable, so if we type in:<br />

20 INPUT "<strong>en</strong>ter the name";NAME$<br />

30 INPUT "<strong>en</strong>ter the teLephone number";TEL$<br />

run<br />

You could <strong>en</strong>ter the name, for example: Joe. You could th<strong>en</strong> <strong>en</strong>ter the 'phone number,<br />

for example: 0277 230222<br />

The program has stored the information, but hasn't produced any printed results on<br />

the scre<strong>en</strong>. A section of program is therefore needed to retrieve the information, th<strong>en</strong><br />

display it. To get the values of N A M E $ and T E L $ at the mom<strong>en</strong>t, we'd use commands<br />

like:<br />

PRINT NAME$ .... and .... PRINT TEL$<br />

Chapter 2 Page 2 Beyond Foundations

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!