1
votes

So i'm working on this pascal application which has a menu where you can do multiple things.

After entering an album (which is what my program does) and trying to edit it by writing over the current album I get an error as shown in the image.

enter image description here

There have been no errors when compiling except the warning:

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

Here is my code:

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
One question at a time here. I edited it to be so. - David Heffernan

1 Answers

0
votes

The function that the warning refers to, on line 100, is

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

The warning says:

Warning: Function result variable does not seem to initialized

And indeed the result variable has not been initialized.

The design of the function is wrong though. You are trying to modify an existing element in an array. You should not be returning a new array. The function is not necessary though. You should simply remove it. Then you need to look at the one place where you call the function.

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

You should instead code that like this:

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

I've not checked anything else in your program. I would not be surprised if there are other problems. I've just tried to address the specific question that you asked.