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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/***************************************************************/
/* This program will calculate the employee payroll */
/* with the data from the 'Data.txt' file and display */
/* it on the screen. It will also calculate the number */
/* of employees in the file, max & min wage as well */
/* as average pay. It will display the bonus if legible. */
/***************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void Report_Data(ifstream& myReadFile, ofstream& myWriteFile);
void Read_Data(ifstream& myReadFile, ofstream& myWriteFile); //Function Prototype
void Print_Summary ();
/***************************************************************/
/* The 'Main' Function will open the reading and writing */
/* file and check for open errors. It will then call the */
/* functions and end the program once the functions are */
/* complete. */
/***************************************************************/
int main()
{
string Data_File = "Data.txt";
string Payroll_File = "Employee Payroll.txt";
ifstream myReadFile; // Local Declarations.
ofstream myWriteFile;
myReadFile.open(Data_File.c_str()); // Open 'Data.txt' for reading.
if(myReadFile.fail())
{
cout << "The file was not successfully opened." << endl;
exit(1);
}
myWriteFile.open(Payroll_File.c_str()); // Open 'Employee Payroll.txt' for writing.
if(myWriteFile.fail())
{
cout << "The file was not successfully opened." << endl;
exit(1);
}
Report_Data(myReadFile, myWriteFile); // Function calls.
Read_Data(myReadFile, myWriteFile);
Print_Summary();
getchar();
return 0;
} // End Program.
/***************************************************************/
/* The 'Report_Data' function will read data from the */
/* 'Data.txt' file and calculate the number of employees */
/* max and min wage, average wage, and bonus. */
/* Then write all the information to the file */
/* 'Emplyoee Payroll.txt'. */
/***************************************************************/
void Report_Data(ifstream& myReadFile, ofstream& myWriteFile)
{
string employee_name[1];
double hourly_wage[1]; // Local Declarations
double hours_worked[1];
int NumOfEmployees = 0;
double Max_Pay = 0, Min_Pay = 0, Total_Pay = 0, Average_Pay;
while(!myReadFile.eof()) // Read data from 'Data.txt'.
{
NumOfEmployees++; // Calculate number of employees in file.
myReadFile >> employee_name[1] >> hourly_wage[1] >> hours_worked[1];
if(Max_Pay < hourly_wage[1]) // Calculate Maximum wage.
Max_Pay = hourly_wage[1];
if(Min_Pay == 0) // Calculate Minimum wage.
Min_Pay = hourly_wage[1];
else
{
if(Min_Pay > hourly_wage[1])
{
Min_Pay = hourly_wage[1];
}
}
Total_Pay = Total_Pay + hourly_wage[1];
}
Average_Pay = Total_Pay / NumOfEmployees; // Calculate Average wage.
// Write all calculated data to 'Employee Payroll.txt'.
myWriteFile << "Number of Employees: " << NumOfEmployees << endl
<< "Maximum Pay Rate: " << Max_Pay << endl
<< "Minimum Pay Rate: " << Min_Pay << endl
<< "Average Pay Rate: " << Average_Pay << endl;
return;
}
/********************************************************/
/* 'Read_Data' Function will open 'Data.txt' file */
/* and once again read all the data. It will then */
/* create a table with all information including */
/* bonus pay. */
/********************************************************/
void Read_Data(ifstream& myReadFile, ofstream& myWriteFile)
{
string employee_name[1];
double hourly_wage[1]; // Local Declarations.
double hours_worked[1];
double gross, adjusted_gross;
myWriteFile << "_____________________" << endl
<< "November 2012 Payroll" << endl
<< "_____________________" << endl
<< " Name Hours Rate Gross Bonus Adjusted-Gross" << endl
<< " ---- ----- ---- ----- ----- --------------" << endl;
myReadFile.seekg(0); // Start at the beginning of the file.
myReadFile.clear();
while(!myReadFile.eof())
{
myReadFile >> employee_name[1] >> hourly_wage[1] >> hours_worked[1]; // Read data from file.
gross = hourly_wage[1] * hours_worked[1];
myWriteFile << setw(10) << employee_name // Write all data for each employee to file line by line.
<< setw(9) << setprecision(0) << hours_worked;
myWriteFile << setw(9) << setiosflags(ios::fixed) << setprecision(2) << hourly_wage
<< setw(11) << gross;
if(hours_worked[1] > 45) // Calculate bonus.
{
adjusted_gross = gross + 50.00;
myWriteFile << setw(11) << "Yes"
<< setw(17) << adjusted_gross
<< endl;
}
else if(hours_worked[1] <= 45 && hours_worked[1] >= 30)
{
adjusted_gross = gross;
myWriteFile << setw(11) << "No"
<< setw(17) << adjusted_gross
<< endl;
}
else if(hours_worked[1] < 30)
{
adjusted_gross = gross - (hours_worked[1] * 0.25);
myWriteFile << setw(11) << "No"
<< setw(17) << adjusted_gross
<< endl;
}
}
return;
}
/*******************************************************************/
/* 'Pring_Summary' function will open 'Employee Payroll.txt' */
/* file and output all the data in the file to the output */
/* Screen. */
/*******************************************************************/
void Print_Summary ()
{
ifstream myReadFile;
string Payroll_File = "Employee Payroll.txt";
string textRead;
int line = 0;
myReadFile.open(Payroll_File.c_str()); // Open 'Employee Payroll.txt' for reading.
if(myReadFile.fail())
{
cout << "The file was not successfully opened." << endl;
exit(1);
}
while(line != 1)
{
getline(myReadFile, textRead);
cout << textRead << endl; // Display line by line to output screen.
if(textRead == "")
line++;
}
return;
}
|