Source of wages.cpp


  1: //wages.cpp
  2: //Illustrates the use of an if...else-statement in a "practical" context.

  4: #include <iostream>
  5: #include <iomanip>
  6: using namespace std;

  8: int main()
  9: {
 10:     const int    STANDARD_HOURS  = 8;   /* number of hours per day */
 11:     const double WAGE_RATE       = 9.5; /* $ per hour              */
 12:     const double OT_RATE         = 1.5; /* "time-and-a-half"       */

 14:     cout.setf(ios::fixed, ios::floatfield);
 15:     cout.setf(ios::showpoint);
 16:     cout << setprecision(2);

 18:     cout << "\nThis program computes a worker's wages for a single day.\n"
 19:         "The wage rate is fixed at $" << WAGE_RATE << " per hour for a "
 20:         "regular work day.\nTime and a half is paid for overtime, which "
 21:         "is anything beyond " << STANDARD_HOURS << " hours.\nA worker "
 22:         "gets paid in full for the last hour, even if the hour is not "
 23:         "complete.\n\n";

 25:     double numHoursWorked;
 26:     double wages;
 27:     bool overtimeWorked;

 29:     cout << "Enter the number of hours worked: ";
 30:     cin >> numHoursWorked;  cin.ignore(80, '\n');
 31:     cout << endl;

 33:     if (numHoursWorked > (int)numHoursWorked)
 34:         numHoursWorked = (int)numHoursWorked + 1;

 36:     overtimeWorked = (numHoursWorked > STANDARD_HOURS);
 37:     if (overtimeWorked)
 38:         wages = (STANDARD_HOURS * WAGE_RATE) +
 39:                 (numHoursWorked - STANDARD_HOURS) * WAGE_RATE * OT_RATE;
 40:     else
 41:         wages = WAGE_RATE * numHoursWorked;

 43:     cout << "Total wages for " << numHoursWorked
 44:          << " hours at this rate: $"  << wages << endl;
 45:     cout << endl;
 46: }