1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream> //For user input and output
#include <conio.h> // Allows the getch() function
#include <string> //For strings
#include <fstream> //For file reading and writing
#include <iomanip> //Allows input and output formatting
using namespace std;
//Function Prototypes
void Initialize(int, double, double);
void ReadData(ifstream, string&, double&, double&);
void WklySalaryCalc(double, double, double, double&);
void AccumulateEmployeeInfo(int, double&, double, double&, double);
void AveragePayAndHours(double&, double, int, double&, double);
void PrintResults(ofstream, string, double, double, double);
void PrintEmployeeInfo(ofstream, int, double, double);
//Declaration of Constants
const double OT = 1.5; //Constant for overtime pay
int main()
{
//Declaration of Variables
ifstream inData; //Variable for reading data from a file
ofstream outData; //Variable for writing data to a file
string employeeName; //Variable for employee name
double hrsWorked; //Variable for hours worked
double hrlyWage; //Variable for hourly wage
double wklySalary; //Variable for weekly salary
int numEmployees; //Variable for total number of employees
double totalHrs; //Variable for total hours worked by employees
double totalWklySalaries; //Variable for total weekly salary of all employees
double avgHrs; //Variable for average hours worked by all employees
double avgSalary; //Variable for average salary made by all employees
//Input Section
inData.open("C:\\data.txt");
outData.open("C:\\results.txt");
outData << fixed << showpoint;
//Processing Section
Initialize(numEmployees, totalHrs, totalWklySalaries);
ReadData(inData, employeeName, hrsWorked, hrlyWage);
if (!inData)
{
cout << "File not found" << endl;
getch();
}
else
{
while (inData)
{
WklySalaryCalc(OT, hrsWorked, hrlyWage, wklySalary);
PrintResults(outData, employeeName, hrsWorked, hrlyWage, wklySalary);
AccumulateEmployeeInfo(numEmployees, totalHrs, hrsWorked, totalWklySalaries, wklySalary);
ReadData(inData, employeeName, hrsWorked, hrlyWage);
}
}
AveragePayAndHours(avgHrs, totalHrs, numEmployees, avgSalary, totalWklySalaries);
PrintEmployeeInfo(outData, numEmployees, avgHrs, avgSalary);
//Output Section
inData.close();
outData.close();
getch();
return 0;
}
//Functions
void Initialize(int numEmployees, double totalHrs, double totalWklySalaries)
//Preconditions: none
//Postconditions: numEmployees, totalHrs, and totalWklySalaries are defined
{
numEmployees = 0;
totalHrs = 0;
totalWklySalaries = 0;
}
void ReadData(ifstream inData, string& employeeName, double& hrsWorked, double& hrlyWage)
//Preconditions: the file is open to read from
//Postconditions: employeeName, hoursWorked, and hourlyWage will be defined based on file
{
while(inData)
{
inData >> employeeName;
inData >> hrsWorked;
inData >> hrlyWage;
}
}
void WklySalaryCalc(double OT, double hrsWorked, double hrlyWage, double& wklySalary)
//Preconditions: OT, hrsWorked, and hrlyWage all must be defined
//Postconditions: wklySalary is calculated, depending on hrsWorked
{
if(hrsWorked > 40)
wklySalary = (((hrsWorked - 40)*OT) + hrsWorked)*hrlyWage;
else
wklySalary = hrsWorked * hrlyWage;
}
void AveragePayAndHours(double& avgHrs, double totalHrs, int numEmployees, double& avgSalary, double totalWklySalaries)
//Preconditions: totalHrs, numEmployees, and totalWklySalaries all must be defined
//Postconditions: avgHrs and avgSalary are calculated and defined
{
avgHrs = totalHrs/numEmployees;
avgSalary = totalWklySalaries/numEmployees;
}
void AccumulateEmployeeInfo(int numEmployees, double& totalHrs, double hrsWorked, double& totalWklySalaries, double wklySalary)
//Preconditions: numEmployees, totalHrs, hrsWorked, totalWklySalaries, and wklySalary must be defined.
//Postconditions: numEmpyees is incremented
//totalHrs is calculated
//totalWklySalaries is calculated
{
numEmployees++;
totalHrs = totalHrs + hrsWorked;
totalWklySalaries = totalWklySalaries + wklySalary;
}
void PrintResults(ofstream &outData, string employeeName, double hrsWorked, double hrlyWage, double wklySalary)
//Preconditions: the file is ready to be written to
//Postconditions: data is written to the file
{
outData << "Employee Name" << "Hours Worked" << "Hourly Wage" << "Weekly Salary" << endl;
outData << employeeName << setprecision(2) << hrsWorked << hrlyWage << wklySalary << endl;
}
void PrintEmployeeInfo(ofstream &outData, int numEmployees, double avgHrs, double avgSalary)
//Preconditions: the file is ready to be written to
//Postconditions: data is written to the file
{
outData << "Total Number of Employees" << "Average Hours" << "Average Weekly Salary" << endl;
outData << numEmployees << setprecision(2) << avgHrs << avgSalary << endl;
}
|