Write a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".
Input for this project:
· the user must enter the number of employees in the company.
· the user must enter as integers for each employee:
o the employee number (ID)
Create a global variable of type ofstream for the output file. Use this variable to open the file employeeAbsences.txt in your program to write data to it.
Sample Output
Display “Enter the number of employees in the company”
Display “Enter the number of employees ID”
Display “Enter the number of days this employee was absent”
Display “Please re-enter the number of days absent”<o:p></o:p>
Display “Please enter the number of days this employee was absent”z
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int getCorrectValue(string,string,int);
void saveToFile(int);
ofstream employeeAbsencesFile;
int main()
{
//Opening the file "employeeAbsences.txt"
employeeAbsencesFile.open("employeeAbsences.txt");
int totalEmployees =getCorrectValue("Enter the number of employees in the company: ","Please re-enter the number of employees in this company: ",1);
saveToFile(totalEmployees);
system("pause");
return 0;
}
//Reads the integer value
int getCorrectValue(string message,string errorMessage,int min){
int value=-1;
while(value==-1){
cout<<message;
cin>>value;
if(cin.fail()){
cin.clear();
cin.ignore(1000,'\n');
message=errorMessage;
value=-1;
}else{
if(value<min){
message=errorMessage;
value=-1;
}
}
}
return value;
}
//Saves to the file "employeeAbsences.txt"
void saveToFile(int totalEmployees){
employeeAbsencesFile << "EMPLOYEE ABSENCE REPORT\n";
employeeAbsencesFile << setw(5) << "Employee ID" << setw(25) << "Days absent\n";
int totalAbsentDays = 0;
for (int i = 1; i <= totalEmployees; i++)
{
//read the employee ID
int employeeID = getCorrectValue("Please enter an employee ID: ","Please re-enter an employee ID: ",1);
//read the number of days this employee was absent
int daysAbsent = getCorrectValue("Please enter the number of days this employee was absent: ","Please re-enter the number of days absent: " ,0);
employeeAbsencesFile << setw(5) << employeeID << setw(25) << daysAbsent << endl;
totalAbsentDays += daysAbsent;
}
double averageAbsent = ((double)totalAbsentDays / (double)totalEmployees);
employeeAbsencesFile << "\nThe "<< totalEmployees<< " employees were absent a total of "<< totalAbsentDays<<" days.\n";
employeeAbsencesFile << "The average number of days absent is "<< showpoint << fixed << setprecision(1) << averageAbsent<<" days.\n";
employeeAbsencesFile.close();
}
Comments
Leave a comment