Bubba, Daved and iMalc thanks soo much for your help!
I've played and changed stuff here and there, and this is the result:
I'm asking the user for the node to delete, based on a datamember's value , putting that into a string and them deleting matches from the std::list.
Code:
case 'b':
{
//---------------------
//Delete a 'Lead(an object of a derived class)'
//---------------------
//---------------------
//NOTE: DELETES * MATCHES
//---------------------
cout << "You chose to delete an object of the derived class" << endl;
if(listname.size() == 0)
{
cout << "There are no derived class objects in the list" << endl;
}
else
{
string leadToDelete;
cout << "Enter the name of the lead to delete" << endl;
getline (cin, leadToDelete);
i iter(listname.begin());
i end(listname.end());
while (iter != end)
{
if ( (*iter)->getName() == leadToDelete )
{
delete *iter;
listname.erase(iter++);//Iterator incremented here because 'iter' becomes invalid after the erase():
}
else
{
++iter;
}
}
}
break;
}
Do you think it looks OK?
Now I'm trying to iterate through a list.
I want the user to be able to display node's values, by letting the user go forwards and backwards.
Code:
case 'f':
{
i iter(listname.begin());
i beginning(listname.begin());
i end(listname.end());
string nextPreviousChoice = "";
do{
cout << "Press 'n' then Enter to show next, 'p' then Enter for previous and 'q' then enter to return to menu" << endl;
getline (cin, nextPreviousChoice);
//FORWARDS
if(nextPreviousChoice == "n")
{
if(iter == listname.end())
{
cout << "at end of list" << endl;
}
else
{
//++iter;//Can overun end of list and cause a crash!!!
cout << "Name: " << (*iter)->getName() << endl;
//++iter;//seems not right either
}
}
//BACKWARDS
else if(nextPreviousChoice == "p")
{
if(iter == listname.begin())
{
cout << "at beginning of list" << endl;
}
else
{
--iter;
cout << "Name: " << (*iter)->getName() << endl;
}
}
else
{
cout << "Tou made a invalid choice" << endl;
}
}
while(nextPreviousChoice != "q");
break;//return to main menu
}
When a user over runs the two ends of the list, it compiles, but crashes.
When a user goes forwards and backwards, the two options of forwards and backwards are not combined correctly.
After trying to find an example of how it could be done most easily, I found THIS example.
Could anyone point me in the right direction with this?
Again, thanks for the great responses.
Helped me out a loads!