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!
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.