Pascal:错误尝试重写数组并帮助打印我的数组

时间:2017-05-29 09:19:59

标签: pascal freepascal

所以我正在研究这个pascal应用程序,它有一个菜单,你可以做多个事情。

输入相册(这是我的程序所做的)并尝试通过写入当前相册来编辑它时,我得到一个错误,如图所示。

enter image description here

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

  

(100,9)警告:函数结果变量似乎没有初始化

这是我的代码:

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.

1 个答案:

答案 0 :(得分:0)

警告在第100行引用的功能是

Human Created
SuperHuman Created
SuperHuman Class Gone

警告说:

  

警告:函数结果变量似乎没有初始化

实际上结果变量尚未初始化。

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

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

您应该改为编码:

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

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