Hello! I'm working on an assignment that requires me to store custom classes in an STL vector and do things like search, display, and delete them, then write them all to file.
This is going well, but my code to delete an object (adapted from this thread) works on any object EXCEPT the last object; it crashes whenever I try to delete the last one in the chain.
Here is my code for the deletion:
vector<course> cache; //holds objects in memory
vector<course>::iterator point; //points at the vector
//fill vector with data, etc etc
cout << "Input course number to delete: ";
cin >> target;
point = cache.begin();
for(int n=0; point != cache.end(); point++, n++) {
if(point->get_number() == target)
{ cache.erase(point); } //erases object
}
Any idea what I'm doing wrong? Thanks!