how do i count the amount of bonus from salary? || structures

Jan 19, 2021 at 11:47am
Q: info about employees contains their last, first, dad's names, position, gender, the date they joined, salary and bonus.

Bonus is not filled(declared?)

Calculate the bonus for each employee using this info:
5% is they worked for 5 years and more; 10% for >=10 years; 30% for >=20 years

....

i have some idea in my head but i just don't get how to actualize it.
i'm thinking of getting another function for calculating the bonus from the salary, but i don't know how to bring the date there

ps: if i just use the year they joined instead of the date it'll be okay by my teacher, since it's less complicated and doesn't require using libraries i'm not familiar with

thanks in advance
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
 #include <iostream>
#include <string>
using namespace std;

struct Employee
{
    string LastName;
    string FirstName;
    string DadsName;
    string position;
    char sex;
    int year_joined;
    double salary;
    double bonus;
};

void EmployeeChart(const Employee& employee)
{
    cout << "Last name: " << employee.LastName << '\n';
    cout << "First name: " << employee.FirstName << '\n';
    cout << "Dad's name: " << employee.DadsName << '\n';
    cout << "Position: " << employee.position << '\n';
    cout << "Gender: " << employee.sex << '\n';
    cout << "Year joined: " << employee.year_joined << '\n';
    cout << "Salary: " << employee.salary << '\n';
    cout << "Bonus: " << employee.bonus << '\n';
}

/*int Bonus(const Employee& amount)
{
    for()
}
*/

int main()
{
    Employee laura = { "Laura", "Fidarova", "Alanovna", "stud", 'F', 2015, 45000 }; // +bonus in the end 
    EmployeeChart(laura);
    return 0;
}
Last edited on Jan 19, 2021 at 11:49am
Jan 19, 2021 at 12:09pm
To calc the duration of the employment you need to get the current year first.
1
2
3
4
5
6
7
int get_current_year() // #include <ctime>
{
  time_t t = time(nullptr);   
  struct tm* now = localtime(&t);

  return now->tm_year + 1900;
}


Now you can try to write a function to calculate the bonus.
Jan 19, 2021 at 1:49pm
To determine the number of years between a specified year and the current year, consider:

1
2
3
4
5
6
7
8
#include <ctime>

int years(int year)
{
	const auto t {std::time(nullptr)};

	return (std::localtime(&t)->tm_year + 1900) - year;
}

Last edited on Jan 19, 2021 at 1:52pm
Jan 20, 2021 at 2:32pm
thank you so much! am i on the right track or even close to it? i'm a little confused about how to correctly use stuctures. is that a correct function for Bonus? is it is, where and how do i put it so it works as needed?
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
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include<ctime>
using namespace std;

struct Employee
{
    string LastName;
    string FirstName;
    string DadsName;
    string position;
    char sex;
    int year_joined;
    double salary;
    double bonus;
};

void EmployeeChart(const Employee& employee)
{
    cout << "Last name: " << employee.LastName << '\n';
    cout << "First name: " << employee.FirstName << '\n';
    cout << "Dad's name: " << employee.DadsName << '\n';
    cout << "Position: " << employee.position << '\n';
    cout << "Gender: " << employee.sex << '\n';
    cout << "Year joined: " << employee.year_joined << '\n';
    cout << "Salary: " << employee.salary << '\n';
    cout << "Bonus: " << employee.bonus << '\n';
}

int years(int year)
{
    const auto t{ time(nullptr) };

    return (localtime(&t)->tm_year + 1900) - year;
}

double Bonus(const Employee& amount)
{
    if (years(amount.year_joined) >= 5 && years(amount.year_joined) <= 10)
        return amount.salary * 5 / 100;
    
    if (years(amount.year_joined) > 10 && years(amount.year_joined) < 20)
        return amount.salary * 10 / 100;

    if (years(amount.year_joined) >= 20)
        return amount.salary * 30 / 100;
}


int main()
{
    Employee laura = { "Laura", "Fidarova", "Alanovna", "stud", 'F', 2015, 45000 };
    EmployeeChart(laura);
    return 0;
}
Last edited on Jan 20, 2021 at 2:33pm
Jan 20, 2021 at 5:04pm
In Bouus, there's no need to keep using the function years() - you just need to use it once and save the result in a variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double Bonus(const Employee& amount)
{
	const int yrs_wrkd = years(amount.year_joined);

	int bnsper = 0;

	if (yrs_wrkd >= 5)
		bnsper += 5;

	if (yrs_wrkd >= 10)
		bnsper += 5;

	if (yrs_wrkd >= 20)
		bnsper += 20;

	return amount.salary * bnsper / 100.0;
}

Jan 20, 2021 at 5:31pm
The original had a flaw:
1
2
3
4
double bonus() {
  if ( x ) return y;
  // compiler should warn: this function does not return a value
}

When x is false (has worked under 5 years), the function does not return a value.

The seeplus version always returns a value.

However, the incremental way to accumulate the bonus might be hard to read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int bnsper = 0;
if (yrs_wrkd >= 5)
  bnsper += 5;
if (yrs_wrkd >= 10)
  bnsper += 5;
if (yrs_wrkd >= 20)
  bnsper += 20;

// equals
int bnsper = 0;
if (yrs_wrkd >= 20)
  bnsper = 30;
else if (yrs_wrkd >= 10)
  bnsper = 10;
else if (yrs_wrkd >= 5)
  bnsper = 5;

Jan 20, 2021 at 8:02pm
okay, thank you so much!
Jan 20, 2021 at 8:56pm
Lastly, bonus should be a method of Employee. Don't store the bonus value since it can change if the salary or date changes. So something like this (untested):
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
struct Employee
{
    string LastName;
    string FirstName;
    string DadsName;
    string position;
    char sex;
    int year_joined;
    double salary;
    double bonus();
};

double Employee::bonus()
{
    int yearsWorked = years(year_joined);
    double rate;
    // 5% if they worked for 5 years and more; 10% for >=10 years; 30% for >=20 years
    if (yearsWorked >= 20) {
        rate = 0.30;
    } else if (yearsWorked >= 10) {
        rate = 0.10;
    } else if (yearsWorked >= 5) {
        rate = 0.05;
    } else {
        rate = 0.0;
    }
    return salary * rate;
}

Topic archived. No new replies allowed.