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.
#include<iostream>
#include<cmath>
usingnamespace 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?
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().