I have this iStream function to read in a .txt file of data in the format of:
Pink Floyd: Dark Side of the Moon
0:01:30 - Speak to Me
My function almost works perfectly.
Here are my problems that I just can’t figure out.
1) It prints (outputs) as each track is added to an Album (so prints x copies of the first album, each with the next track attached.
2) When a new Album is created, it keeps the old tracks from the previous album and continues to add them one-by-one. (so the final album has every single track from all albums)
Obviously I want each album to only be printed once, and each album to only have its own songs… Any help would be appreciated. Thanks.
I see a number of problems with your code.
You aren’t using the variable
stringstreamFirstandlineis empty at this point.There are a number of problems with this line.
One the very first line,
artistName,albumTitle, andTrackVectorare empty. When you finally come across a new album, theartistName,albumTitle, andTrackVectorare those for the previous album, not the current one. The values are correct by the time you get to the tracks on an album — but that’s not when you want to create a new album object.As placed, this statement creates an
Albumobject for each line in the input file. The right place to create a new album object is when you have encountered a new album entry in the input file.Why the convoluted names, and why do you need two variables? An alternative would be to use but one
stringstream, created as the first line of yourwhileloop.Don’t replicate your boolean conditions like this. If you mean
else(which is what you mean), just useelse. The line is either an album entry or a track entry; there is nothing else.You don’t have any error handling. What if you can’t parse what presumably should be an album entry, or what presumably should be a track entry?
What you need to do (pseudocode):