2

I have a class Employees. I'm trying to make the user insert and delete an employee but it's not working. The size of the vectors should be 500.

class Employees{
public:
    int maxx = 500;
    vector<string> Surname;
    vector<string> FirstName;
    vector<string> birthdate;
    int vacation[500];
public:
    Employees() : Surname(500) {}
};

This is the function that inserts, but printing elements of the vectors is not working at all:

void Process(Employees ZZ){

    string dateyear;
    string datemonth;
    string dateday;
    int dateyear1;
    int datemonth1;
    int dateday1;
    int Realage;
    int Vacationi = 0;

    for(int i = 0; i < 500; i++) {

        string s;
        cin >> s;
        string d;
        cin >> d;
        string c;
        cin >> c;

        ZZ.Surname.push_back(s);
        ZZ.FirstName.push_back(d);
        ZZ.birthdate.push_back(c);

        cout << endl << ZZ.Surname[1] << endl;
    }

Now the delete function, if I input a string then search for it in the vector then get his index then delete, but the vector doesn't update any values.

void DeleteEmployee(Employees ZZ){

    cout<< endl <<  ZZ.Surname[1] << endl ;

    for (int i = 0; i < ZZ.Surname.size(); i++){
        cout << ZZ.Surname[i] ;
    }
    cout << " delete employee";
    string delete1;
    cin >> delete1;

    auto it = std::find(ZZ.Surname.begin(), ZZ.Surname.end(), delete1);
    if (it == ZZ.Surname.end())
    {
        cout<< " name not in vector "  << endl;
    }
    else
    {
        //auto index = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_)));
        //ZZ.Surname.erase(ZZ.Surname.begin()+index) ;
    }
}

This is the main function, also the values of the vector are not printing:

int main()
{
    Employees ZZ;
    Process(ZZ);
    DeleteEmployee(ZZ);
    cout << "fyccck";

    for (int i = 0; i < ZZ.Surname.size(); i++){
        cout << ZZ.Surname[i] ;
    }
}
0

1 Answer 1

1

There are a lot of things wrong with this code. But the particular issue you are asking about is caused by your functions passing the Employees object by value, so a copy is made, and any changes you make to the copy are not reflected in the original object in main().

You need to change the parameters to pass the Employees object by reference instead:

void Process(Employees &ZZ)
void DeleteEmployee(Employees &ZZ)

That being said, the whole design of the code is not good in general. The vectors are not being kept in sync properly, and for that matter you are using more vectors then you actually need, 1 single vector will suffice. And Process() and DeleteEmployee() should be members of the Employees class, not separate functions. And they are both accessing out-of-bounds of the Surname vector.

I would suggest completely rewriting the code from scratch, for instance something more like this:

struct Employee{
    string Surname;
    string FirstName;
    string BirthDate;
    int Vacation;

    string DisplayName() const { return Surname + ", " + FirstName; }
};

class Employees{
public:
    static const int maxx = 500;
    vector<Employee> employees;

    Employees() { employees.reserve(maxx); }

    bool Add(const Employee &e);
    bool Delete(string Surname, string FirstName);
};

bool Employees::Add(const Employee &e) {
    if (employees.size() < maxx) {
        employees.push_back(e);
        return true;
    }
    return false;
}

bool Employees::Delete(string Surname, string FirstName) {
    auto it = std::find_if(employees.begin(), employees.end(),
        [&](const Employee &e){
            return e.Surname == Surname && e.FirstName == FirstName;
        }
    );
    if (it != employees.end()) {
        employees.erase(it);
        return true;
    }
    return false;
}

int main()
{
    Employees ZZ;

    for(int i = 0; i < Employees::maxx; ++i) {
        Employee e;
        cin >> e.Surname;
        cin >> e.FirstName;
        cin >> e.BirthDate;
        e.Vacation = 0;//cin >> e.Vacation;

        ZZ.Add(e);

        cout << endl << e.DisplayName() << endl;
    }

    cout << " delete employee";
    string Surname, FirstName;
    if (cin >> Surname >> FirstName) {
        if (ZZ.Delete(Surname, FirstName)) {
            cout << " name deleted from vector " << endl;
        } else {
            cout << " name not in vector " << endl;
        }
    }

    cout << "fyccck";

    for (auto &e : ZZ.employees) {
        cout << e.DisplayName() << endl;
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

can you give me brief about wrong things and yeah passing by reference it's alright and worked but here { class Employees{ public: int maxx = 500; vector<string> Surname; vector<string> FirstName; vector<string> birthdate; int vacation[500]; public: Employees() : Surname(500) {} }; } it doesn't create a vector of size 500 i wanna to create a vector of size 500 for each one of them i searched a lot and this what i found Employees() : Surname(500) {}
"it doesn't create a vector of size 500" - you have 3 vectors and 1 fixed array, why? Your constructor is pre-sizing only 1 of the vectors. And worse, you are pre-sizing it and then pushing 500 more elements into it. So all of your vectors/arrays get mismatched. Why are you using 4 separate vectors/arrays at all? 1 single vector/array will suffice, just put each person's info into a struct/class instead. I have added an example for that.
i want 3 vectors of size 500 to access every employee at the same index e.g. entering Surname,FirstName,BirthDate at 0 index that i could delete all at 0 index . excuse me i'm quite weak in OOP but when i access Employee names like this "cout << endl << "Name" << e.Surname[i]<< endl ;" it doesn't work .. how to use alternative way (not struct) to make kinda Vector of Class object of size 500 and access every employee of the 500
You don't need 3 vectors. 1 vector of struct/class elements will suffice, as I demonstrated in my answer. Your alternative is not a good idea, it is very error prone (as your original code demonstrated)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.