Bob Bob - 3 years ago 210
Pascal Question

Functions and procedures that call other functions and procedures

I need to:


  1. Write a function called ReadCar(): Car; that reads from the terminal values for each of the fields in a Car record and returns the completed record.

  2. Write a procedure called WriteCar(c: Car); that takes a car record and writes each of the fields to the terminal with a description for the field as well as the field value.

  3. Write a function called ReadAllCars(count: Integer): Cars; that calls your ReadCar() function count times and stores each car in Cars.

  4. Write a procedure called WriteAllCars(carArray: Cars); that calls your WriteCar() procedure for each car in carArray.



So far I believe I have done steps 1 and 2 correctly but I am not sure how to do steps 3 and 4. How should I begin those steps? By the end of this program I am supposed to be able to enter data for 3 cars and it print the data properly.

program carDetails;
uses TerminalUserInput;

type Cars = Array of Car;
Car = record
ID : integer;
Manufacturer : string;
Model : string;
Registration : integer;
end;

function ReadCar(): Car;
begin
WriteLn(promt);
ReadCar.ID := readInteger('Please enter the Car ID ');
ReadCar.Manufacturer := readString('Please enter the manufacturer of car '+ ReadCar.ID);
ReadCar.Model := readString('Please enter the model of car '+ ReadCar.ID);
ReadCar.Registration := readInteger('Please enter the registration number for car '+ ReadCar.ID);
end;

procedure WriteCar(c: Car);
begin
WriteLn('ID - ', c.ID);
WriteLn('Manufacturer - ', c.Manufacturer);
WriteLn('Model - ', c.Model);
WriteLn('Registration - ', c.Registration);
end;

function ReadAllCars(count: integer): Cars;
begin

end;

procedure WriteAllCars(carArray: Cars);
begin

end;

procedure Main();
var cars: Array of Car;
index: Integer;
begin
cars := ReadAllCars(3);
WriteAllCars(cars);
end;

begin
Main();
end.

Answer Source

I'm not going to do your coursework (from Swinburne Uni?) for you, but here are a few points.

You need to declare your Car record before your Cars array.

  type //Cars = Array of Car;
  Car = record
      ID : integer;
      Manufacturer : string;
      Model : string;
      Registration : integer;
  end;

Cars = Array of Car;

In ReadCar, your Prompt variable is undeclared (and mispelt). It should be

function ReadCar(const Prompt : String): Car;
begin
//    WriteLn(promt);
    WriteLn(Prompt);

Also in ReadCar, you need to convert Car.ID to a string before you can use it in your calls to readString, like so:

ReadCar.Manufacturer := readString('Please enter the manufacturer of car ' + IntToStr(ReadCar.ID));
ReadCar.Model := readString('Please enter the model of car ' + IntToStr(ReadCar.ID));
ReadCar.Registration := readInteger('Please enter the registration number for car ' + IntToStr(ReadCar.ID));

In ReadCars, you need to set the length of the array returned by it:

function ReadAllCars(count: integer): Cars;
begin
  SetLength(Result, Count);
end;

Having done all that, writeCars is actually very simple. All you need is

procedure WriteAllCars(carArray: Cars);
var
    i : Integer;
begin
   for i:= Low(carArray) to High(carArray) do
     WriteCar(carArray[i]);
end;

Using the Low() and High() functions sidesteps the issue of having to know the lower and upper bounds of an array declared the way your Cars one is (i.e. an array of record). Actually they are zero-based, not 1-based.

For some reason, SO's code-formatter doesn't do its normal stuff with the code in this answer, I'll try and tidy it up later.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download