Joshua Nguyen
Joshua Nguyen

Reputation: 153

Pascal: Will not read anything from my file and getting errors

I've been stressing out for the past few hours trying to get this working and I simply cannot figure it out. Basically it doesn't read anything from my .dat file and when I try to use options 2, 3 or 4 it just comes up with these

errors.

I've include the .dat file and pascal below. I'm desperate for help.

mytestfile.dat

1
Adele
Pop
2
Hello
ff
Remedy
dd

pascal

program MusicPlayer;
uses TerminalUserInput;

type
    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; 
        tracks: array of TrackRec;
    end;
type AlbumArray = array of AlbumRec; 


function ReadGenre(): 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('');

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

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

procedure NewAlbum(var albums: AlbumArray; var myFile: TextFile);
var
    number, i, tracks, y: Integer;
begin
    AssignFile(myFile, 'mytestfile.dat');
    ReWrite(myFile);

    number := ReadInteger('How many albums do you want to make?: ');
    WriteLn(myFile, number);
    SetLength(albums, number);

    for i := Low(albums) to High(albums) do
    begin
        albums[i].name := ReadString('Enter album name:');
        WriteLn(myFile, albums[i].name);

        albums[i].genre := ReadGenre();
        WriteLn(myFile, albums[i].genre);

        tracks := ReadIntegerRange('How many tracks do you want to enter? (max 15)', 0, 15);
        WriteLn(myFile, tracks);
        SetLength(albums[i].tracks, tracks);

        for y := Low(albums[i].tracks) to tracks - 1 do 
        begin
            albums[i].tracks[i].name := ReadString('Track name:');
            WriteLn(myFile, albums[i].tracks[i].name);
            albums[i].tracks[i].location := ReadString('Track Location:');
            WriteLn(myFile, albums[i].tracks[i].location);
        end;
    end;
    Close(myFile);
end;

procedure ReadTrack(count: Integer; var albums: AlbumArray; var myFile: TextFile);
var
    i: Integer;
begin
    ReadLn(myFile, i);
    SetLength(albums[count].tracks, i);
    for count := Low(albums[count].tracks) to High(albums[count].tracks) - 1 do
    begin
        ReadLn(myFile, albums[count].tracks[i].name);
        ReadLn(myFile, albums[count].tracks[i].location);
    end;
end;

procedure ReadAlbum(var albums: AlbumArray; var myFile: TextFile);
var
    albumNumber, tracknumber, count, i: Integer;

begin
    AssignFile(myFile, 'mytestfile.dat');
    Reset(myFile);
    ReadLn(myFile, albumNumber);
    SetLength(albums, albumNumber); 

    for i := Low(albums) to High(albums) do
    begin
        ReadLn(myFile, albums[i].name);
        ReadLn(myFile, albums[i].genre);
        ReadLn(myFile, tracknumber);
        SetLength(albums[i].tracks, tracknumber);
        for count := Low(albums[count].tracks) to tracknumber - 1 do
        begin
            ReadLn(myFile, albums[count].tracks[i].name);
            ReadLn(myFile, albums[count].tracks[i].location);
        end;
    end;
end;

procedure ReadAlbums(var albums: AlbumArray; var myFile: TextFile);
var 
    i, number: Integer;
begin
    ReadAlbum(albums, myFile);
    WriteLn('Album is:');
    for i := Low(albums) to High(albums) do
    begin
        WriteLn((i + 1),'.', albums[i].name);
        WriteLn('.', albums[i].genre);
        for number := Low(albums[i].tracks) to High(albums[i].tracks) do
        begin
            WriteLn((number + 1), '', albums[i].tracks[Low(albums[i].tracks)].name);
        end;
    end;
end;

procedure PlayAlbum (var albums: AlbumArray; var myFile: TextFile);
var
    i, number: Integer;
begin

    ReadAlbums(albums, myFile);

    i := ReadInteger('Please select an album: ');
    i := i - 1;
    number := ReadIntegerRange('Please select a song from album:', 1, 20);
    number := number - 1;

    WriteLn('Now playing');
    WriteLn('Track selected: ', albums[i].tracks[number].name);
    WriteLn('Album: ', albums[i].name);

end;

procedure Update(var albums: AlbumArray; var myFile: TextFile);
var
    i: Integer;
begin
    ReadAlbums(albums, myFile);
    i := ReadInteger('Select an album to update');
    i := i - 1;
    albums[i].name := ReadString('New Album Name:');
    WriteLn('Album has now been updated');
end;

procedure Main();
var
    i, count, select, change: Integer;
    albums: AlbumArray;
    myFile: TextFile;
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('Select option for menu:');
        case i of  
            1: ReadAlbum(albums, myFile);

            2: ReadAlbums(albums, myFile);

            3: PlayAlbum(albums, myFile);

            4: Update(albums, myFile);
        end;
    until i = 5;
end;

begin
    Main();
end.

Upvotes: 0

Views: 374

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

In the ReadAlbum() procedure you have two for loops, an outer one that loops through the albums and an inner one that loops through the tracks. In the latter you have messed up the indexes:

This is the erroneous code:

    for count := Low(albums[count].tracks) to tracknumber - 1 do
    begin
        ReadLn(myFile, albums[count].tracks[i].name);
        ReadLn(myFile, albums[count].tracks[i].location);
    end;

albums[] should use i as index and tracks[] should use count as index.

Until you get more used to arrays and indexes I suggest you name your variables with a clear meaning, for example:

`NumOfAlbums` in the meaning of how many albums you have
`AlbumIndex` as the index to the albums array
`NumOfTracks` as the number of tracks in an album
`TrackIndex` as the index to the tracks array

It may feel tiresome to write so long names, but you would probably not have made the mistake you did, with the more verbose names.

Upvotes: 1

Related Questions