I've worked on it for a few days. It works great when I comment out the erase function. But it doesn't work when I include the erase function, which I use to remove an element in the playlist vector of type Songs.
Here's the else if statement. The problem is the "playlist.erase(j);". I've done it in everyway possible (ie - playlist.at(j).erase(); and many other variations)
else if(deleteChoice == 'd')
{
cin.ignore(10,'\n');
cout << "Please enter the exact song name you would like to delete: ";
getline(cin, songDelete);
for (int j = 0; j < playlist.size(); j++)
{
if(songDelete == playlist.at(j).getSongName())
{
playlist.erase(j);
cout << "The song has been deleted." << endl;
flag = false;
break;
}
}
if(flag == true)
{
cout << "Song not found. Please try again." << endl;
continue;
}
}
Here's the full function if that helps:
void Songs::addSongLibrary(vector<Songs> &library, vector<Songs> &playlist)
{
char choosePlaylist;
cin.ignore(10000,'\n');
cout << "Please enter song name: ";
getline(cin, songName);
cout << "Please enter artist name: ";
getline(cin, artistName);
cout << "Please enter album name: ";
getline(cin, albumName);
cout << "Please enter length of song in seconds: ";
cin >> playTime;
while(true)
{
if(!cin.fail() && playTime > 0)
break;
else if(cin.fail())
{
cout << "Time must be in seconds. ";
cin.clear();
cin.ignore(1000,'\n');
}
else if(playTime < 0)
{
cout << "Seconds cannot be negative. ";
cin.clear();
cin.ignore(1000,'\n');
}
cout << "Please enter length of song in seconds: ";
cin >> playTime;
}
cout << "Please enter the year the song was made: ";
cin >> year;
while(true)
{
if(!cin.fail() && year >= 1900)
break;
else if(cin.fail())
{
cout << "Year must be in numbers. ";
cin.clear();
cin.ignore(1000,'\n');
}
else if(year < 1900)
{
cout << "Year must be 1900 or greater. ";
cin.clear();
cin.ignore(1000,'\n');
}
cout << "Please enter the year the song was made: ";
cin >> year;
}
cout << "Please enter a star rating for the song (1 to 5 stars): ";
cin >> starRating;
while(true)
{
if(!cin.fail() && starRating >= 1 && starRating <= 5)
break;
else if(cin.fail())
{
cout << "Rating can only be the digits 1, 2, 3, 4, or 5. ";
cin.clear();
cin.ignore(1000,'\n');
}
else if(starRating < 1 || starRating > 5)
{
cout << "Rating must be between 1 and 5. ";
cin.clear();
cin.ignore(1000,'\n');
}
cout << "Please enter a star rating for the song (1 to 5 stars): ";
cin >> starRating;
}
cout << "Please enter a genre (Rock, Rap, Country, Gospel, or Other) for the song: ";
cin >> genre;
while(true)
{
if(genre == "Rock" || genre == "Rap" || genre == "Country" || genre == "Gospel" || genre == "Other")
break;
else
{
cout << "Genre not recognized. ";
cin.clear();
cin.ignore(1000,'\n');
}
cout << "Please enter a genre (Rock, Rap, Country, Gospel, or Other) for the song: ";
cin >> genre;
}
Songs *newTunes = new Songs();
newTunes->setSongName(songName);
newTunes->setArtistName(artistName);
newTunes->setAlbumName(albumName);
newTunes->setPlayTime(playTime);
newTunes->setYear(year);
newTunes->setStarRating(starRating);
newTunes->setSongGenre(genre);
library.push_back(*newTunes);
cout << endl << "Enter y to add song to playlist, or enter any other character to do nothing: ";
cin >> choosePlaylist;
if(choosePlaylist == 'y')
playlist.push_back(*newTunes);
}
void Songs::printReviewLibrary(vector<Songs> library)
{
cout << "Music Library" << endl;
cout << "-------------" << endl;
cout << setw(20) << left << "Song Name" << setw(20) << left << "Artist Name" << setw(20) << left << "Album Name" << setw(20) << left << "Length (Seconds)"
<< setw(10) << left << "Year" << setw(20) << left << "Star Rating" << setw(20) << left << "Genre" << endl;
for(int i = 0; i < library.size(); i++)
{
cout << setw(20) << left << library.at(i).getSongName();
cout << setw(20) << left << library.at(i).getArtistName();
cout << setw(20) << left << library.at(i).getAlbumName();
cout << setw(20) << left << library.at(i).getPlayTime();
cout << setw(10) << left << library.at(i).getYear();
cout << setw(20) << left << library.at(i).getStarRating();
cout << setw(20) << left << library.at(i).getSongGenre() << endl;
}
}
void Songs::printReviewPlaylist(vector<Songs> &playlist)
{
char deleteChoice;
bool flag = true;
string songDelete;
cout << "Music Playlist" << endl;
cout << "--------------" << endl;
cout << setw(20) << left << "Song Name" << setw(20) << left << "Artist Name" << setw(20) << left << "Album Name" << setw(20) << left << "Length (Seconds)"
<< setw(10) << left << "Year" << setw(20) << left << "Star Rating" << setw(20) << left << "Genre" << endl;
for(int i = 0; i < playlist.size(); i++)
{
cout << setw(20) << left << playlist.at(i).getSongName();
cout << setw(20) << left << playlist.at(i).getArtistName();
cout << setw(20) << left << playlist.at(i).getAlbumName();
cout << setw(20) << left << playlist.at(i).getPlayTime();
cout << setw(10) << left << playlist.at(i).getYear();
cout << setw(20) << left << playlist.at(i).getStarRating();
cout << setw(20) << left << playlist.at(i).getSongGenre() << endl;
}
while(flag)
{
cout << endl << "Enter d to search for and delete songs from playlist. Enter any other key to revert to main menu: ";
cin >> deleteChoice;
if (cin.fail())
{
cout << "You did not enter a character. Please try again." << endl;
cin.clear();
}
else if(deleteChoice != 'd')
break;
else if(deleteChoice == 'd')
{
cin.ignore(10,'\n');
cout << "Please enter the exact song name you would like to delete: ";
getline(cin, songDelete);
for (int j = 0; j < playlist.size(); j++)
{
if(songDelete == playlist.at(j).getSongName())
{
playlist.erase(j);
cout << "The song has been deleted." << endl;
flag = false;
break;
}
}
if(flag == true)
{
cout << "Song not found. Please try again." << endl;
continue;
}
}
}
}
This is the last bug in the program. Thanks so much.