Functions with different return types and parameters. Need advice on my code! urgent

hi all

i need to write a program that calculates and prints payslips. User inputs are the names of the employees, the number of hours worked and the pay rate. i have to declare three functions

a) one for input:
b) one to calculate the employees pay;
c) one to print the pay slip.

The input function has to input the name of the employee, the number of hours worked and the hourly pay rate into the variables theEmployee, theHoursWorked and thePayRate. the variable 'employee' is a string and the other two are floats. as the values of the three variable numbers will be changed preference parameters need to be used.

The calculation function will receive two parameters that represent the number of hours worked and the hourly pay rate, do the calculation and return the pay for the employee. An employee, who has worked more than 40 hours, is paid 1.5 times the hourly rate for each hour of overtime. As the parameters are not changed in the function they should be value parameters. The function should return a float value which represents the pay.

the output function has to display the name of the employee, the number of hours worked, the number of overtime hours and the hourly pay rate entered by the user as well as the employees pay. eg

Pay slip for Harry Matsipe
Hours worked: 43.5 hours
Overtime hours: 3.5
Hourly pay rate: R125.35
Pay: R5672.09

The main function includes a for loop that allows the user to repeat the calculation of a pay slip for five employees.
*******************************************************************************

I have managed to complete some code but i am now very stuck and battling. Please can someone help me with the rest of the code for this problem

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
 #include <iostream>
#include <string>
using namespace std;
//declare function for the input
void theEmployee (string & employee)
{
    cout << "Enter the employees name: " << endl;
    cin >> employee;
}
void theHoursWorked (float & hoursworked)
{
    cout << "Enter the number of hours worked: " << endl;
    cin >> hoursworked;
}

void thePayRate (float & payRate)
{
    cout << "Enter the pay rate for the employee: " << endl;
    cin >> payRate;
}
//Declare function for the calculation

void thePay ( float pay, float payRate, float hoursworked)
{
    cout << "The pay for the employee is: " << endl;
    pay = payRate * hoursworked;
}
{

if ( hoursworked > 40 )
{
   overTimePay = ((hoursworked - 40) * (payRate * 1.5) + ( 40 * payRate ) )
}
else
{
    overTime = 0
}
}
int main()
{
    for ( i = 5)
    


}
Last edited on
You can make a separate function for over time hours, or place it inside the thePay function and keep track how many hours the user has worked overtime.

What's the point of the loop in main?

You don't need any parameters for any of the void functions if you plan to do everything inside the functions.

Inside your void functions you can cout the variables after the user input.

You main could look something like this:
1
2
3
4
5
6
7
8
9
int main(){
  
   theEmployee();
   theHoursWorked();
   thePay();
   theHoursWorked();
   
  return 0;
}
Thank you. I keep getting errors with the rest of my code though. Anything that you can help me with?
Can you post your updated code and the errors you're getting?
Here it is.

So i have gotten the code doing pretty much what i want however it wont calculate the overtimepay * 1.5. it only gives the normal pay.
And when i try to complete the other loops 5 loops with the other employees names it is jumping the "Enter the employees name and surname"

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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

void getData (string & theEmployee , float & theHoursWorked, float & thePayRate)
{
    cout<< "Enter the employees name and surname: "<< endl;
    getline(cin, theEmployee);

    cout << "Enter the numbers of hours the employee worked: " << endl;
    cin >> theHoursWorked;

    cout << "Enter the employees hourly pay rate?" << endl;
    cin >> thePayRate;

}

float calculatePay( string theEmployee, float theHoursWorked, float thePayRate)
{
float regularPay, thePay, overtimeHours;
if (theHoursWorked > 40)
    {
    regularPay = 40 * thePayRate;
    overtimeHours = theHoursWorked - 40;
    thePay = regularPay + (overtimeHours * 1.5);
    }
else
    thePay = theHoursWorked * thePayRate;
    return thePay;
}

void printPaySlip( string theEmployee, float theHoursWorked, float
thePayRate, float thePay)
{
float overtimeHours;
cout << "Pay slip for " << theEmployee <<endl;
cout << "Hours worked: "<< theHoursWorked << endl;
if (theHoursWorked > 40)
    overtimeHours = theHoursWorked - 40;
else
    overtimeHours = 0;
cout << "Overtime hours: "<< overtimeHours << endl;
cout << "Hourly pay rate: " << thePayRate << endl;
cout << "Pay: " << thePay << endl;
cout << endl;

}


int main()
{
string theEmployee;
float theHoursWorked;
float thePayRate;
int thePay;

for (int i = 0; i < 5; i++)
{
    getData(theEmployee, theHoursWorked, thePayRate);
    thePay = calculatePay (theEmployee, theHoursWorked, thePayRate);
    printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay);
}

return 0;
}

Last edited on
This is the output that i get. I need the overtime hours to be times by 1.5 and i need it to allow me to enter the other employee names. Not skip it. I know it has something to do with the getline but i cannot figure out how to put it

Enter the employees name and surname:
harry matsibe
Enter the numbers of hours the employee worked:
43.5
Enter the employees hourly pay rate?
125.35
Pay slip for harry matsibe
Hours worked: 43.5
Overtime hours: 3.5
Hourly pay rate: 125.35
Pay: 5019

Enter the employees name and surname:
Enter the numbers of hours you worked:
37.4
Enter the employees hourly pay rate?
126.76
Pay slip for
Hours worked: 37.4
Overtime hours: 0
Hourly pay rate: 126.76
Pay: 4740

Enter the employees name and surname:
Enter the numbers of hours you worked:

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
string theEmployee;
float theHoursWorked;
float thePayRate;
int thePay; //***************

for (int i = 0; i < 5; i++)
{
    getData(theEmployee, theHoursWorked, thePayRate);
    thePay = calculatePay (theEmployee, theHoursWorked, thePayRate);
    printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay);
}

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
Enter the employees name and surname:
mr mmm
Enter the numbers of hours you worked:
56.7
Enter the employees hourly pay rate?
10.45
Pay slip for mr mmm
Hours worked: 56.7
Overtime hours: 16.7
Hourly pay rate: 10.45
Pay: 443.05

Change int thePay to float thePay, ints don't do decimal places!

Have a look here for cin with spaces:-
http://stackoverflow.com/questions/5838711/stdcin-in-input-with-spaces
Last edited on
Ok but it still is not letting me enter the employees name for the payslips 2-5.
All sorted. Thank you to everyone who helped me!
Topic archived. No new replies allowed.