previous page main index next page
 

Example 20

Here's the first attempt at a design for Example 14 again:

1.  get information from player 1
2.  get answer from player 2
3.  print appropriate message

Each of these three steps will become a procedure. Let's choose names for them as follows:
 

step name
1. get information from player 1 get_info
2. get answer from player 2 get_answer
3. print appropriate message message
 
So the program looks like this just now:
 
PROGRAM quiz;             {a quiz game}

VAR
  country : STRING[8];
  capital : STRING[8];
  answer  : STRING[8];

BEGIN                     {main program starts here}
  get_info;
  get_answer;
  message
END.

 
As before, though, if you try to compile this you'll get error messages because there are three procedures - get_info, get_answer and message - which we haven't yet coded. We'll now put in the first two of these and you can complete the other one yourself:
 
PROGRAM quiz;             {a quiz game}

VAR
  country : STRING[8];
  capital : STRING[8];
  answer  : STRING[8];

PROCEDURE get_info;       {information from player 1}
BEGIN
  WRITE('enter name of country ');
  READLN(country);
  WRITE('enter name of capital ');
  READLN(capital);
  CLRSCR
END;

PROCEDURE get_answer;     {answer from player 2}
BEGIN
  WRITE('what is the capital of ', country, '? ');
  READLN(answer)
END;

{the other procedure goes in here}

BEGIN                     {main program}
  get_info;
  get_answer;
  message
END.

  • enter the code above and save it as cities2.pas
  • the procedure message below has been done for you, except that the lines have been jumbled up - you need to put them in the right order and put the procedure in the correct place in the program...

PROCEDURE message;    {print appropriate message}
ELSE
WRITELN('wrong - the answer is ', capital)
IF answer = capital
WRITELN('correct - well done!')
THEN
END;
BEGIN

previous page main index next page
 
© 2001 by Mike Hardy