Hello, I'm having a simple problem.
For my program, each song has an id number which stores the songs information. For my deleteSong function, I am asking the user what id of the song does he/she wants to be deleted. After the user enters the id of the song he/she wants to delete, I then print out the list of songs. After I print the list of songs, the problem is the song that is chosen to be deleted remains on the list, while all other songs that were on the list is now deleted. Basically, I just want the song that user chooses to delete, but instead of it being deleted, the others are deleted. How do I solve this problem in my code?
#include <iostream>
#include <string>
const int SONGS = 2;
using namespace std;
struct mp3Type
{
int id;
string songTitle;
string artist;
string length;
string size;
mp3Type *link;
};
void addSong(mp3Type*& first, mp3Type*& last);
void deleteSong(mp3Type*& first);
void modifySong(mp3Type*& first, mp3Type*& last);
void printSong(mp3Type*& first);
int main()
{
mp3Type *first, *last;
char selection;
do
{
cout<<"a - Add a song "<<endl;
cout<<"d - Delete a song"<<endl;
cout<<"m - Modify a song's information "<<endl;
cout<<"p - Print a list of songs "<<endl;
cout<<"q - Quit "<<endl;
cin>>selection;
switch(selection)
{
case 'a': addSong(first,last);
break;
case 'd': deleteSong(first);
break;
case 'm': modifySong(first,last);
break;
case 'p': printSong(first);
break;
case 'q':
break;
default: cout<<"Invalid option "<<endl;
}
}
while (selection != 'q');
system("PAUSE");
return 0;
}
void addSong(mp3Type*& first, mp3Type*& last)
{
int id;
char songTitle[100];
char artist[100];
string length;
string size;
mp3Type *newNode;
char selection;
first = NULL;
last = NULL;
for (int i = 0; i < SONGS; i++)
{
cout<<endl;
cout<<"Adding song # "<<i+1<<endl;
cout<<endl;
cout<<"\nSong title: ";
std::cin.getline(songTitle, 100);
std::cin.getline(songTitle, 100);
cout<<"ID: ";
cin>>id;
cout<<"Artist: ";
std::cin.getline(artist, 100);
std::cin.getline(artist, 100);;
cout<<"Length: ";
cin>>length;
cout<<"Size: ";
cin>>size;
newNode = new mp3Type; // create new node
newNode->songTitle = songTitle;
newNode->id = id;
newNode->artist = artist;
newNode->length = length;
newNode->size = size;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
}
}
void deleteSong(mp3Type*& first)
{
mp3Type *last,*current,*trailcurrent;
bool found;
int id;
current = first;
cout<<"Enter song ID that you like to delete"<<endl;
cout<<endl;
cin>>id;
for (int i = 0; i < SONGS; i++)
{
if (first->link != NULL)
{
current = first->link;
first->link = first->link->link;
delete current;
}
else
{
found = false;
trailcurrent = first;
}
current = first->link;
}
}
void modifySong(mp3Type*& first, mp3Type*& last)
{
int id;
char newsongTitle[100];
char newartist[100];
string newlength;
string newsize;
bool found = false;
mp3Type *current;
printSong(first);
current = first;
cout<<"Enter the id of the song to modify information"<<endl;
cin>>id;
while (current != NULL)
{
if (current->id == id)
{
cout<<endl;
cout<<"Song title: ";
std::cin.getline(newsongTitle, 100);
std::cin.getline(newsongTitle, 100);
cout<<"Artist: ";
std::cin.getline(newartist, 100);
cout<<"Length: ";
cin>>newlength;
cout<<"Size: ";
cin>>newsize;
current->songTitle = newsongTitle;
current->artist = newartist;
current->length = newlength;
current->size = newsize;
}
current = current->link;
}
}
void printSong(mp3Type*& first)
{
cout<<"Printing song list..."<<endl;
mp3Type *current;
current = new mp3Type;
current = first;
while (current != NULL)
{
cout<<endl;
cout<<current-> songTitle <<endl;
cout<<current-> id <<endl;
cout<<current-> artist <<endl;
cout<<current-> length <<endl;
cout<<current-> size <<endl;
cout<<endl;
current = current->link;
}
}