Friday, January 6, 2017

Programming Challenge 6.13 - Days Out

/* Days Out - This program calculates the average number of days a
   company's employees are absent. This program has the following
   functions:
 
   * getNumEmployees()
   * getDaysAbsent()
   * calcAbsence()

   Input Validation: No number less than 1 for the number of
   employees is accepted. No negative numbers for the days any
   employee missed are accepted. */

/* Prototypes: Get number of employees, Number of employees,
   calculateAbsence */
int getNumEmployees();
int getDaysAbsent(int);
double calcAbsence(double, double);

#include "Utility.h"

int main()
{
   /* Variable: Average days absent */
   double absenceAvgTotal = 0.0;

   /* Variables: Number of employees, Days of absence */
   int numEmployees = 0,
       daysOfAbsence = 0;

   /* Display: Introduction */
   cout << "\t\tBurlington Inc. Employee Absence Database\n\n"
        << "Please enter the necessary data to get a detailed report\n"
        << "of our company employee's mean absence this year.\n\n";

   /* Call: getNumEmployees */
   numEmployees = getNumEmployees();

   /* Call: getDaysAbsent */
   daysOfAbsence = getDaysAbsent(numEmployees);

   /* Call: calcAbsence */
   absenceAvgTotal = calcAbsence(daysOfAbsence, numEmployees);

   /* Set Up: Formatted numeric output */
   cout << fixed << showpoint << setprecision(2);

   /* Display: Absence report */
   cout << "\nBurlington Inc. Annual Mean Absence Report\n\n"
        << "Our " << numEmployees << " employees "
        << "were absent a total of " << daysOfAbsence << " days "
        << "this year.\n"
        << "The average absence of all employees was " << absenceAvgTotal
        << " days.\n";

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getNumEmployees

   This function asks the user for the number of employees in
   the company. This value is returned as an int.
   ********************************************************** */

int getNumEmployees()
{
   /* Variable: Number of employees */
   int numEmployees = 0;

   /* Get: The number of employees */
   cout << "Please enter the total number of employees: ";
   cin >> numEmployees;

   /* Catch: Infinite loop */
   cin.clear();
   cin.ignore();

   /* Input Validation: If the user enters a number below 1
      for numEmployees, a message is displayed, and the user
      is asked to repeat his or her input. */
   while (numEmployees <= 0)
   {
      cout << "Input Failure: You entered a number lower than\n"
           << "1 which is not valid.\n"
           << "Please enter the total number of employees: ";
      cin >> numEmployees;

      /* Catch: Infinite loop */
      cin.clear();
      cin.ignore();
   }

   /* Return: Number of employees */
   return numEmployees;
}

/* **********************************************************
   Definition: getDaysAbsent

   This function asks the user to enter the number of days
   each employee missed during the past year. The total of
   these days is returned as an int.
   ********************************************************** */

int getDaysAbsent(int daysAbsent)
{
   /* Variables: Absence counter, Employee absence,
                 Absent days total */
   int absenceCount = 1,
       employeeAbsence = 0,
       absentDaysTotal = 0;

   /* Get the number of days each employee was absent */
   for (absenceCount; absenceCount <= daysAbsent;
        absenceCount++)
   {
      /* Get: Number of days every employee was absent */
      cout << "\nHow many days has employee " << absenceCount
           << " been absent? ";    
      cin >> employeeAbsence;

      /* Catch: Infinite loop */
      cin.clear();
      cin.ignore();

      /* Input Validation: If employeeAbsence gets a negative
         number, a message is displayed and the user is asked
         to repeat his or her input. */
      while (employeeAbsence <= 0)
      {
         cout << "\nYou entered negative number which is not valid.\n"
              << "How many days has employee " << absenceCount
              << " been absent? ";
         cin >> employeeAbsence;

         /* Catch: Infinite loop */
         cin.clear();
         cin.ignore();
      }

      /* Accumulate the absent days total */
      absentDaysTotal += employeeAbsence;
   }

   /* Return: The total days of absences */
   return absentDaysTotal;
}

/* **********************************************************
   Definition: calculateAbsence

   This function accepts two arguments, employee number and
   days they were absent as arguments. It calculates the
   average number of days absent, and returns the result as
   a double.
   ********************************************************** */

double calcAbsence(double daysAbsent, double numEmployees)
{
   /* Variable: Average absence total */
   double avgAbsenceTotal = 0.0;

   /* Calculate: The mean of absences among all employees */
   avgAbsenceTotal = (daysAbsent / numEmployees);

   /* Return: The average total of absences */
   return avgAbsenceTotal;
}

No comments:

Post a Comment