我如何读取数字以输入数组和记录的详细信息?
How do i read in a number to enter details with arrays and records?
我在使用 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;
问题在这里:
function ReadAllCars(count:integer): Cars;
这个函数returns类型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;
我在使用 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;
问题在这里:
function ReadAllCars(count:integer): Cars;
这个函数returns类型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;