Hello.
I'm having 2 difficulties...
1) I am trying to print out a string with spaces... Functions used: addSong and printSong
For ex: (This is not the whole code...I'm pointing out the functions that it is used in)
void addSong(mp3Type*& first, mp3Type*& last)
{
cout<<"Song title: ";
cin>>songTitle;
getline(cin,songTitle);
}
void printSong(mp3Type*& first)
{
cout<<current-> songTitle <<endl;
}
The output looks like:
Song title: Jerry Newman
It will only print out Newman. I'm trying to print out the whole name though.
I am able to store the string with spaces, but when I print it out, it just gives me the last part of the space... like if I type in "Billy Newman" It prints out "Newman" and thats it.
2) When I select Delete from my menu, I am trying to able to delete the node of the Song and its information stored in the node. When I try to delete, the node with all of the information is still there. What am I currently missing in the deleteSong function?
Any help is appreciated... Thanks
Here is my full program:
#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;
string songTitle;
string artist;
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<<"Song title: ";
cin>>songTitle;
getline(cin,songTitle);
cout<<"ID: ";
cin>>id;
cout<<"Artist: ";
cin>>artist;
getline(cin,artist);
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;
cout<<"Enter song ID that you like to delete"<<endl;
cout<<endl;
cin>>id;
if (first->id == id)
{
current = first;
first = first->link;
}
if (first == NULL)
{
last = NULL;
delete current;
}
else
{
found = false;
trailcurrent = first;
current = first->link;
}
}
void modifySong(mp3Type*& first, mp3Type*& last)
{
int id;
string songTitle;
string newsongTitle;
string newartist;
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<<"Enter the song's new information"<<endl;
cin>>newsongTitle;
getline(cin,newsongTitle);
cin>>newartist;
getline(cin,newartist);
cin>>newlength;
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;
current = current->link;
}
}