I wanna extract the data from the file so I can sort it out, add and delete files. How can I do it? My file contains a list of songs with the song name, artist and song duration.
Here is what I have so far:
//Nathaly Advincula
//Song Assignment
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Song
{
private:
string title;
string artist;
double duration_sec;
public:
//Defaul constructor
Song ()
{ title= "";
artist= "";
duration_sec=99; next=0;}
//3 argument constructor
Song (string st, string sa, double min)
{ title=st;
artist=sa;
duration_sec=min*60; next=0;}
void setTitle (string st)
{ title=st; }
void setArtist(string sa)
{ artist=sa; }
void setDuration_sec ( double min)
{ duration_sec=min*60; }
string getTitle ()
{return title; }
string getArtist ()
{return artist; }
double getDuration_sec ()
{ return duration_sec; }
Song *next;
};
class SongList
{
private:
Song *head;
public:
SongList()
{ head=NULL; }
Song* getHead()
{ return head;}
//functions
void insertSong (Song *);
void deleteSong (string);
double songCount ();
void printTitle_n_Duration(double);
void printSong_by_Artist(string);
};
void SongList::insertSong(Song *ptrAddTitle)
{
Song* nodePtr, *prevPtr;
if(head == NULL || head->getTitle() >= ptrAddTitle->getTitle())
{
if(head != NULL)
ptrAddTitle->next = head;
head = ptrAddTitle;
}
else
{
prevPtr = head;
nodePtr = head->next;
//find point to insert
while(nodePtr != NULL && nodePtr->getTitle() < ptrAddTitle->getTitle())
{
prevPtr = nodePtr;
nodePtr = nodePtr->next;
}
prevPtr->next = ptrAddTitle;
ptrAddTitle->next = nodePtr;
}
}
void SongList::deleteSong(string title)
{
Song* nodePtr, *prevPtr;
//if(head == NULL) is the same thing as
if(!head)
return;
//is the first element the one 2 delete
if(head->getTitle() == title)
{
nodePtr = head;
head = head->next;
delete nodePtr;
}
else
{
nodePtr = head;
while(nodePtr != NULL && nodePtr->getTitle() != title)
{
prevPtr = nodePtr;
nodePtr = nodePtr->next;
}
//if(nodePtr != NULL) is the smae thing as
if(nodePtr)
{
prevPtr->next = nodePtr->next;
delete nodePtr;
}
}
}
//count the number of songs contained in the SongList (return the count)
double SongList::songCount ()
{
Song* nodePtr, *prevPtr;
double total=0;
//if(head == NULL) is the same thing as
if(!head)
return 0;
else
{
nodePtr = head;
while(nodePtr != NULL )
{
total++;
prevPtr = nodePtr;
nodePtr = nodePtr->next;
}
cout <<total<< endl;
}
return total;
}
//Print the title and the duration of every song that is longer than the number of minutes passed in as an argument.
void SongList::printTitle_n_Duration(double mins)
{
double secs=mins*60;
Song* nodePtr, *prevPtr;
//if(head == NULL) is the same thing as
if(!head)
return;
else
{
nodePtr = head;
while(nodePtr != NULL)
{if (nodePtr->getDuration_sec() > secs)
cout <<"Title: "<<nodePtr->getTitle()<<" Duration: "<<nodePtr->getDuration_sec()<< endl;
prevPtr = nodePtr;
nodePtr = nodePtr->next; }
}
}
//Print the title of every song in the list performed by a passed in artist as an argument
void SongList::printSong_by_Artist(string sArtist)
{
Song* nodePtr, *prevPtr;
//if(head == NULL) is the same thing as
if(!head)
return;
else
{
nodePtr = head;
while(nodePtr != NULL)
{if (nodePtr->getArtist() == sArtist)
cout <<"Title: "<<nodePtr->getTitle()<< endl;
prevPtr = nodePtr;
nodePtr = nodePtr->next; }
}
}
void main()
{
ifstream reader; //don't forget to #include <fstream>
char songName[101], songArtist[101];
int songLength;
//*** Change to the location where you downloaded
reader.open("C:\\nathaly\\songs.txt"); //open the appropriate file
while(!reader.eof())
{
reader.getline(songName, 100, ','); //reads the name of the song into songName
reader.getline(songArtist, 100, ','); //reads the artist of the song into songArtist
reader >> songLength;
reader.ignore();
cout << "Song name:" << songName << endl;
cout << "Song artist:" << songArtist << endl;
cout << "Song duration:" << songLength << endl;
}
SongList Ipod;
Song *ptr;
ptr=new Song("Photogragh", "Nickelback" , 3.45);
Ipod.insertSong (ptr);
ptr=new Song("Mr. Brightside", "The Killers" , 2.30);
Ipod.insertSong (ptr);
ptr=new Song("Kansas", "The Killers" , 4 );
Ipod.insertSong (ptr);
ptr=new Song("Adagio for Strings", "Tiesto" , 2.5);
Ipod.insertSong (ptr);
ptr=new Song("Turbulence", "Tiesto" , 2.2);
Ipod.insertSong (ptr);
ptr=new Song("Zebra", "Nickelback" , 3.45);
Ipod.insertSong (ptr);
Song* curr=NULL;
curr = Ipod.getHead();
cout<<"INSERTED SONGS"<<endl;
while(curr != NULL)
{
cout << "Song Title:" << curr->getTitle() <<
" artist " << curr->getArtist() << " duration " << curr->getDuration_sec()<< endl;
curr = curr->next;
}
string name2delete;
cout<<"WHAT SONG WOULD YOU LIKE TO DELETE?"<<endl;
cin>>name2delete;
Ipod.deleteSong(name2delete);
cout << "NOT DELETED ITEMS\n";
curr = Ipod.getHead();
while(curr != NULL)
{
cout << "TITLE: " << curr->getTitle() <<
" ARTIST " << curr->getArtist() << " DURATION " << curr->getDuration_sec() << endl;
curr = curr->next;
}
cout<<"TOTAL OF SONGS"<<endl;
Ipod.songCount ();
double durVal;
cout<<"PRINT SONGS LONGER THAN"<<endl;
cin>>durVal;
Ipod.printTitle_n_Duration(durVal);
string searchArtist;
cout<<"WHAT ARTIST WOULD YOU LIKE TO SEARCH?"<<endl;
cin>>searchArtist;
Ipod.printSong_by_Artist(searchArtist);
reader.close(); //close the file
}