Hi again!
So, I have an object that gets appended to a linked list now. Now I need to have all of that item's data output in a formatted manner - something similar to this:
CD Artist CD Title CD Length Track Name Track Length
Arist name here Title here hh:mm:ss Track name here mm:ss
Track name here mm:ss
Track name here mm:ss
The problem with this is that the output for this is all in the overloaded << operator for my CD object and I'm having trouble figuring out how to append the track names and track lenghts, which are stored in their own linked lists (as per my assignment instructions), to a string, which I feel should be the best way to do things?
I don't feel like my code is far off, but.. if I was right, I wouldn't need help.
Code that should actually be right:
ostream &operator << (ostream &strm, const CD &obj)
{
string outCD = "";
outCD.append("\t");
outCD.append(obj.artist);
outCD.append("\t\t");
outCD.append(obj.title);
outCD.append("\t\t");
outCD.append(obj.length);
for (int it = 0; it <= obj.trackNum; it++)
{
if (it == 0)
{
outCD.append("\t\t");
outCD.append(obj.tracks.returnString(it));
outCD.append("\t\t");
outCD.append(obj.tracktimes.returnString(it));
}
else
{
outCD.append("\n\t\t\t\t\t\t\t\t");
outCD.append(obj.tracks.returnString(it));
outCD.append("\t\t");
outCD.append(obj.tracktimes.returnString(it));
}
}
strm << outCD << endl;
return strm;
}
Code that's giving me trouble:
//Return Single Stored String****************
//*******************************************
template <class T>
string TList<T>::returnString(int thisNode) const
{
ListNode<T> *nodePtr;
//Set nodePtr to the first position.
nodePtr = head;
//Traverse the list, moving to the nth node, where n is thisNode.
for (int it = 0; it < thisNode; it++)
{
if (nodePtr)
{
nodePtr = nodePtr->next;
}
}
//Return the string in the current node.
if (nodePtr)
return nodePtr->value;
}
1: I'm going to replace all the tabs with better formatting once I get it working.
2: If I throw some /* */ around my loop, it works fine, and it doesn't seem like anything's wrong with my loop.
3: Only a single string is ever stored in the tracks and tracktimes lists.