Below is a method that properly deletes a Professor from a list of Professors.
If this method is not called(as there are other options in the menu) then the program terminates just fine. However, after calling this method to delete a Professor, and subsequently terminating the program after seeing that the method did in fact perform delete correctly, a Seg Fault appears.
Folks in #C++ on freenode were very helpful, but we did not completely get rid of the problem. It is believed the problem may lie within the for loop with profIterator. I do know that the program runs without a hitch if there exists only one element in the list. But not for more than one element, which is where the for loop comes into play. It was suggested I create an operator<< overload and output what profIterator is returning.. but for some reason, the standard style:
Code:
ostream& operator<<(ostream& os, const Professor& P) {
os << P.getLastName() << ", " << P.getFirstName() << endl;
}
does not compile correctly. The errors fill a large screen with 'trait' stuff.
Can you see the problem without having to implement an operator<< method? I'd be grateful if you could.
Here's the method:
Code:
// delete a specific professor
bool deleteProfessor()
{
// local variable for readibility
int size = profs.size();
// handle empty list
if ( size < 1 ) {
cout << endl << " The list contains " << profs.size() << " elements.\n" << endl;
// handle non-empty list
} else {
// handle one element list, no need to iterate
if ( size == 1 ) {
cout << endl << " Only one element in the list. Delete (y/n)? " << flush;
char choice; // for yes or no answer
cin >> choice; // get user's input
cin.ignore( 100, '\n' ); // ignore all input to end of line
if ( toupper( choice ) == 'Y' ) {
// delete the only professor in the list
profs.clear();
cout << "\n Professor succesfully deleted. 'P' to print.\n" << endl;
return true; // return immediately instead of continuing
} else {
// notify user that no one was deleted
cout << "\n No element was deleted. List is intact.\n" << endl;
return true; // return immediately instead of continuing
}
// handle larger than one element list
} else {
// present the list for viewing
handlePrintList();
cout << "\nWhich professor do you wish to delete (1-" << size
<< ", or 0 to quit)? " << flush;
int choice; // for user input(which professor)
char* option; // for conversion of string to int
cin >> option; // get the user's input
cin.ignore( 100, '\n' ); // ignore all input to end of line
choice = atoi( option ); // convert to int for evaluation
// quit the operation if user desires to do so
if ( choice < 1 ) {
cout << "\n Quitting - returning to main menu.\n" << endl;
// take the appropriate action as requested
} else if ( choice >= 1 && choice <= size ) {
// delete the chosen professor
list<Professor>::iterator profIterator; // iterator for stepping through Professor list
int k = 1; // counter used to number list items
for ( profIterator = profs.begin(); // pointer to first element
k != choice; // stop point doesn't equal input
profIterator++, k++ ) // increment both iterator and counter
; // <- purpose is to move iterator to desired location and stop/continue
profs.erase( profIterator ); // erase professor at profIterator location
// notify user of success
cout << "\n Professor succesfully deleted. 'P' to print.\n" << endl;
} else {
// error handling
cout << "\n Invalid input. Please try again.\n" << endl;
}
}
}
return true;
}
Thanks
EDIT: blasted.. well if anyone needs this there ya go. the problem was solved ten minutes after I posted this. Here's the correct codeblock that was causing the problem:
Code:
int choice; // for user input(which professor)
string option; // user option for conversion of string to int
cin >> option; // get the user's input
istringstream i(option); // using stringstream
i >> choice; // convert string to int