I want to update an object’s variables within a vector array of objects, using input from the user as to what variable they wish to update.
the code is:
class Employee {
public:
std::string empID, empName, empTitle, empHireDate, empSalary;
Employee() {}
// constructor
Employee(string id, string name, string title, string hireDate, string salary) :
empID(id),
empName(name),
empTitle(title),
empHireDate(hireDate),
empSalary(salary)
{}
//returns the employee's ID
string getEmpID() {
return empID;
}
};
(Operator== function, if you saw this project before, you know.)
class employeeMutator : public Employee
void updateEmployee(string input) {
//requests what the user wants to update, and updates it
if (input == "1") {
cout << "what is the new name? ";
string name;
cin >> name;
empName = name;
cout << "updated.";
cout << "n";
}
(...) repeat and change for other variables. Yes, yes, there is almost certainly a cleaner way to do
this.
}
}
};
int Main() {
vector <Employee> Payroll;
Employee employee;
(insert a few test employees here, they all have different IDs)
string mutate;
cout << "what would you like to update? '1' to update the name, '2' to update the title, '3' to update
the hire date, and '4' to update the salary: ";
cin >> mutate;
//looks for what the user wants to update
for (Employee employee : Payroll) {
if (input == employee.getEmpID()) {
employeeMutator update;
update.updateEmployee(mutate);
}
}
};
The code compiles and runs, but when I use a display method, it shows the vector was never updated.
I’ve been looking at other similar posts on this and other websites, which haven’t helped, and they have been noting a possible copying of the vector. What’s is going on here exactly to cause the update to fail, and how can I fix it?
Daniel Gowin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3