Turbo Pascal 7.0 Guide
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.
Type Tprod =record
description : string ;
price : real;
VAR myproducts : array[1..5] of Tprod;
VAR prodsearch, n : integer;
FOR n:= 1 TO 5 DO
writeln('Enter the name of product ', n, ' ');
writeln('Enter the price of product ',n, ' ');
writeln('Enter a product number to find ');
writeln('Product ', prodsearch, ' is ', myproducts[prodsearch].description, ' £', myproducts[prodsearch].price:4:2);
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.
- 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.
- 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.
- Use a loop to check whether each answer is correct and store the running total of marks in an integer variable.
- End the program by telling the user how many questions they got right.