-3

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

  • `while (is >> track)` is sufficient, no need for `&& !is.fail()`. – chris Dec 11 '12 at 18:28
  • 3
    reduce your code to the actual problem, most people will just downvote instead of reading the entire post. – Syntactic Fructose Dec 11 '12 at 18:31
  • @Need4Sleep i try to reduce my code a lot but what i have been posted is my acutallly problem which is the operator >> stream. And i think it happens in all 4 classes coz if if any of these does have problem, it cannot read the file. Sorry :((((( – Ht Sonca Dec 11 '12 at 18:50

1 Answers1

0

The problem is in the operator>> of Album. That operator tries to read as many tracks as it can, until the Track operator>> signals a read failure. Afterwards, the Album operator>> resets the failure status of the stream. By continually resetting the failure status of the stream, even if the artist name or album title could not be read, the operator can't signal that it has exhausted all albums.

As it is typically not possible to tell where a 'collection of X' ends when stored in a file, it is customary to store the expected number of items before the actual items. To do that, you would need to change your file format to (for example):

2
The Jimi Hendrix Experience: Are you Experienced? 
2
0:03:22 - Foxy Lady
0:03:32 - Highway Chile
Pink Floyd: Dark Side of the Moon
2
0:01:30 - Speak to Me
0:02:43 - Breathe

If changing the file format is not an option, you can also change the operator>> for Album to bail out early if there is no artist and/or album to read.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
  • hi, thank u for ur help. it does work now, however, it cannot read the hold file. i dont really know why – Ht Sonca Dec 11 '12 at 19:06