basic help -function and structs.

I JUST NEED HELP ON THE WRITING THE FUNCTION PART.

write a function called calculatePay( ) which takes an employee as an argument and returns the amount of money they need to be paid this pay period. Employees should be paid "time and a half" for all hours over 40. You should set values for the employee's hours and payRate before sending it to the function.

HERE'S WHAT I HAVE : http://cpp.sh/2rj56
Last edited on
I'll help wioth the syntax as you seem to have an idea of what to do.
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
#include <iostream>
#include <string>

struct employee 
{
	string name;
	double payRate;
	double hours;
};

double calculatePay(employee);  // you must declare "employee" before using it

int main()
{
	employee a;
	a.name = "Sterling Archer";
	a.payRate = 18.50;
	a.hours = 55;
	cout << a.name << " has earned " << calculatePay(a) << " this week.\n";
	
	system("PUASE");
	return 0;
}

double calculatePay(employee e) // added a variable e of type employee
{
	double pay = 0;  // declare local variable pay

	if (e.hours > 40) {
		pay += hours * payRate * 1.5;
	}

	if (e.hours <= 40) {
		pay += hours * payRate;
	}

	return pay;  // return the value we calculated
}
i got errors. :(
Hello Gerardo559,

I have answers. 42 https://www.youtube.com/watch?v=aboZctrHfK8

To my knowledge no one here can read minds. Also no one has the ability to have remote access to your computer.

So you need to post what code you have and the complete error messages so all can better understand what is going wrong.

Posting the link that you have may have worked at one time, but I believe it does not exist any more, so I can not see what was there.

Unless the code is very large it is better to post it here.

Awaiting you code,

Andy
@Gerardo559

Not sure if this is the cause of your error, but line 27 of your code, (and the same for kbw's code, line 21), is spelled wrong. It should be system("PAUSE");
1
2
3
4
5
6
7
8
9
10
11
12
13
double calculatePay(employee e)
{
	double pay = e.hours * e.payRate;

	//Employees should be paid "time and a half" for all hours over 40
	//that can be read as «they have a .5 bonus»
	const double work_limit = 40; //some day this will be reduced
	double extra_hours = e.hours - work_limit;
	if(extra_hours > 0)
		pay += extra_hours * 0.5 * e.payRate;

	return pay;
}
Last edited on
i think i got figured out now here my final code : http://cpp.sh/7ozdy
thanks for the help guys. :)
Hello Gerardo559,


Where is this company located. I would work for them since they pay over $1000 more than they should for 15 hours of overtime. And even for 40 hours or less it is twice what it should be.

Your function would work better as:
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
double calculatePay(employee e)
{
	constexpr double MAXREGHOURS{ 40 };

	double pay{};
	//double pay = e.hours *e.payRate;// <--- Why?

	if (e.hours > MAXREGHOURS) 
	{
		//pay = MAXREGHOURS * e.payRate;

		//std::cout << "\n  " << pay << '\n'; // <--- Used for testing.
		//std::cout << "\n  " << (e.hours - MAXREGHOURS) * (e.payRate * 1.5) << '\n'; // <--- Used for testing.

		//pay += (e.hours - MAXREGHOURS) * (e.payRate * 1.5);

		pay = (MAXREGHOURS * e.payRate) + ((e.hours - MAXREGHOURS) * (e.payRate * 1.5));

		//std::cout << "\n  " << pay << '\n'; // <--- Used for testing.
	}

	if (e.hours <= MAXREGHOURS)
	{
 		pay = e.hours * e.payRate;
	}

	return pay;
}

The commented line 10 - 19 I left to show what the function is doing in case you want to do some testing to see what line 17 does in one line.

Hope that helps,

Andy

Edit:
Last edited on
1
2
3
4
5
6
7
8
9
if (e.hours > MAXREGHOURS)
{
   //...
}
//if (e.hours <= MAXREGHOURS)
else
{
   //...
}
Registered users can post here. Sign in or register to post.