Turbo Pascal 7.0 Guide
User-Defined Types

Records

Pascal allows you to create your own data types by combining a series of variables together into a record. This can be useful when you want to process those different elements together.

PROGRAM products;
USES crt;
Type Tprod =record
   description : string [20];
   price : real;
END;
VAR myproducts : array[1..5] of Tprod;

VAR prodsearch, n : integer;
BEGIN
   clrscr;
   FOR n:= 1 TO 5 DO
   BEGIN
      writeln('Enter the name of product ', n, ' ');
      readln(myproducts[n].description);
      writeln('Enter the price of product ',n, ' ');
      readln(myproducts[n].price);
   END;
   writeln;
   writeln;
   writeln('Enter a product number to find ');
   readln(prodsearch);
   writeln('Product ', prodsearch, ' is ', myproducts[prodsearch].description, ' £', myproducts[prodsearch].price:4:2);
   readln;
END.

When creating user-defined types, we tend to use the letter T as a prefix to the type name. This helps to distinguish the type definition from the variables of that type.

When declaring instances of the type, you use the Type name in the data type part of the declaration.

The individual elements of the record are accessed by the variable name and the property name.

Exercises

  1. Design a record structure to store multiple choice questions for a small quiz program. Each record must include the question text, the text for 3 possible answers and the letter that corresponds to the correct answer.
  2. Use an array to store 3 questions using the record structure. Use a loop to output the questions to the screen and accept the user's answers in the form a, b, or c. Store these answers in an array.
  3. Use a loop to check whether each answer is correct and store the running total of marks in an integer variable.
  4. End the program by telling the user how many questions they got right.