0

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:

  1. all declarations of any variable need to be at beginning of a function.

  2. 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.

  3. every function needs a return statement

  4. minimum and average payrates are incorrect.

  5. your program is printing garbage information as an additional record.

  6. output not aligned with headings.enter code here

  7. numbers must be right justified so all decimals are lined up

  • 1
    what is your specific question? – 463035818_is_not_an_ai Apr 29 '22 at 10:01
  • If you have problems, try to minimize the programs (into a [mre]) to try and isolate the problem. Is the problem with the file reading? With the output? With the processing? And have you tried to [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your program in any way? – Some programmer dude Apr 29 '22 at 10:04
  • Also: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Apr 29 '22 at 10:04
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Apr 29 '22 at 10:32
  • Can someone help convert the void functions into normal functions with return statement in the end – Kassemhashem1988 Apr 29 '22 at 12:04
  • A function is a function is a function... It doesn't matter if a function is declared to return `void` or an actual value, it's still a "normal" function. Sometimes it makes sense to return a value, sometimes not. You have two functions without a return value (`void` return value), why should they be modified to return a value? Why should they return a value? What value should they return? What is the actual problem you really try to solve by changing these functions? Please **[edit]** your question to give us all the details. – Some programmer dude Apr 29 '22 at 12:33
  • And please take the time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 29 '22 at 12:33
  • I also recommend you invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), learn about structures and classes and objects, and about [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Apr 29 '22 at 12:35

0 Answers0