Problem with functions.

Hello everyone.
I am having a problem with functions. As I took programming (c++) course this semester, I'm having some beginners difficulties.
An employee is paid at a rate of $16.78 per hour for regular hours worked in a week. Any hours over that are paid at the overtime rate of one and one-half times that. Regular working hours are 40.
Write a program that will read in the number of hoursworked in a week and the number of dependents as input and that will then output the worker’s grosspay, each withholding amount, and the net take-home pay for the week.Write program using functions.


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
#include<iostream>
#include<cmath>
using namespace std;


	 int number_of_hours_worked;
	 double overtime;
	 double regular_salary=16.78;
	 double overtime_pay;

	 int overtimePay (int, double)
	 {
			int a=40;
			int b=1.5;

			overtime_pay=(regular_salary*a)+(overtime*b);
			return (overtime_pay);
	 }

	 int regularPay (int, double)
	 {
		   double regular_payment;
		   regular_payment=regular_salary*number_of_hours_worked;
		   return (regular_payment);

	 }


int main()
{
     cout<<"Please insert number of hours you have worked: ";	
	 cin>>number_of_hours_worked;

	 overtime=number_of_hours_worked-40;

	 

	 if (number_of_hours_worked>40)
	 {

		 cout<<"You have earned: "<<overtimePay<<" euros."<<endl;

	 }
	 else 
		 cout<<"You didnt work any additional hour."<<endl;
		 cout<<"You have earned: "<<regularPay<<" euros."<<endl;	
	
	
	
	system("pause");
	return 0;

}


What is wrong with my code? Why I can not call the function at my main one?
Last edited on
which part you are calling a function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	 int overtimePay (int, double)
	 {
			int a=40;
			int b=1.5;

			overtime_pay=(regular_salary*a)+(overtime*b);
			return (overtime_pay);
	 }

	 int regularPay (int, double)
	 {
		   double regular_payment;
		   regular_payment=regular_salary*number_of_hours_worked;
		   return (regular_payment);

	 }


this part ofc, but I think there may be some logical error about declaring. I can't realize where is the mistake
Last edited on
It isn't working because you aren't actually calling the functions. For example, at line 41 you have cout<<"You have earned: "<<overtimePay<<" euros."<<endl. Here, overtimePay is just a pointer to the function, so the program prints the value of the pointer. To call a function, you have to put parenthesis after the name and include any parameters in the parentheses. Since overTimePay takes two parameters, an int and a double, you need to pass those.

But wait, what are the parameters? At line 10 you declare two anonymous parameters. Hmm. Also, overtimePay returns an integer but the pay will probably be a floating point number. So I'm thinking overtimePay should be something like this:
1
2
3
4
5
6
7
8
9
// Given the number of hours worked, return the overtime pay
double overtimePay(double hoursWorked)
{
    double overtimeHours = hoursWorked - 40;
    if (overtimeHours > 0) {
        return overtimeHours * regular_salary * 1.5;
    } else {
        return 0.0;
}


You would need to make similar changes for regularPay().
Topic archived. No new replies allowed.