Im trying to do my C++ exercise about the Album Project. Basically, i need to have 4 classes, namely: Duration, Track (duration object + title of track), Album (Name of artist, title of album and a collection of my Track object) and AlbumCollection (only a collection of Album objects ) is variable.
I did test all the class by assigning the value or given value from the key board and they all work. However, when i try to ask them to read in the an txt file. It just keep looping in the album collection.app and never stop. I know that my problem is in the >> operator and whether i did something wrong with the failbit. However, i really dont know what should i do to fix this.
This is my >> operator in Duration object
inline std::istream& operator >> (std::istream& is, Duration& f)
{
char c;
int h,m,s;
if (is >> h >> c >> m >> c >> s){
if (c==':'){
f = Duration (h,m,s);
}
else{
is.clear(std::ios_base::failbit);
}
} else{
is.clear(std::ios_base::failbit);
}
return is;
}
This is my >> operator in Track object
istream& operator>>(istream& is, Track& t){
Duration duration;
char trackTitle[256];
char c1;
if (is>>duration>>c1){
is.getline(trackTitle,256);
t = Track(duration,trackTitle);
}
else{
is.clear(ios_base::failbit);
}
return is;
}
This is my >> operator in the Album class
istream& operator>>(istream& is, Album& album){
char artistName[256];
char albumTitle [256];
vector<Track> trackCollection;
Track track;
is.getline(artistName, 256, ':');
is.getline(albumTitle, 256);
while ((is>>track) && (!is.fail())){
trackCollection.push_back(track);
}
album = Album(artistName,albumTitle,trackCollection);
if (is.eof()){
is.clear(ios_base::failbit);
is.ignore(256,'\n');
}
else{
is.clear();
}
return is;
}
and this is my >> operator in the AlbumCollection class
std::istream& operator>>(std::istream& is,AlbumCollection& albumCollection){
Album album;
vector<Album>albums;
while (is>>album) {
albumCollection.addAlbum(album);
}
return is;
}
and the format of the input file .txt is:
The Jimi Hendrix Experience: Are you Experienced?
0:03:22 - Foxy Lady
0:03:32 - Highway Chile
Pink Floyd: Dark Side of the Moon
0:01:30 - Speak to Me
0:02:43 - Breathe
Could u please help me on that? I did try my best to solve but still i cannot do this :(((((
Thank you really much