Sam Sam - 3 years ago 204
Pascal Question

Prompt for function not working

At the moment when I run the code it doesn't prompt me to type anything in and prints all the ID's and the registration numbers as 0 and manufacturer and model are blank. I need to be able to enter sets of details from 3 different cars and it print them in the way of the WriteCar procedure. How do I go about fixing this?

program carDetails;
uses TerminalUserInput;

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

Cars = Array of Car;

function ReadCar(const Prompt : String): Car;
begin
WriteLn(prompt);
ReadCar.ID := readInteger('Please enter the Car ID ');
ReadCar.Manufacturer := readString('Please enter the manufacturer of the car ');
ReadCar.Model := readString('Please enter the model of the car ');
ReadCar.Registration := readInteger('Please enter the registration number for the car ');
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
SetLength(result, count);
end;

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

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

begin
Main();
end.

Answer Source

As noted in a comment, you don't actually call your readCar procedure anywhere, so it never executes. Probably the simplest way to fix this is to add a loop to call it in your readAllCars procedure, which at the moment only allocates memory for the Cars array it returns. You can do this by changing your readAllCars to something like

function ReadAllCars(count: integer): Cars;
var
      i : Integer;
begin
  SetLength(Result, Count);
  for i := Low(Result) to High(Result) do
    Result[i] := readCar('Enter car details');
end;

Once you've done that, your program should run, but you'll notice a problem with your declaration of Registration. You've declared it as an integer, whereas a car registration normally contains letters to. So Registration needs to be a string, and you need to change how you read it from readInteger to readString.

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