Greetings...I'm trying to add a part for 2 times the rate for any hours over the first 60...Can anyone aid me? Many thanks

// This program calculates gross pay.
#include <iostream>
#include <iomanip>
 
// Global constants
const double PAY_RATE = 10;    // Hourly pay rate
const double BASE_HOURS = 40.0;   // Max non-overtime hours
const double OT_MULTIPLIER = 1.5; // Overtime multiplier
// Function prototypes
double getBasePay(double);
double getOvertimePay(double);
int main()
{
   double hours,           // Hours worked
          basePay,         // Base pay
          overtime = 0.0,  // Overtime pay
          totalPay;        // Total pay
   // Get the number of hours worked.
   cout << "How many hours did you work? ";
   cin >> hours;
   // Get the amount of base pay.
   basePay = getBasePay(hours);
   // Get overtime pay, if any.
   if (hours > BASE_HOURS)
      overtime = getOvertimePay(hours);
   // Calculate the total pay.
   totalPay = basePay + overtime;
   // Set up numeric output formatting.
   cout << setprecision(2) << setiosflags(ios::fixed);
   // Display the pay.
   cout << "Base pay: $" << basePay << endl;
   cout << "Overtime pay $" << overtime << endl;
   cout << "Total pay $" << totalPay << endl;
   return 0;
}
//************************************************
// The getBasePay function accepts the number of *
// hours worked as an argument and returns the   *
// employee's pay for non-overtime hours.        *
//************************************************
double getBasePay(double hoursWorked)
{
   double basePay; // To hold base pay
   // Determine base pay.
   if (hoursWorked > BASE_HOURS)
      basePay = BASE_HOURS * PAY_RATE;
   else
      basePay = hoursWorked * PAY_RATE;
   return basePay;
}
//*************************************************
// The getOvertimePay function accepts the number *
// of hours worked as an argument and returns the *
// employee's overtime pay.                       *
//*************************************************
double getOvertimePay(double hoursWorked)
{
   double overtimePay; // To hold overtime pay
   // Determine overtime pay.
   if (hoursWorked > BASE_HOURS)
   {
      overtimePay = (hoursWorked - BASE_HOURS) *
               PAY_RATE * OT_MULTIPLIER;
   }
   else
      overtimePay = 0.0;
   return overtimePay;
}

you will have to add a little code to getOvertimePay() to make that check. First, check for over 60 hours. If its over 60 then calculate 2.0 rate. If not over 60 then do what the function does now -- you don't need to change that part. Something like this:

Is hoursWorked > 60
{
    overtimePay = XXX
}
Otherwise is hoursWorked > 40
{
    overtimePay = XXX
}
else
{

}

I'm trying to turn this part into a function into my code

grosspay = (hours * rate);
      if (hours > 40)
      grosspay = ((hours - 40) * rate * 1.5) + (40 * rate);
      if (hours > 60)
      grosspay = ((hours - 60) * rate * 2) + (20 * rate * 1.5) +(40 * rate);

does anyone have a sugestion? whenever I do it I get errors. please help

heres the code

