2017-05-29 31 views
1

所以我正在研究这个pascal应用程序,它有一个菜单,您可以在其中执行多项操作。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. 
+0

在这里的一个问题。我编辑它是如此。 –

回答

0

f结了警告指,第100行,是

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); 

而是应该代码,像这样:

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

我没有签任何东西在你的程序。如果还有其他问题,我不会感到惊讶。我只是试图解决你提出的具体问题。

+0

是的,比你更有效,我可以看到你在那里使用的逻辑。不应该也添加“-1”,以便它访问数组中的正确位置? –

+0

如果您希望基于1个输入,那么,你需要减去1转换到内部基于0的索引 –

+0

至于我的约,如果选择打印的所有专辑,或只是按流派等问题,这将是过分的要求这里还是我需要等待90分钟?只是试图尽快完成它 –