I see a number of problems with your code.
stringstream stringstreamFirst(line);
You aren't using the variable stringstreamFirst
and line
is empty at this point.
Album anAlbum(artistName,albumTitle,trackVector);
There are a number of problems with this line.
- It's using the wrong values.
One the very first line, artistName
, albumTitle
, and TrackVector
are empty. When you finally come across a new album, the artistName
, albumTitle
, and TrackVector
are 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.
- It's in the wrong place.
As placed, this statement creates an Album
object 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.
stringstream stringstreamNew(line);
stringstream stringstreamNewNew(line);
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 your while
loop.
if (!(line[8] == '-'))
else if (line[8] == '-')
Don't replicate your boolean conditions like this. If you mean else
(which is what you mean), just use else
. The line is either an album entry or a track entry; there is nothing else.
else // These lines are missing
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):
while (getline(istr, line)) {
stringstream linestream (line);
if (line looks like an album line) {
if (not the first line in the file) {
// Create an album using the artist name, album title, and track vector
// and add this album onto the vector of albums
}
// Parse the line for artist name and album title, preferably handling errors
// Clear the trackVector that now pertains to the previous album
}
else {
// Parse the line for track duration and name, preferably handling errors
// Add the track to the track vector.
}
}
// Create an album to cover the last album plus set of tracks