#include <iostream>
#include <iomanip>
/* Function Prototype */
/* Main Program */
int main(void)
{
 int empNum, sumh, sump;
   double hours, rate, grosspay;
   cout << setprecision(2) << setiosflags(ios::fixed);
   cout << "Please enter the employee number (0 to stop): ";
   cin >> empNum;
   while (empNum != 0)
   {
    cout << "Hours: ";
      cin >> hours;
      cout << "Rate: ";
      cin >> rate;
      grosspay = (hours * rate);
      if (hours > 40)
      grosspay = ((hours - 40) * rate * 1.5) + (40 * rate);
      if (hours > 60)
      grosspay = ((hours - 60) * rate * 2) + (20 * rate * 1.5) +(40 * rate);

      cout << "\nGross Pay is " << grosspay;
      cout << "\n\nPlease enter the employee number (0 to stop): ";
    cin >> empNum;
   }
             cout << "Please enter the first employee's hours: ";
             cin >> hours;
             cout << "Please enter the second employee's hours: ";
             cin >> hours;
             cout << "Please enter the third employee's hours: ";
             cin >> hours;
             cout << "Please enter the fourth employee's hours: ";
             cin >> hours;
             cout << "Please enter the fifth employee's hours: ";
             cin >> hours;
             cout << "Please enter the sixth employee's hours: ";
             cin >> hours;
             sumh = hours + hours + hours + hours + hours + hours;
   cout << "\nThe total hours worked by all employees is " << sumh << endl;

             cout << "Please enter the first employee's grosspay: ";
             cin >> grosspay;
             cout << "Please enter the second employee's grosspay: ";
             cin >> grosspay;
             cout << "Please enter the third employee's grosspay: ";
             cin >> grosspay;
             cout << "Please enter the fourth employee's grosspay: ";
             cin >> grosspay;
             cout << "Please enter the fifth employee's grosspay: ";
             cin >> grosspay;
             cout << "Please enter the sixth employee's grosspay: ";
             cin >> grosspay;
   sump = grosspay + grosspay + grosspay + grosspay + grosspay + grosspay;
   cout << "\nThe total gross pay earned by employees is " << sump << endl;
   return (0);
}
/* Function: GrossPay
 * Usage: grosspay = GrossPay(hours, rate);
 * ----------------------------------------
 * This function calculates the gross pay for an employee for the
 * given number of hours worked (hours) and the rate of pay (rate).
 * An employee is paid at the regular rate for the first 40 hours worked,
 * at 1.5 times the rate for any hours over the first 40, and
 * at 2 times the rate for any hours over the first 60.
 *
 *   e.g.  62 hours at $10 per hour,
 *         gives $400 for the first 40 hours,
 *           $300 for the next  20 hours, and
 *         $ 40 for the final  2 hours,
 *                   ----               --
 *       thus  $740 for the total 62 hours for a gross pay.
 */

I'm trying to turn this part into a function into my code

But where is the function in your code ... I dont see it.

Post the updated code.

I'm trying to turn this part into a function into my code

grosspay = (hours * rate);
if (hours > 40)
      grosspay = ((hours - 40) * rate * 1.5) + (40 * rate);
if (hours > 60)
      grosspay = ((hours - 60) * rate * 2) + (20 * rate * 1.5) +(40 * rate);
does anyone have a sugestion? whenever I do it I get errors. please help

1) Give the function a name that indicates it's purpose, like calculateGrossPay().
2)Decide what information the function needs to do it's job. In this case, it needs hours and rate.
3)If function is going to change of the information sent it then arrange to have the information sent by reference using a reference or pointer to data back in the calling function. If the data being sent is a big object with lots of information in it, send it by reference as well. If the information is a plain old data type that doesn't get changed then send it by value.
4)Decide if you need any new information back from the function. If not, give it a return type of void. If you do need something back, other than changed values to information sent in, then determine the type. Remember, don't return pointers to information local to the function you are writing!
5)Decide if you are going to declare the function before main() and define it after main() or declare and define it before main().
6)Create the body of the funciton writing code to have the function do what you want to the information sent and returning the information desired.

In this case the function prototype could be something like this:
double calculateGrossPay(const double hours, const double rate);

or

void calculateGrossPay(const double hours, const double rate, double * grossPay);

I'll leave it to you to fill in the body of the function and decide what code you can add/remove from main() after your write the function. Repost if you have questions.

grosspay = (hours * rate);
if (hours > 40)
      grosspay = ((hours - 40) * rate * 1.5) + (40 * rate);
if (hours > 60)
      grosspay = ((hours - 60) * rate * 2) + (20 * rate * 1.5) +(40 * rate);

The if condition statements are backwards. First check for over 60 else if not over 60 check for over 40. The way you have it, over 60 will get both calculations.

This article has been dead for over six months. Start a new discussion instead.