previous page main index next page
 

Example 14

This is a game for two players. The program should ask one of the players to enter the name of a country and then the name of its capital. The screen is cleared and the program should then ask the other player to enter the capital of the country and print an appropriate message, depending on whether the answer is right or wrong.

1. Design a Solution

Our first attempt looks like this:

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

Step 1 becomes:

1.1  prompt player 1 for country
1.2  read in country
1.3  prompt player 1 for capital
1.4  read in capital
1.5  clear the screen


Step 2 becomes:

2.1  prompt player 2 for answer
2.2  read in answer

Step 3 is the crucial one and becomes:

3.1  if answer is correct
3.2 then
3.3     print 'correct' message
3.4 else
3.5     print 'incorrect' message

So the complete design is:
 

1.1  prompt player 1 for country
1.2  read in country
1.3  prompt player 1 for capital
1.4  read in capital
1.5  clear the screen

2.1  prompt player 2 for answer
2.2  read in answer

3.1  if answer is correct
3.2 then
3.3     print 'correct' message
3.4 else
3.5     print 'incorrect' message

2. Code the Program
  • type in the following program
  • you should note that CLRSCR - the instruction to clear the screen - may be different on some machines; see your teacher if you're not sure
     
PROGRAM quiz;       {a quiz game}

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

BEGIN
  WRITE('enter name of country ');
  READLN(country);
  WRITE('enter name of capital ');
  READLN(capital);
  CLRSCR;
  WRITE('what is the capital of ', country, '?');
  READLN(answer);
  IF answer = capital
  THEN
    BEGIN
      WRITELN('the capital is ', capital);
      WRITELN('well done, you got it right')
    END
  ELSE
    BEGIN
      WRITELN('sorry, you got it wrong');
      WRITELN('the correct answer is ', capital)
    END
END.

  • save the program as cities1.pas
  • compile and run the program
     

3. Test the Program

  • copy and complete this table for the different test data shown
     
country capital answer result
France Paris Paris correct
France Paris paris  
France Paris 77  
Venezuela Caracas Caracas  
USA Washington Washington  
A summary of the results might be:

"The program gave correct results when the answer matched the capital exactly - it is case-sensitive in as much as upper and lower case letters have to be the same. For example, "Paris" is not the same as "paris". If the user enters a number instead of some text then the answer will be wrong but it will not cause a 'run-time error'.

If the text is longer than the string - eight characters, in this case - then the word is simply shortened to fill the space allowed."

4. Evaluate the Solution

An evaluation of the program might be:

"The program meets the design specification and produces correct results when tested. Many improvements are, however, possible. An obvious one would be so that the user could use either upper or lower case letters - provided, of course, that the spelling was correct. The program could be developed such that a number of questions were asked and the user could then be given a score."
 

previous page main index next page
 
© 2001 by Mike Hardy