Hello, I am having a issue here. Somehow I can't remove or erase an class object inside the vector of objects. To make it clear ill add some parts of my code.
void edit(string empId,int companyId){
string depId;
fflush(stdin);
cout<<"Enter the department id to change"<<endl;
getline(cin,depId);
fflush(stdin);
string name;
string id;
for(unsigned i=0; i<comp[companyId].depVec.size(); i++){
for(unsigned j=0; j<comp[companyId].depVec[i].emp.size(); j++){
if(comp[companyId].depVec[i].emp[j].getEmpId() == empId){
name = comp[companyId].depVec[i].emp[j].getEmpName();
id = comp[companyId].depVec[i].emp[j].getEmpId();
comp[companyId].depVec[i].emp[j].erase();
}
}
}
for(unsigned i=0; i<comp[companyId].depVec.size(); i++){
if(comp[companyId].depVec[i].getDepId()==depId){
comp[companyId].depVec[i].createEmp(name,id);
}
}
}
So as you can see i tried to erase the employee data from vector of employees, I know that I am using erase function not in right way but somehow i cant manage to use it. You may wonder about comp, depVec, its a vector of class objects. I organized my class such as, each company class object there is department class object, so each company can have many departments inside so i declared vector of departments, and also on each department there is a employee which is also vector of employees. So i was trying to implement a function where I need to change the employee to another department within a 1 company, this is i tried so far. I tried to get the employee name and the id then store it on a name, id variables then i push_back those into new department where it is equal to user's input department id.
Oh my god thank you very much, that solved my problem. I really appreciate you, i've been trying this whole day and i kinda feel bad :(, i think i wont be a good coder and for that auto i never knew that it is possible to do like that, once again thank you really much.
@cire sorry can I ask 1 more question, how can i make that erasing function to be true only if the user inputed department id exists within the company not for other company? For example when i enter 1-b which is department id of another company. Somehow I cant make that happen i tried like
I don't think you want that exclamation mark there.
how can i make that erasing function to be true only if the user inputed department id exists within the company not for other company?
I'm not sure exactly what you're asking, but if you want to erase when depId is equal to comp[companyId].depVec[i].getDepId(), then you'll want to use equality (==) in the comparison, and not inequality (!=), but I suspect that extraneous exclamation mark was screwing you up.
well I mean i want to make that erase happen only if the user inputed department id exists in the current company, but when i enter another company's depId it removes the employee from deparment. For example employee can not change to another company's department. Without if statement the erase function will erase even when I input random non existing department id, so I dont want to remove the employee from a vector if I enter just random id it should be true only if user input id exists in current company. I tried the 1 you suggested but then the erase is not removing the employee from department
void remove(string empId, int companyId, string deptId)
{
auto& departments = comp[companyId].depVec;
// find the deptartment with matching deptId
std::size_t index = 0;
while (index < departments.size() && departments[index].getDepId() != deptId)
++index;
if (index == departments.size())
{
// No matching department was found for the specified company.
// so do nothing
return;
}
auto & employees = departments[index].emp;
index = 0;
while (index < employees.size() && employees[index].getEmpId() != empId)
++index;
if (index == employees.size())
{
// No matching employee was found in the specified company/department.
// so do nothing
return;
}
employees.erase(employees.begin() + index);
}
@cire it seems not working, or maybe i am doing it wrong i just copy pasted your function and called that function on my edit function passing those parameters, am I doing it wrong?