searching and deleting

hi guys,

basically i am making a bank account program for an assignment, and now i have encountered a problem i cannot get my head round. Basically i have created a Customer vector and i am creating instances of Customer calling the constructor.

Customer *cust1 = new Customer(param,param,etc);

and then using the push_back funciton to add it to the vector. My system should be able to delete a customer and this is what i am kind of stuck on.

Note: I created a vector called custVec.

cout << "Enter customer name: " << endl;
cin >> custName;
int iCust;
bool custFound = false;
for(iCust = 0; iCust < custVec.size(); iCust++)
{
if(custName == custVec[iCust]->getLastName())
{
custFound = true;
//delete customer here
break;
}
}
if(custFound == false)
{
cout << "A customer with this name was not found. Please add customer to the system first." << endl << endl;
}

As you can see. I have created a string variable where the user enters the customer surname and a for loop which loops through the vector. If the surname string matches the name found in the vector, i want it to delete the customer. Now i guess that I would probably need to create a Customer pointer and then use the delete keyword to delete the customer.

Can somebody please tell me how to do this. It would be most appreciated. Many thanks!
I think you shoud use iterator.
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
#include <vector>
#include <string>
#include <iostream>

using namespace std;

class CCustomer
{
public:
	CCustomer(string name)
	{
		this->name =name;
	}
	string getLastName() const
	{
		return name;
	}
private:
	string name;
};

int main(int i, char* argv[])
{ 
	vector<CCustomer> custVec;
	CCustomer   customer1("Bujiwu");
	CCustomer   customer2("Swallow");

	custVec.push_back(customer1);
	custVec.push_back(customer2);

	string custName;
	cout << "Enter customer name: " << endl;
	cin >> custName;

	vector<CCustomer>::iterator Itor;
	bool custFound = false;
	for( Itor=custVec.begin(); Itor!=custVec.end(); Itor++)
	{
		if( custName ==Itor->getLastName() )
		{
			custFound = true;
                        custVec.erase(Itor);
			break;
		}
	}

	if(custFound == false)
	{
		cout << "A customer with this name was not found. Please add customer to the system first." << endl << endl;
	}
    
	return 0;
} 
Last edited on
This is probably what you need:
1
2
delete custVec[iCust];
custVec[iCust]=0; //or NULL, if you're into that sort of thing 

Note that this will leave empty spots in the vector, and that you will need to check that custVec[iCust]!=NULL before deferencing or you'll get a segmentation fault.
The good part is that you will save some time by not displacing the range [iCust+1;n-1], which can be a real time-saver in the long run.
http://www.cplusplus.com/reference/stl/vector/

Check this out so you can remove from the vector easier.
After deleting the object, you could use vector's erase method to remove it from the vector.
ok guys. thank you all for your speedy replies :D
Topic archived. No new replies allowed.