calling a function

We started to call functions this week and I'm having a bit of an issue. Hours worked over 37 pays time and a half and hours worked 50 and over is double time. Can anyone steer me in the right direction? New to the site and programming TIA!

#include <iostream>
using namespace std;

//getOVertime function prototype
double getOvertime(double hours, double rate);


//declare variables
double hoursWorked = 0.0;
double payRate = 0.0;
double overtimePay = 0.0;
double grossPay = 0.0;


int main()
{

while(true)
{
cout << "Enter hours worked (-1 to stop): ";
cin >> hoursWorked;
//leave loop if -1
if (hoursWorked == -1) { break; }


cout << "Enter pay rate: ";
cin >> payRate;



if (hoursWorked <= 37)
{
grossPay = hoursWorked * payRate;
cout << "Gross pay (-1 to stop): $" << grossPay << endl;
}//end if

else
{
overtimePay = getOvertime(hoursWorked, payRate);
grossPay = overtimePay + grossPay;
cout << "Gross pay (-1 to stop): $" << grossPay << endl;
}//end if


}
system("pause");
return 0;
}//end main

//getOvertime function definition
double getOvertime(double hours, double rate)
{
double extraPay = 0.0;

extraPay = (hours - 37) * rate * 1.5;
if (hours > 50)
extraPay += (hours - 50) * rate * 2;
//end if
return extraPay;
} //end of getOvertime function
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
#include <iostream>

double overtime( double hours, double rate ) ;

int main()
{
    double hours_worked ;
    while( std::cout << "hours worked (-1 to stop)? " && std::cin >> hours_worked && hours_worked >= 0 )
    {
        double pay_rate ;
        std::cout << "pay rate? " ;
        std::cin >> pay_rate ;

        if( pay_rate >= 0 )
        {
            std::cout << overtime( hours_worked, pay_rate ) << '\n' ;
            const double gross_pay = hours_worked * pay_rate + overtime( hours_worked, pay_rate ) ;
            std::cout << "gross pay: " << gross_pay << '\n' ;
        }
    }
}

double overtime( double hours, double rate )
{
    static const double no_overtime = 37.0 ; // hours under 37 get no overtime
    if( hours <= no_overtime ) return 0.0 ; // no overtime

    static const double half_overtime = 50.0 ; // hours 37 to 50 get half overtime
    if( hours <= half_overtime ) return (hours-no_overtime) * rate * 0.5 ; // half overtime for hours above 37

    // hours 37 to 50 get half overtime, hours above 50 get full overtime
    return (half_overtime-no_overtime) * rate * 0.5 + (hours-half_overtime) * rate ;
}
Topic archived. No new replies allowed.