Pascal:尝试重写数组时出错并协助打印我的数组

Pascal: Error trying to rewrite array and assistance with printing my array

所以我正在开发这个 Pascal 应用程序,它有一个菜单,您可以在其中执行多项操作。

进入相册(这是我的程序所做的)并尝试通过覆盖当前相册来对其进行编辑后,出现如图所示的错误。

除了警告之外,编译时没有错误:

(100,9) Warning: Function result variable does not seem to initialized

这是我的代码:

program MusicPlayer;
uses TerminalUserInput;

type
    // You should have a track record
    TrackRec = record
        name: String;
        location: String;
    end;
    type TrackArray = array of TrackRec;
    GenreType = (Pop, Rap, Rock, Classic);
    AlbumRec = Record
        name: String;
        genre: GenreType;
        location: array of TrackRec; // this and track should be track: array of TrackRec
        numberOfTracks: Integer;
        tracks: TrackArray;
    end;
type AlbumArray = array of AlbumRec; // this should be an array of AlbumRec




function ReadGenre(prompt: String): GenreType;
var
    option: Integer;
begin
    WriteLn('Press 1 for Pop');
    WriteLn('Press 2 for Rap');
    WriteLn('Press 3 for Rock');
    WriteLn('Press 4 for Classic');
    option := ReadInteger(prompt);

    while (option<1) or (option>3) do
    begin
        WriteLn('Please enter a number between 1-4');
        option := ReadInteger(prompt);
    end;

    case option of
        1: result := Pop;
        2: result := Rap;
        3: result := Rock;
    else
        result := Classic;
    end;
end;

function CheckLength(prompt: string): Integer;
var
    i: Integer;
begin
    i := ReadInteger(prompt);
    while (i < 0) or (i > 20) do
    begin
        WriteLn('Please enter a number between 1-20');
        i := ReadInteger(prompt); 
    end;
    result := i;
end;


function ReadTracks(count: Integer): TrackArray;
var
    i: Integer;
begin
    setLength(result, count);
    for i := 0 to High(result) do
    begin
        result[i].name := ReadString('Track Name: ');
        result[i].location := ReadString('Track Location: ');
    end;
end;

function ReadAlbum(): AlbumRec;
begin
    result.name := ReadString('What is the name of the album?');
    result.genre := ReadGenre('What is the genre of the album?');
    result.numberOfTracks := CheckLength('How many tracks are in the album?');
    result.tracks := ReadTracks(result.numberOfTracks);
end;

function ReadAlbums(count: Integer): AlbumArray;
var 
    i: Integer;
begin
    SetLength(result, count); 
    for i := 0 to High(result) do
    begin
        result[i] := ReadAlbum();
    end;
end;

function ChangeAlbum(count: Integer): AlbumArray;
var
    i: Integer;
begin
    for i := count to count do
    begin
        result[i] := ReadAlbum();
    end;
end;

procedure PrintAlbum(count: Integer; album: array of AlbumRec);
var
    i: Integer;
begin
    if count = 1 then
    begin
        for i := 0 to High(album) do
        begin
            WriteLn('Album Number: ', i);
            WriteLn('Album name is: ', album[i].name);
            WriteLn('Album genre is: ', album[i].genre);
        end
    end;

    for i := 1 to count - 1 do
    begin
    WriteLn('Album name is: ', album[i].name);
    WriteLn('Album genre is: ', album[i].genre);
    end;
end;

procedure PrintTrack(tracks: TrackArray);
var
    i: Integer;

begin
    i := ReadInteger('Which track number do you wish to play?');
    i := i - 1;
    WriteLn('Now playing track: ', tracks[i].name);
    WriteLn('Track location: ', tracks[i].location);
end;

function CheckIfFinished(): Boolean;
var answer: String;
begin
    WriteLn('Do you want to enter another set of tracks? ');
    ReadLn(answer);
    LowerCase(answer);
    case answer of 
        'no': result := true;
        'n': result := true;
        'x': result := true;
    else
        result := false;
    end;
end;

procedure Main();
var
    i, count, select, change: Integer;
    albums: AlbumArray;
begin
    WriteLn('Please select an option: ');
    WriteLn('-------------------------');
    WriteLn('1. Read Albums');
    WriteLn('2. Display Albums');
    WriteLn('3. Select an Album');
    WriteLn('4. Update an Album');
    WriteLn('5. Exit');
    WriteLn('-------------------------');
    repeat
        i := ReadInteger('Your Option:');
        case i of  
            1: 
            begin 
                count := ReadInteger('How many albums: ');
                albums := ReadAlbums(count);
            end;

            2:
            begin 
                WriteLn('1. Display All Albums');
                WriteLn('2. Display All Albums by Genre');
                select := ReadInteger('Your Option: ');
                if i = 1 then
                    begin
                        PrintAlbum(select, albums);
                    end;
                // if i = 2 then
                // WriteLn('1. Pop');
                // WriteLn('2. Rap');
                // WriteLn('3. Rock');
                // WriteLn('4. Classic');
                // albums := ReadAlbums(count);
            end;

            3: 
            begin
                select := ReadInteger('Which album would you like to play? ');
                PrintAlbum(select, albums);
                PrintTrack(albums[select-1].tracks);
            end;

            4:
            begin
                change := ReadInteger('Which album would you like to edit?');
                albums := ChangeAlbum(change);
            end;
        end;
    until i = 5;
end;

begin
    Main();
end.

第 100 行警告所指的函数是

function ChangeAlbum(count: Integer): AlbumArray;
var
    i: Integer;
begin
    for i := count to count do
    begin
        result[i] := ReadAlbum();
    end;
end;

警告说:

Warning: Function result variable does not seem to initialized

而且result变量确实还没有初始化。

虽然函数的设计是错误的。您正在尝试修改数组中的现有元素。您不应该返回一个新数组。虽然该功能不是必需的。你应该简单地删除它。然后你需要看一下你调用函数的地方。

change := ReadInteger('Which album would you like to edit?');
albums := ChangeAlbum(change);

您应该改为这样编码:

change := ReadInteger('Which album would you like to edit?');
albums[change] := ReadAlbum();

我没有检查你程序中的任何其他内容。如果还有其他问题,我不会感到惊讶。我刚刚尝试解决您提出的具体问题。