c++ delete function code

the program asks user to add employee and save using ofstream, and delete ::->

""Delete an Employee. This should ask for the last name of the employee (your program does not
need to handle duplicate last names). You then need to write code to search the database for the
given employee and delete him/her from the database (i.e., the std::vector). If the user
enters in a name that does not exist in the database, report that information to the user and return
to the main menu.""

rest of the program should be as below:: but tell me the delete function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class employee
{
      public:
      employee(string name,string last,int sal)
      {mName =name;mLastName =last; mSalary= sal;}          
      virtual void printStats()
       {
       cout<<"Name ="<<mName<<endl<<"LastName ="<<mLastName<<endl<<"Salary ="<<mSalary<<endl;
       }
       virtual void save(ofstream& out)
       {
               out<<"Name ="<<mName<<endl;
               out<<"Last Name ="<<mLastName<<endl;
               out<<"Salary ="<<mSalary<<endl;
              }      
      protected:
             string mName;
             string mLastName;
             int mSalary;
                 };

class manager :public employee
{
      public:
             manager(string name,string last,int sal,int meet,int vac)
             :employee(name,last,sal)
             {mMeet=meet; mVac =vac;}
            void printStats()
            {
                 employee::printStats();
                 cout<<"Meeting ="<<mMeet<<endl<<"Vacation ="<<mVac<<endl;
                 
                 }
          void save(ofstream& out)
          {
               employee::save(out);
                  out<<"MANAGER:";
                   out<<"Meeting ="<<mMeet<<endl;
                  out<<"Vacation ="<<mVac<<endl;
                  out<<endl<<endl;
                  }       
        protected:
                  int mMeet;
                  int mVac;
                  };
class engineer:public employee
{
     .......................}
class researcher:public employee
{
    ......................}

int main()
{
    vector<employee*> database;
  int count =1;
 
ofstream out("~Spam~");

   employee* emp[50];
      string garbage; 
bool done =false;
int input=0;
int select1 =0;
int select2 =0;
int j=0;
string first; string last; int sal; int exp; int num; string trade1;string school;string topic;
int meet; int vac;
while(!done){   
        cout<<"Database size="<<database.size()<<endl;
                          cout<<"Databse contains:" ;
                          if(database.size()==0)
                          {
                          cout<<"Null"<<endl;
                          cout<<endl<<endl;
                          }
                          else
                          {
                          for(int i=0;i<database.size();i++)            
                          {      emp[i]->printStats();
                                  cout<<endl<<endl;
                                  }
                                  }
cout<<"1.Add Employee 2.Remove Employee 3.Quit!";
cin>>select1;
switch(select1){     case 1:
                 start:   cout<<"1.Engineer 2.Manager 3.Researcher";
                    cin>>select2;
                   
                    switch(select2)
                    {
                          case 1:   
                              getline(cin,garbage);
                              cout<<"Enter Name :"; getline(cin,first);
                              cout<<"Enter last name: "; getline(cin,last);
                                cout<<"Enter trade: ";
                              getline(cin,trade1);
             
                              cout<<"Enter salary:"; cin>>sal;

                               cout<<"Enter experience:";  cin>>exp;                          
                              cout<<"Enter number of members with c++:"; cin>>num;
                                  emp[j] =  new engineer(first,last,sal,num,exp,trade1);                                                                                                                 
                              database.push_back( emp[j]);
                              emp[j]->save(out);
                              j++;
                              break;
                              case 2:
                               getline(cin,garbage);
                              cout<<"Enter Name :"; getline(cin,first);
                              cout<<endl;
                              cout<<"Enter last name: "; getline(cin,last);
                              cout<<endl;
                              cout<<"Enter salary:"; cin>>sal;
                              cout<<endl;
                              cout<<"Enter Meeting:"; cin>>meet;
                              cout<<endl;
                              cout<<"Enter Vacation"; cin>>vac;
                              cout<<endl;
                             emp[j]=new manager(first,last,sal,meet,vac);
                              database.push_back( emp[j]);
                              emp[j]->save(out);
                             j++;
                              break;
                              case 3:
                                   getline(cin,garbage);
                                    cout<<"Enter Name :"; getline(cin,first);
                              cout<<endl;
                              cout<<"Enter last name: "; getline(cin,last);
                              cout<<endl;
                              cout<<"Enter salary:"; cin>>sal;
                              cout<<endl;
                              cout<<"Enter School:"; cin>>school;
                              cout<<endl;
                              cout<<"Enter Phd Topic:"; cin>>topic;
                              cout<<endl;
                               emp[j] =new researcher(first,last,sal,school,topic);
                              database.push_back(emp[j]);
                              emp[j]->save(out);
                              j++;
                              break;
                              default:
                                      cout<<"Wrong input"<<endl;
                                      goto start;
                                      break;
                                      }
                                      break;             
                     case 2:
                          cout<<"Enter index of employee"; cin>>input;
                          database.erase(database.begin()+input);
                          break
                          default:
                                  done =true;
                                  break;
                                  }
                             }
                                  for(int k =0;k<database.size();k++)
                                  {
                                          delete emp[k];}

                                                                                             
                                           system("pause");
                                           } 
I dont understand y it is difficult to delete an employee.

You can use vector iterator and write a function int the employee class that checks the last name , and if it is present then delete .

And as per memory leak is concerned, since you are not allocating any memory dynamically so no memory leak, u just have to delete the object, I don't see any problem. Can you tell me what is the problem u r facing.
problem is that i dont know STL yet, i have just done inheritance and polymorphism and the book is just in advance defining push_back and erase method.

second please see my another post..there is a problem in string (LastName)
closed account (S6k9GNh0)
I can't even read this the formatting is so irritating. And I even see system(). I give up...
Topic archived. No new replies allowed.