2017-09-23 108 views
0

我無法使用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; 

回答

2

的問題是在這裏:

function ReadAllCars(count:integer): 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;