Crashing when finding searchKey = Stimson , i dont know why

//classData Array.cpp
//data items as class objects

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Person
{
private:
string lastName;
string firstName;
int age;
//----------------------------------------------------------
public:
Person(string last,string first, int a): //constructor
lastName(last), firstName(first), age(a)
{ }
//----------------------------------------------------------
void displayPerson()
{
cout << " Last name: " << lastName;
cout << " First name:" << firstName;
cout << " Age: " << age << endl;
}
//---------------------------------------------------------
string getLast()
{return lastName;} // get last name

}; // end class person

class ClassDataArray
{
private:
vector<Person*> v;
int nElems;
public:

ClassDataArray(int max) : nElems(0)
{ v.resize(max);}

~ClassDataArray()
{
for(int j=0; j<nElems; j++)
delete v[j];
}

Person* find(string searchName)
{
int j;
for(int j=0; j<nElems; j++)

if(v[j]->getLast()== searchName)
break;

if(j == nElems)
return NULL;
else
return v[j];

};

void insert(string last, string first, int age)
{
v[nElems] = new Person(last, first, age);
nElems++;
}

bool remove(string searchName)
{
int j;
for(j=0; j<nElems; j++)
if( v[j] ->getLast() == searchName)
break;
if (j==nElems)
return false;
else
{
delete v[j];
for(int k=j; k<nElems;k++)
nElems--;
return true;
}
}

void displayA()
{
for(int j=0; j<nElems; j++)
v[j]->displayPerson();
}

}; //end class ClassDataArray
////////////////////////////////////////////////////////////////
int main()
{
int maxSize = 100; //array size
ClassDataArray arr(maxSize); //array

arr.insert("Evans", "Patty", 24); //insert 10 items
arr.insert("Smith", "Lorraine", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Adams", "Henry", 63);
arr.insert("Hashimoto", "Sato", 21);
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Lamarque", "Henry", 54);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);

arr.displayA(); //display items

string searchKey = " Stimson "; //search for item
cout << "Searching for Stimson" << endl;
Person* found;
found=arr.find(searchKey);
if(found != NULL)
{
cout << " Found ";

found->displayPerson();
}
else
cout << " Can’t find " << searchKey << endl;
cout << "Deleting Smith, Yee, and Creswell" << endl;
arr.remove("Smith"); //delete 3 items
arr.remove("Yee");
arr.remove("Creswell");
arr.displayA(); //display items again

return 0;
} //end main()
closed account (DSLq5Di1)
1
2
3
4
5
Person* find(string searchName)
{
    int j;
    for(int j=0; j<nElems; j++) // a silly mistake?
...

The remove function has a problem too.. you are deleting the object an element points to, but not the element itself. I assume you are trying to avoid erase() as that would alter the size of your vector? If that is the case,

#include <algorithm>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool remove(string searchName)
{
    auto end = v.begin() + nElems;
    auto found = std::remove_if(v.begin(), end, [&](Person*& p) -> bool
    {
        bool remove = (p->getLast() == searchName);

        if (remove)
        {
            delete p;
            nElems--;
        }
        return remove;
    });
    return (found != end);
}

http://www.cplusplus.com/reference/algorithm/remove_if/
lol silly mistakes always kill me, and i did forget some stuff in the Member Function remove, Thank You very much for your help. :)
Topic archived. No new replies allowed.