need help modifying 2 functions

hey guys so i have a few different files with code...

here is a header file...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef EMPLOYEE_H
#define EMPLOYEE_H


#include <string>

using namespace std;

class Employee
{
private:
    string name;
    double salary;
public:
    Employee();
    Employee(string n, double s);
    string get_name();
    double get_salary();
    void set_salary(double s);
};

#endif



okay here is the code...

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

#include <iostream>

#include "Employee.h"

using namespace std;

Employee::Employee(): name(""), salary(0) { }

Employee::Employee(string n, double s): name(n), salary(s) { }

string Employee::get_name() 
{ 
    return name; 
}

double Employee::get_salary() 
{ 
    return salary; 
}

void Employee::set_salary(double s) 
{ 
    salary = s; 
}


here is the 2nd header file

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

#ifndef STAFF_H
#define STAFF_H


#include <iostream>
#include <string>
#include <vector>

#include "Employee.h"

using namespace std;

class Staff
{
private:
    vector<Employee> members;
    int find(string n); // returns position of n in members or -1 if not found
public:
    Staff() { }
    void add_employee(Employee e); // better: const Employee &
    void raise_salary(string n, int percent);
    void print();
};

#endif



More code...

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

#include <iostream>
#include <string>
#include <vector>

#include "Staff.h"

using namespace std;

int Staff::find(string n)
{
    for (int i = 0; i < members.size(); i++)
        if (members[i].get_name() == n)
            return i;
    return -1;
}

void Staff::add_employee(Employee e)
{
    members.push_back(e);
}

void Staff::raise_salary(string n, int percent)
{
    double salary;

    int i = find(n);
    if (i == -1) {
        cout << "Couldn't find " << n << endl;
    } else {
        salary = members[i].get_salary();
        salary += salary * percent/100; 
        members[i].set_salary(salary);
    }
}

void Staff::print()
{
    for (int i = 0; i < members.size(); i++)
        cout << members[i].get_name() << " " << members[i].get_salary() << endl;
}



and finally the main function...

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

#include <iostream>
#include <string>
#include <vector>

#include "Employee.h"
#include "Staff.h"

using namespace std;

main()
{
    Staff company;
    string name;
    double salary;

    cout << "Enter a set of name, salary pairs, terminate with -1 -1\n";
    cin >> name >> salary;

    while (name != "-1") {
        company.add_employee(Employee(name, salary));

        cin >> name >> salary;
    }

    string response;
    int percent;

    cout << "Raise anybody's salary? ";
    cin >> response;

    while (response == "yes" or response == "y") {
        cout << "Enter name and percent: ";
        cin >> name >> percent;
        company.raise_salary(name, percent);

        cout << "Raise anybody else's salary? ";
        cin >> response;
    }

    company.print();
}



Now what in need to do is modify the find() in the add_employee() so that checks and doesn't allow a user to enter a name that already exists

second i need to in raise_salary() give the minimum of percentage computed or $1,000.00

i know that this is a lot to ask for but if anyone has any ideas on what i need to do i would really appreciate any assistance.

I have been trying all day at it but i am really stuck...

I thank you all for your help and your time...

-shad0w

Now what in need to do is modify the find() in the add_employee() so that checks and doesn't allow a user to enter a name that already exists

Why don't you use your find function to see if it returns -1, in this case the employee doesn't exist.
well i can do that but i need it to check to make sure that the user isn't entering a name that already exists...

anyone have any ideas on how i can do that??..
well i can do that but i need it to check to make sure that the user isn't entering a name that already exists...


Think carefully about what Thomas1965 said here :+)

Why don't you use your find function to see if it returns -1, in this case the employee doesn't exist.


Some other things:

Pass strings by const reference:

int Staff::find(const std::string& n)

Try using a range based for loop:

1
2
3
4
5
6
7
8
void Staff::print()
{
    for (const auto& item : members) 
    {   // always use braces even if there is 1 statement
        // it will save you one day when adding new code
        cout << item.get_name() << " " << item.get_salary() << endl;
    }
}


There are other places where you could do something similar.

When using double literal values in code, I like to put digits before and after the decimal point. It saves the compiler doing an implicit cast to double, and can help with hard to see errors:

1
2
salary += salary * percent/100;
salary *=  1.0 + percent/100.0;


Use const where ever you can:

1
2
void Staff::raise_salary(const std::string& n, const int percent)
{

Note that I mark the parameters const in the implementation, not the class definition.

Functions that don't change the value of the class members, should be marked const:

1
2
3
4
5
6
7
8
9
10
11
12
class Employee
{
private:
    string name;
    double salary;
public:
    Employee();
    Employee(string n, double s);
    string get_name() const ;
    double get_salary() const ;
    void set_salary(double s);
};


Good Luck !!




Topic archived. No new replies allowed.