Alex
Alex

Reputation: 5

How do i read in a number to enter details with arrays and records?

I am having trouble reading the array of records using my ReadAllCars function. How do I get to read all 4 inputs of the Car records into the Cars array? I keep getting dynamic array error.

type
cars = record
    model:String;
    year:integer;

end;
car = array of cars;


function readCar(prompt: String): Cars;
begin
    WriteLn(prompt);
    result.model := ReadString('Car Model: ');
    result.year := ReadInteger('Year: ');
end;

**(this is my problem)**
function ReadAllCars(count:integer): Cars;
var
    carArray: array of cars;
    i:integer;
begin
    setLength(carArray, count);

    for i := 0 to high(carArray)do
    begin
        result.carArray[i] := readCar('Enter Car Details');
    end;
end;

procedure Main();

var
cars: Array of Car;
begin
    cars := ReadAllCars(4);
end;

Upvotes: 0

Views: 79

Answers (1)

LU RD
LU RD

Reputation: 34899

The problem is here:

function ReadAllCars(count:integer): Cars; 

This function returns type cars, which is declared as a record, not an array.

You have mixed up type Cars = record ... with a declared variable cars : array of cars.


This is how ReadAllCars should look like:

function ReadAllCars(count:integer): Car;
var
  i:integer;
begin
  setLength(Result, count);
  for i := 0 to high(Result)do
  begin
    result[i] := readCar('Enter Car Details');
  end;
end;

Upvotes: 2

Related Questions