So I'm trying to make a Pascal text program that allows the user to enter the name, genre, artist etc. of an album, and also view, play, update the album through 4 menu options. But im having issues as when I try to enter the first menu (the menu to add a new album), it just re prints the main menu. I'm using Sublime text editor and Mingw as a terminal. Here is the code, I believe the issue involves functions GetAlbum and GetAlbums.
Thanks :)
program TextMusicPlayer;
uses TerminalUserInput;
type
Tracks = record
name: String;
end;
Album = record
name: String;
artist: String;
genre: String;
track: array of String;
key: Integer;
trackcount: integer;
end;
playlist = array of Album;
function GetAlbum( var myFile: TextFile): Album;
var
albumInfo: Album;
i: Integer;
begin
albumInfo.name := ReadString('Enter name: ');
albumInfo.artist := ReadString('Enter artist: ');
albumInfo.genre := ReadString('Enter genre: ');
albumInfo.trackcount := ReadInteger('Enter track no.: ');
SetLength(albumInfo.track, albumInfo.trackcount);
for i := 0 to High(albumInfo.track) do
begin
ReadLn(myFile, albumInfo.track[i]);
end;
albumInfo.key := -1;
result := albumInfo;
Close(myFile);
end;
function GetAlbums(): playlist;
var
myFile: TextFile;
playlist: array of Album;
i, r: integer;
begin
AssignFile(myFile, 'music.txt');
Reset(myFile);
ReadLn(myFile, r);
SetLength(playlist, r);
for i := 0 to High(playlist) do
begin
playlist[i] := GetAlbum(myFile);
end;
result := playlist;
end;
function PrintAllAlbums(playlist: playlist): playlist;
var
i: integer;
begin
for i := 0 to High(playlist) do
begin
playlist[i].key := i;
WriteLn(playlist[i].name, ' key number: ', i);
end;
result := playlist;
end;
function PrintAllGenres(playlist: Playlist): Playlist;
var
genre: String;
i: Integer;
r: Integer;
m: Integer;
begin
m := 0;
for i := 0 to High(playlist) do
begin
playlist[i].key := -1;
end;
genre := ReadString('Enter the Genre: ');
for r := 0 to High(playlist) do
begin
if (genre = playlist[r].genre) then
begin
playlist[i].key := m;
WriteLn(playlist[r].name, ' key number: ', m);
m := m + 1;
end;
end;
result := playlist;
end;
function PrintAlbums(playlist: playlist): playlist;
var
val: integer;
begin
WriteLn('***Displaying Albums***');
WriteLn('1 - Display all albums');
WriteLn('2 - Display all of one genre');
WriteLn('3 - Back to Main Menu');
val := ReadInteger('Enter a number to enter a Menu: ');
case val of
1: playlist := PrintAllAlbums(playlist);
2: playlist := PrintAllGenres(playlist);
end;
result := playlist;
end;
procedure PlayAlbum(albumInfo: Album);
var
i: Integer;
r: Integer;
begin
WriteLn('Album:', albumInfo.name);
for i := 0 to High(albumInfo.track) do
begin
WriteLn(i + 1, albumInfo.track[i]);
end;
r := ReadInteger('Select a track number to play: ');
while (r < 1) or (r > (High(albumInfo.track) + 1)) do
begin
r := ReadInteger('Please select a track number from above: ');
end;
WriteLn('Playing track ', r, albumInfo.track[(r - 1)], ' from the album ', albumInfo.name);
ReadString('Press Enter to return to Main Menu ');
end;
procedure SelectAlbum(playlist: Playlist);
var
val: Integer;
i: Integer;
r: Integer;
begin
r := 0;
WriteLn('***Welcome to the Track Player***');
val := ReadInteger('Enter an Albums key number');
for i := 0 to High(playlist) do
begin
if (playlist[i].key = val) then
begin
PlayAlbum(playlist[i]);
end
else
begin
r := r + 1;
end;
end;
if r > High(playlist) then
begin
WriteLn('Album was not found, now returning to Main Menu ');
end;
end;
function ChangeAlbum(albumInfo: Album): Album;
begin
albumInfo.name := ReadString('Please enter a new name for this album');
albumInfo.genre := ReadString('Please enter a new gernre for this album');
result := albumInfo;
end;
function ChangeAlbums(playlist: Playlist): Playlist;
var
val: Integer;
i: Integer;
j: Integer;
begin
j := 0;
WriteLn('***Album Updater***');
val := ReadInteger('Enter an Albums key number: ');
for i := 0 to High(playlist) do
begin
if (playlist[i].key = val) then
begin
playlist[i] := ChangeAlbum(playlist[i]);
end
else
begin
j := j + 1;
end;
end;
if j > High(playlist) then
begin
WriteLn('Album was not found, now returning to Main Menu ');
end;
result := playlist;
end;
procedure Main();
var
i, val: Integer;
playlist: array of Album;
begin
i := 0;
while (i = 0) do
begin
WriteLn('');
WriteLn('***Text Music Player Menu***');
WriteLn('1 - Add new Albums');
WriteLn('2 - View Albums');
WriteLn('3 - Play an Album');
WriteLn('4 - Update an Album');
WriteLn('5 - Quit');
val := ReadInteger('Enter a number to enter a Menu: ');
case val of
1: playlist := GetAlbums();
2: playlist := PrintAlbums(playlist);
3: SelectAlbum(playlist);
4: playlist := ChangeAlbums(playlist);
else
i := i + 1;
end;
end;
end;
begin
Main();
end.
TerminalUserInput, so I can't try your code. - Rudy VelthuisGetAlbums(plural), you are opening'music.txt'to read from it. But you only read one value, the size of the playlist. Then you useGetAlbum(singular) to get that many albums from the user. That is not how it should be. You should get an album from the user and then append it to yourmusic.txtfile, somehow. In other words: there is an error in your logic. - Rudy Velthuis