In: Computer Science
Spring 2019 CMSC 140 Programming Project 4: Days Out
Project Specifications
Input for this project:
Input Validation:
Output: The program should display the following data:
// C++ program to take inputs of number of days the employees of a company was absent and write these information as well as average number of days absent to the file
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// global variable
ofstream fout;
// function declaration
int NumOfEmployees();
int TotDaysAbsent(int numEmployees);
double AverageAbsent(int numEmployees, int absentDays);
int main() {
fout.open("employeeAbsences.txt");
int numEmployees, absentDays;
numEmployees = NumOfEmployees();
fout<<left<<setw(15)<<"Employee ID"<<left<<setw(10)<<"Days Absent"<<endl;
absentDays = TotDaysAbsent(numEmployees);
double avgAbsent = AverageAbsent(numEmployees, absentDays);
fout<<"Average Number of days a company's employees are absent : "<<fixed<<setprecision(2)<<avgAbsent;
fout.close();
return 0;
}
// function to get and return the number of employees in a company
int NumOfEmployees()
{
int num;
cout<<"Enter the number of employees :";
cin>>num;
while(num < 1)
{
cout<<"Number of employees must be at least 1"<<endl;
cout<<"Enter the number of employees :";
cin>>num;
}
return num;
}
// function get and return the total number of days the employees of a company was absent
int TotDaysAbsent(int numEmployees)
{
int daysAbsent, totalDaysAbsent = 0, empId;
for(int i=0;i<numEmployees;i++)
{
cout<<"Employee "<<(i+1)<<" : "<<endl;
cout<<"Enter the employee id : ";
cin>>empId;
cout<<"Enter the number of days that employee missed during the past year : ";
cin>>daysAbsent;
while(daysAbsent < 0)
{
cout<<"Number of days absent cannot be negative"<<endl;
cout<<"Enter the number of days that employee missed during the past year : ";
cin>>daysAbsent;
}
fout<<left<<setw(15)<<empId<<left<<setw(10)<<daysAbsent<<endl;
totalDaysAbsent += daysAbsent;
}
return totalDaysAbsent;
}
// function to calculate and return the average number of days the employees of a company was absent
double AverageAbsent(int numEmployees, int absentDays)
{
return(((double)absentDays)/numEmployees);
}
//end of program
Output:
Console:
File: