I have an array processing assignment based on C++ by which the records of each employee presented in a given text file should be calculated and printed as Hours worked, wage and other parameters.
find the assignment below: enter image description here enter image description here
my code is :
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
//process_paylor() function prototype
int process_payroll(ifstream& input, string name[], int hours[], float wage[]);
//process_employees() function prototype
void process_employees(string name[], int hours[], float wage[], float gross[], float
adjusted_gross[], int n);
//print_results() function prototype
void print_results(string name[], int hours[], float wage[], float gross[], float
adjusted_gross[], int n);
//max_wage() function prototype
int max_wage(float wage[], int n);
//min_wage() function prototype
int min_wage(float wage[], int n);
//average_payrate() function prototype
float average_payrate(float wage[], int n);
int main()
{
//maximum size of arrays
const int SIZE = 15;
//ifstream to read the input file
ifstream input;
//Open the input file
input.open("c:/users/mdhainy/desktop/inputlab2.txt");
if (input.fail())
{
cout << "Input file failed to open\n" << endl;
system("pause");
exit(1);
}
else
{
string name[SIZE];
float wage[SIZE], gross[SIZE], adjusted_gross[SIZE];
int hours[SIZE];
//call process_payroll function
int n = process_payroll(input, name, hours, wage);
//call process_employees function
process_employees(name, hours, wage, gross, adjusted_gross, n);
//call print_results function
print_results(name, hours, wage, gross, adjusted_gross, n);
//close the input file
input.close();
}
system("pause");
return 0;
}
//Define process_payroll() function
int process_payroll(ifstream& input, string name[], int hours[], float wage[])
{
int n = 0;
//while the file is not ended yet and number of employees <=10
while ((!input.eof()) && (n <= 10))
{
input >> name[n] >> wage[n] >> hours[n];
n++;
if (n > 10)
{
cout << "Error! More than 10 Names are read" << endl;
}
}
return n;
}
//Define process_employees() function
void process_employees(string name[], int hours[], float wage[], float gross[], float
adjusted_gross[], int n)
{
for (int i = 0; i < n; i++)
{
//calculate gross
gross[i] = hours[i] * wage[i];
adjusted_gross[i] = gross[i];
if (hours[i] > 45)
{
adjusted_gross[i] += 50;
}
else if (hours[i] < 30)
{
adjusted_gross[i] -= (hours[i] * 0.25);
}
}
}
//Define print_results() function
void print_results(string name[], int hours[], float wage[], float gross[], float
adjusted_gross[], int n)
{
//open an output file
ofstream output("results.txt");
//Set precision two decimal point
output << fixed << setprecision(2);
//Headings
output << "Number of Employees:" << n << endl;
output << endl;
output << "Maximum Pay Rate: Washington @ $" << wage[max_wage(wage, n)] << endl;
output << endl;
output << "Minimum Pay Rate: Kennedy @ $" << wage[min_wage(wage, n)] << endl;
output << endl;
output << "Average Pay: $" << average_payrate(wage, n) << endl;
output << endl;
output << "October 2009 Payroll:" << endl;
output << endl;
output << "Name" <<"\t\t\tHours" << "\t\tRate" << "\t\tGross" << "\t\tBonus"<< "\t\tAdjusted Gross" << endl;
output << endl;
//print employee informations
for (int i = 0; i < n; i++)
{
output << setw(15) << left << name[i] << "\t\t" << hours[i] << "\t\t" << wage[i] << "\t\t" << gross[i] << "\t\t"<< (adjusted_gross[i] > gross[i] ? "Y" : "N") << "\t\t" << adjusted_gross[i] << endl;
}
//close the output file
output.close();
}
//Define max_wage() function
int max_wage(float wage[], int n)
{
int max = 0;
for (int i = 1; i < n; i++)
{
if (wage[i] > wage[max])
max = i;
}
return max;
}
//Define min_wage() function
int min_wage(float wage[], int n)
{
int min = 0;
for (int i = 1; i < n; i++)
{
if (wage[i] < wage[min])
min = i;
}
return min;
}
//Define average_payrate() function
float average_payrate(float wage[], int n)
{
float total = 0;
for (int i = 0; i < n; i++)
{
total += wage[i];
}
return total / n;
}
comments on the exercise that I should modify:
all declarations of any variable need to be at beginning of a function.
while ((!input.eof()) && (n <= 10)) You need to process all records up to 15 and give me a warning error when I read > 10 names.
every function needs a return statement
minimum and average payrates are incorrect.
your program is printing garbage information as an additional record.
output not aligned with headings.enter code here
numbers must be right justified so all decimals are lined up