如何读取数字以输入数组和记录的详细信息?

时间:2017-09-23 11:16:06

标签: pascal

我无法使用ReadAllCars函数读取记录数组。如何将Car记录的所有4个输入读入Cars数组?我不断得到动态数组错误。

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;

1 个答案:

答案 0 :(得分:2)

问题在于:

function ReadAllCars(count:integer): Cars; 

此函数返回类型cars,它被声明为记录,而不是数组。

您已将type Cars = record ...与声明的变量cars : array of cars混淆。

这就是ReadAllCars的样子:

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;