Can someone please edit my code so that it satisfies the assignments' requirements?
I pasted the codes below.
Requirement:
Goals for This Project:
Using class to model Abstract Data Type
OOP-Data Encapsulation
You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should save the data back to the same data file when the program exits.
Write an interactive text based menu interface (using a loop) that will allow the user to
Enter information for a new song
Display information for all the songs in the database with index for each song
Remove a song by index
Search for songs by a certain artist
Search for songs by a certain album
Quit
For each song, you need to keep track of:
title
artist
duration
album
Allow the program to keep looping until user wants to quit. When the program starts, it should load the tasks from external file ("songs.txt") into memory. When user enters information about the new song, the program needs to read them in, save them in memory and eventually write them to the external data file ("songs.txt"). The file format could look like:
Stereo Hearts;Gym Class Heroes;3;34;The Papercut Chronicles II Counting Stars;OneRepulic;4;17;Native
The ';' is used as a delimiter or field separator. Each record ends with a new line character.
Some Implementation Requirements:
Use structs or class named Song to model song. You are encouraged in every way to use class to model song in this project. If you do the work here, you will benefit from this in Project 4 and 5.
Use class named SongList to model the collection of songs.
Use array of Song to implement class SongList
When using class, please make sure you encapsulate the data which means make all the
instance data member private and provide accessor methods and mutator methods to
access and manipulate the data.
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
//Declare variables
const int MAX_CHAR = 100;
const int Title_W = 40;
const int Artist_W = 40;
const int Duration_W = 40;
const int Album_W = 40;
//Create a type of musicLibrary
struct musicLibrary
{
char title[MAX_CHAR];
char artist[MAX_CHAR];
char duration[MAX_CHAR];
char album[MAX_CHAR];
};
//Declare functions
void displyOpt();
char option();
void readinSearch(char search[]);
void readInput (const char prompt[], char inputStr[], int maxChar);
void executeOption(char opt, musicLibrary list[], int& listSize);
void allSongs(const musicLibrary list[], int listSize);
void readInEntry(musicLibrary& anEntry);
void addEntry(const musicLibrary& anEntry, musicLibrary list[], int& listSize);
bool artistSearch(const char search[], const musicLibrary list[], int listSize);
bool albumSearch(const char search[], const musicLibrary list[], int listSize);
void loadFile(const char fileName[], musicLibrary list[], int& listSize);
void saveFile(const char fileName[], const musicLibrary list[], int listSize);
void rmSong(musicLibrary list[], int& listSize);
//Main function control the all program flows
int main()
{
int listSize;
char opt;
musicLibrary list[MAX_CHAR];
char fileName[] = "songs.txt";
loadFile(fileName, list, listSize);
displyOpt();
opt = option();
while (opt != 'f')
{
executeOption(opt, list, listSize);
displyOpt();
opt = option();
}
saveFile(fileName, list, listSize);
return 0;
}
//Display the menu
void displyOpt()
{
cout << "\n\nWelcome to use the music library manager!\n\n"
<< "a) View all your songs.\n"
<< "b) Add a new song.\n"
<< "c) Search for songs by a certain artist.\n"
<< "d) Search for songs by a certain album.\n"
<< "e) Delete a song by index number.\n"
<< "f) Exit\n"
<< "Please choose what you want to do: ";
}
//Read in the option
char option()
{
char opt;
cin >> opt;
cin.ignore(100, '\n');
return tolower(opt);
}
//Switch to different case when user make different options.
void executeOption(char opt, musicLibrary list[], int& listSize)
{
musicLibrary entry;
char search[MAX_CHAR];
switch (opt)
{
case 'a': allSongs(list, listSize);
break;
case 'b': readInEntry(entry);
addEntry(entry, list, listSize);
allSongs(list, listSize);
break;
case 'c': readinSearch(search);
artistSearch(search, list, listSize);
break;
case 'd': readinSearch(search);
albumSearch(search, list, listSize);
break;
case 'e': rmSong(list, listSize);
break;
case 'f': //saveFile(fileName, list, listSize);
cout << "Bye!" << endl;
break;
default: cout << endl << "Illegal value!" << endl;
break;
}
}
//Display all the songs' information in different columns.
void allSongs(const musicLibrary list[], int listSize)
{
int index;
cout << setw(Title_W) << "Title"
<< setw(Artist_W) << "Artist"
<< setw(Duration_W) << "Duration"
<< setw(Album_W) << "Album"
<< endl;
for(index=0; index<listSize; index++)
{
cout << index << setw(Title_W) << list[index].title
<< setw(Artist_W) << list[index].artist
<< setw(Duration_W) << list[index].duration
<< setw(Album_W) << list[index].album
<< endl;
}
cout << "Number of record: " << listSize << endl;
}
void readInput (const char prompt[], char inputStr[], int maxChar)
{
cout << endl << prompt;
//read until reach the limit or a new line
cin.get(inputStr, maxChar, '\n');
while(!cin)
{
cin.clear ();
cin.ignore (100, '\n');
cout << endl << prompt;
cin.get(inputStr, maxChar, '\n');
}
//disgard the '\n'
cin.ignore (100, '\n');
}
//Read the key word that user want to search in the music library/
void readinSearch(char search[])
{
readInput("Please enter the name of the artist/album: ", search, MAX_CHAR);
}
//Use the key word user provided search the artist column
bool artistSearch(const char search[], const musicLibrary list[], int listSize)
{
int index;
bool found = false;
// cout << "Number of record: " << listSize << endl;
cout << "Here are the matched songs: " << endl;
for(index=0; index<listSize; index++)
{
//cout << index << endl;
if(strcmp(search, list[index].artist) == 0)
{
// cout << list[index] << endl;
cout << index << setw(Title_W) << list[index].title
<< setw(Artist_W) << list[index].artist
<< setw(Duration_W) << list[index].duration
<< setw(Album_W) << list[index].album
<< endl;
found = true;
}
}
//Display Error messaage when nothing is found.
if ( found == false)
{
cout << "No matches found." << endl;
}
return found;
}
//Search the key word in album column.
bool albumSearch(const char search[], const musicLibrary list[], int listSize)
{
int index;
bool found = false;
// cout << "Number of record: " << listSize << endl;
cout << "Here are the matched songs: " << endl;
for(index=0; index<listSize; index++)
{
//cout << index << endl;
if(strcmp(search, list[index].album) == 0)
{
// cout << list[index] << endl;
cout << index << setw(Title_W) << list[index].title
<< setw(Artist_W) << list[index].artist
<< setw(Duration_W) << list[index].duration
<< setw(Album_W) << list[index].album
<< endl;
found = true;
}
}
if ( found == false)
{
cout << "No matches found." << endl;
}
return found;
}
//When user want to add a new song, display the information
//and ask for input for each column.
void readInEntry(musicLibrary& anEntry)
{
char title[MAX_CHAR];
char artist[MAX_CHAR];
char duration[MAX_CHAR];
char album[MAX_CHAR];
//read in name and email
readInput("Please enter the title: ", title, MAX_CHAR);
readInput("Please enter the artist: ", artist, MAX_CHAR);
readInput("Please enter the length of the song: ", duration, MAX_CHAR);
readInput("Please enter the name of the album: ", album, MAX_CHAR);
//populate the passed in object
strcpy(anEntry.title, title);
strcpy(anEntry.artist, artist);
strcpy(anEntry.duration, duration);
strcpy(anEntry.album, album);
}
//Add the information user entered into each columns.
void addEntry(const musicLibrary& anEntry, musicLibrary list[], int& listSize)
{
strcpy(list[listSize].title, anEntry.title);
strcpy(list[listSize].artist, anEntry.artist);
strcpy(list[listSize].duration, anEntry.duration);
strcpy(list[listSize].album, anEntry.album);
listSize++;
}
//Load the text file that contains the music information.
void loadFile(const char fileName[], musicLibrary list[], int& listSize)
{
ifstream in;
char title[MAX_CHAR];
char artist[MAX_CHAR];
char duration[MAX_CHAR];
char album[MAX_CHAR];
musicLibrary anEntry;
in.open (fileName);
if(!in)
{
in.clear();
cerr << endl << "Fail to open " << fileName << " for input!" << endl << endl;
exit(1);
}
in.get(title, MAX_CHAR, ';');
while (!in.eof())
{
//remove field seperator ';' and read next data.
in.get();
in.get(artist, MAX_CHAR, ';');
in.get();
in.get(duration, MAX_CHAR, ';');
in.get();
in.get(album, MAX_CHAR, ';');
in.get();
in.ignore(100, '\n');
strcpy(anEntry.title, title);
strcpy(anEntry.artist, artist);
strcpy(anEntry.duration, duration);
strcpy(anEntry.album, album);
addEntry(anEntry, list, listSize);
in.get(title, MAX_CHAR, ';'); //start the next record
}
in.close();
}
//Save changes to the file
void saveFile(const char fileName[], const musicLibrary list[], int listSize)
{
ofstream out;
int index;
out.open (fileName);
if(!out)
{
out.clear();
cerr << endl << "Fail to open " << fileName << " for output!" << endl << endl;
exit(1);
}
for(index=0; index<listSize; index++)
{
out << list[index].title << ';'
<< list[index].artist << ';'
<< list[index].duration << ';'
<< list[index].album << ';'
<< endl;
}
out.close();
}
//Remove song by using the index number that user provided. To make this work,
//I choose to do it in two cases:
// 1. user choose last one:
// delete the last line
// 2. user choose other lines:
// copy the last line to the chosen line then delete the last line.
//Then show the updated the list
void rmSong(musicLibrary list[], int& listSize)
{
int index;
cout << "Enter the index number of the song to delete: " << endl;
cin >> index;
if (index < 0 || index >= listSize)
{
cout << "Illegal value." << endl;
return;
}
// remove last item, it's.....
if (index == listSize - 1)
{
listSize = listSize - 1;
}
else
{
strcpy(list[index].title, list[listSize - 1].title);
strcpy(list[index].artist, list[listSize - 1].artist);
strcpy(list[index].duration, list[listSize - 1].duration);
strcpy(list[index].album, list[listSize - 1].album);
listSize = listSize - 1;
}
cout << "Unpdated the reordered list." << endl;
allSongs(list, listSize);
}
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
//Declare variables
const int MAX_CHAR = 100;
const int Title_W = 40;
const int Artist_W = 40;
const int Duration_W = 40;
const int Album_W = 40;
//Create a type of musicLibrary
struct musicLibrary
{
char title[MAX_CHAR];
char artist[MAX_CHAR];
char duration[MAX_CHAR];
char album[MAX_CHAR];
};
//Declare functions
void displyOpt();
char option();
void readinSearch(char search[]);
void readInput (const char prompt[], char inputStr[], int
maxChar);
void executeOption(char opt, musicLibrary list[], int&
listSize);
void allSongs(const musicLibrary list[], int listSize);
void readInEntry(musicLibrary& anEntry);
void addEntry(const musicLibrary& anEntry, musicLibrary list[],
int& listSize);
bool artistSearch(const char search[], const musicLibrary list[],
int listSize);
bool albumSearch(const char search[], const musicLibrary list[],
int listSize);
void loadFile(const char fileName[], musicLibrary list[], int&
listSize);
void saveFile(const char fileName[], const musicLibrary list[], int
listSize);
void rmSong(musicLibrary list[], int& listSize);
//Main function control the all program flows
int main()
{
int listSize;
char opt;
musicLibrary list[MAX_CHAR];
char fileName[] = "songs.txt";
loadFile(fileName, list, listSize);
displyOpt();
opt = option();
while (opt != 'f')
{
executeOption(opt, list, listSize);
displyOpt();
opt = option();
}
saveFile(fileName, list, listSize);
return 0;
}
//Display the menu
void displyOpt()
{
cout << "\n\nWelcome to use the music library
manager!\n\n"
<< "a) View all your songs.\n"
<< "b) Add a new song.\n"
<< "c) Search for songs by a certain artist.\n"
<< "d) Search for songs by a certain album.\n"
<< "e) Delete a song by index number.\n"
<< "f) Exit\n"
<< "Please choose what you want to do: ";
}
//Read in the option
char option()
{
char opt;
cin >> opt;
cin.ignore(100, '\n');
return tolower(opt);
}
//Switch to different case when user make different
options.
void executeOption(char opt, musicLibrary list[], int&
listSize)
{
musicLibrary entry;
char search[MAX_CHAR];
switch (opt)
{
case 'a': allSongs(list, listSize);
break;
case 'b': readInEntry(entry);
addEntry(entry, list, listSize);
allSongs(list, listSize);
break;
case 'c': readinSearch(search);
artistSearch(search, list, listSize);
break;
case 'd': readinSearch(search);
albumSearch(search, list, listSize);
break;
case 'e': rmSong(list, listSize);
break;
case 'f': //saveFile(fileName, list, listSize);
cout << "Bye!" << endl;
break;
default: cout << endl << "Illegal value!" <<
endl;
break;
}
}
//Display all the songs' information in different columns.
void allSongs(const musicLibrary list[], int listSize)
{
int index;
cout << setw(Title_W) << "Title"
<< setw(Artist_W) << "Artist"
<< setw(Duration_W) << "Duration"
<< setw(Album_W) << "Album"
<< endl;
for(index=0; index<listSize; index++)
{
cout << index << setw(Title_W) <<
list[index].title
<< setw(Artist_W) << list[index].artist
<< setw(Duration_W) << list[index].duration
<< setw(Album_W) << list[index].album
<< endl;
}
cout << "Number of record: " << listSize <<
endl;
}
void readInput (const char prompt[], char inputStr[], int
maxChar)
{
cout << endl << prompt;
//read until reach the limit or a new line
cin.get(inputStr, maxChar, '\n');
while(!cin)
{
cin.clear ();
cin.ignore (100, '\n');
cout << endl << prompt;
cin.get(inputStr, maxChar, '\n');
}
//disgard the '\n'
cin.ignore (100, '\n');
}
//Read the key word that user want to search in the music
library/
void readinSearch(char search[])
{
readInput("Please enter the name of the artist/album: ", search,
MAX_CHAR);
}
//Use the key word user provided search the artist column
bool artistSearch(const char search[], const musicLibrary list[],
int listSize)
{
int index;
bool found = false;
// cout << "Number of record: " << listSize <<
endl;
cout << "Here are the matched songs: " << endl;
for(index=0; index<listSize; index++)
{
//cout << index << endl;
if(strcmp(search, list[index].artist) == 0)
{
cout << index << setw(Title_W) <<
list[index].title
<< setw(Artist_W) << list[index].artist
<< setw(Duration_W) << list[index].duration
<< setw(Album_W) << list[index].album
<< endl;
found = true;
}
}
//Display Error messaage when nothing is found.
if ( found == false)
{
cout << "No matches found." << endl;
}
return found;
}
//Search the key word in album column.
bool albumSearch(const char search[], const musicLibrary list[],
int listSize)
{
int index;
bool found = false;
// cout << "Number of record: " << listSize <<
endl;
cout << "Here are the matched songs: " << endl;
for(index=0; index<listSize; index++)
{
//cout << index << endl;
if(strcmp(search, list[index].album) == 0)
{
// cout << list[index] << endl;
cout << index << setw(Title_W) <<
list[index].title
<< setw(Artist_W) << list[index].artist
<< setw(Duration_W) << list[index].duration
<< setw(Album_W) << list[index].album
<< endl;
found = true;
}
}
if ( found == false)
{
cout << "No matches found." << endl;
}
return found;
}
//When user want to add a new song, display the
information
//and ask for input for each column.
void readInEntry(musicLibrary& anEntry)
{
char title[MAX_CHAR];
char artist[MAX_CHAR];
char duration[MAX_CHAR];
char album[MAX_CHAR];
//read in name and email
readInput("Please enter the title: ", title, MAX_CHAR);
readInput("Please enter the artist: ", artist, MAX_CHAR);
readInput("Please enter the length of the song: ", duration,
MAX_CHAR);
readInput("Please enter the name of the album: ", album,
MAX_CHAR);
//populate the passed in object
strcpy(anEntry.title, title);
strcpy(anEntry.artist, artist);
strcpy(anEntry.duration, duration);
strcpy(anEntry.album, album);
}
//Add the information user entered into each columns.
void addEntry(const musicLibrary& anEntry, musicLibrary list[],
int& listSize)
{
strcpy(list[listSize].title, anEntry.title);
strcpy(list[listSize].artist, anEntry.artist);
strcpy(list[listSize].duration, anEntry.duration);
strcpy(list[listSize].album, anEntry.album);
listSize++;
}
//Load the text file that contains the music information.
void loadFile(const char fileName[], musicLibrary list[], int&
listSize)
{
ifstream in;
char title[MAX_CHAR];
char artist[MAX_CHAR];
char duration[MAX_CHAR];
char album[MAX_CHAR];
musicLibrary anEntry;
in.open (fileName);
if(!in)
{
in.clear();
cerr << endl << "Fail to open " << fileName
<< " for input!" << endl << endl;
exit(1);
}
in.get(title, MAX_CHAR, ';');
while (!in.eof())
{
//remove field seperator ';' and read next data.
in.get();
in.get(artist, MAX_CHAR, ';');
in.get();
in.get(duration, MAX_CHAR, ';');
in.get();
in.get(album, MAX_CHAR, ';');
in.get();
in.ignore(100, '\n');
strcpy(anEntry.title, title);
strcpy(anEntry.artist, artist);
strcpy(anEntry.duration, duration);
strcpy(anEntry.album, album);
addEntry(anEntry, list, listSize);
in.get(title, MAX_CHAR, ';'); //start the next record
}
in.close();
}
//Save changes to the file
void saveFile(const char fileName[], const musicLibrary list[], int
listSize)
{
ofstream out;
int index;
out.open (fileName);
if(!out)
{
out.clear();
cerr << endl << "Fail to open " << fileName
<< " for output!" << endl << endl;
exit(1);
}
for(index=0; index<listSize; index++)
{
out << list[index].title << ';'
<< list[index].artist << ';'
<< list[index].duration << ';'
<< list[index].album << ';'
<< endl;
}
out.close();
}
//Remove song by using the index number that user provided. To
make this work,
//I choose to do it in two cases:
// 1. user choose last one:
// delete the last line
// 2. user choose other lines:
// copy the last line to the chosen line then delete the last
line.
//Then show the updated the list
void rmSong(musicLibrary list[], int& listSize)
{
int index;
cout << "Enter the index number of the song to delete: "
<< endl;
cin >> index;
if (index < 0 || index >= listSize)
{
cout << "Illegal value." << endl;
return;
}
// remove last item, it's.....
if (index == listSize - 1)
{
listSize = listSize - 1;
}
else
{
strcpy(list[index].title, list[listSize - 1].title);
strcpy(list[index].artist, list[listSize - 1].artist);
strcpy(list[index].duration, list[listSize - 1].duration);
strcpy(list[index].album, list[listSize - 1].album);
listSize = listSize - 1;
}
cout << "Unpdated the reordered list." << endl;
allSongs(list, listSize);
}
Get Answers For Free
Most questions answered within 1 hours.