Sunday, February 5, 2017

Programming Challenge 7.9 - Payroll

/* Payroll - This program uses the following arrays:

   * empId: An array of seven long integers to hold the employee
            identification numbers. The array is initialized with
            the following numbers:

            5658845     4520125  7895122  8777541
            8451277     1302850  7580489

   * hours: An array of seven integers to hold the number of hours
            worked by each employee

   * payRate: An array of seven doubles to hold each employee's
              hourly pay rate

   * wages: An array of seven doubles to hold each employee's gross
            wages

   The program relates the data in each array through the subsdispRepipts.
   For example, the number in element 0 of the hours array is the
   number of hours worked by the employee whosee identification number
   is stored in element 0 of the empId array. That same employee's pay
   rate is stored in element 0 of the payRate array.

   The program displays each employee number and asks the user to enter
   that employee's hours and pay rate. It then calculates the gross
   wages for that employee (hours * payRate) and stores them in the
   wages array.

   After the data has been entered for all the employees, the program
   displays each employee's identification number and gross wages.

   Input Validation: No negative values for hours or numbers less than
   15.00 for pay rate are accepted. */

#include "Utility.h"

/* Function prototypes: Get data */
void getData();

int main()
{
   /* Call: getData */
   getData();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getData

   This function asks for the following items:

      * The Numbers of hours an employee has worked
      * The Gross pay

   The data is stored in the following arrays:

      * hours[]
      * payRate[]
  
   This data is used to calculate and store the gross pay for
   each employee in grossWage[]. Once the data entry process
   is finished, a final report is displayed.
   ********************************************************** */

void getData()
{
   /* Constant: Number of Employees */
   const int NUM_EMPLOYEES = 7;

   const long int empId[NUM_EMPLOYEES] = { 5658845, 4520125, 7895122,
                                              8777541, 8451277, 1302850,
                                           7580489 };

   /* Array variables: Hours, Pay rate, Gross wage */
   int hours[NUM_EMPLOYEES] = { 0 };
   double payRate[NUM_EMPLOYEES] = { 0.0 },
          grossWage[NUM_EMPLOYEES] = { 0.0 };

   /* Variable: Display report */
   int dispRep = 0;

   /* Display: Header */
   cout << "\t\tNAKAMA CORP. DATABASE - WEEKLY EARNINGS REPORT SYSTEM\n\n"
        << "(Enter the hours worked for each employee and their\n"
        << "pay rates to calculate the employee's gross wages.)\n\n";
  
   /* Get the data for each employee and validate the input */
   for (int data = 0; data < NUM_EMPLOYEES; data += 1)
   {
      cout << "Employee # " << empId[data] << "\tHours worked: ";
      cin >> hours[data];

      /* Validate: Input */
      while (hours[data] <= 0)
      {
         cout << "\nInput Failure: The number of hours entered is invalid. "
              << "Please repeat your input.\n\n"
              << "Employee # " << empId[data] << "\tHours worked: ";
         cin >> hours[data];
      }

      cout << "Employee # " << empId[data] << "\tPayRate: "
           << setw(5) << left << "$ ";
      cin >> payRate[data];
      cout << endl;

      /* Validate: Input */
      while (payRate[data] < 15)
      {
         cout << "Input Failure: The gross pay amount entered was invalid.\n"
              << "It has to be $15.00 or above. Please repeat your input.\n\n"
              << "Employee # " << empId[data] << "\tPayRate: $ ";
         cin >> payRate[data];
      }

      /* Calculate: The gross wage */
      grossWage[data] = hours[data] * payRate[data];
   }

   /* Set up: Numeric output formatting */
   cout << showpoint << fixed << setprecision(2);

   /* Display the earnings of each employee */
   cout << "\n\t\tNAKAMA CORP. DATABASE - EARNINGS REPORT SUMMARY\n\n";
   cout << "Employee ID:\t\t" << "Hours Worked:\t\t" << "Pay Rate:\t\t"
        << "Gross Wage:\n"
        << "-----------\t\t------------\t\t--------\t\t----------\n";

   for (dispRep = 0; dispRep < NUM_EMPLOYEES; dispRep++)
      cout << empId[dispRep] << setw(23) << right << hours[dispRep] << " hours\t"
           << setw(11) << right << "$ " << setw(5) << right <<  payRate[dispRep]
           << setw(19) << right << "$ " << setw(7) << right << grossWage[dispRep]
           << setw(6) << right << "\n";
   cout << endl;
}

Example Output: 





No comments:

Post a Comment