Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this code from C++ and these are the instructions Write a program that does the following: Reads 5 employee identification numbers from a

I have this code from C++ and these are the instructions

Write a program that does the following:

Reads 5 employee identification numbers from a file.

Asks the user to enter the number of hours worked during the week and pay rate of each employee.

Calculates the gross salary of each employee.

Writes to a file the employee identification number and gross salary of each employee.

The program MUST use the following parallel arrays:

an array to store 5 employee identification numbers. This array must be filled with the employee identification numbers stored in the file employee_ids.txt. (Note: Download the file from the link at the top by right clicking.)

an array to store the number of hours worked during the week by each employee. This array will be filled with input provided by the user.

an array to store the hourly pay rate of each employee. This array will be filled with input provided by the user.

an array to store the gross salary of each employee. The gross salary of an employee is computed by multiplying the employee's hourly pay rate times the number of hours worked.

The program MUST have the following functions (You are allowed to use additional functions):

A function that opens the file employee_ids.txt containing the employee identification numbers. This function only opens the file for input. It doesn't read the contents of the file. The user must be asked to enter the location and name of the file. If the file specified can't be found, a loop must be used to continue to prompt the user for a valid file location and name.

A function that opens the output file for writing. This function only opens the file for output. It doesn't write the results to the file. The user must be asked to enter the location and name of the file. If the file specified is not valid, a loop must be used to continue to prompt the user for a valid file location and name.

A function which reads the employee identification numbers from the input file and stores them in the appropriate array.

A function that asks the user to enter the number of hours worked during the week and pay rate of each employee, and stores the information in the corresponding arrays. The employee identification number of the employee MUST be displayed before asking the user for the employee's information. The number of hours worked must be zero or greater, and the pay rate must be $7.50 or greater. If the input provided is not valid, a loop must be used to continue to prompt the user for valid data.

A function that calculates the gross salary of each employee and stores it in the corresponding array.

A function which writes to the output file the employee indentification number and gross salary of each employee. The output should be formatted nicely.

Here is sample program run:

Enter the hours worked and pay rate for the employee with id A12345: Hours: 20 [Enter] Pay rate: $10.50 [Enter] Enter the hours worked and pay rate for the employee with id B55874: Hours: 25 [Enter] Pay rate: $15.00 [Enter] Enter the hours worked and pay rate for the employee with id C69851: Hours: 30.75 [Enter] Pay rate: $20.25 [Enter] Enter the hours worked and pay rate for the employee with id D12458: Hours: 35 [Enter] Pay rate: $22.00 [Enter] Enter the hours worked and pay rate for the employee with id E52242: Hours: 40 [Enter] Pay rate: $25.50 [Enter]

Output written to file:

Employee ID Gross Salary
A12345 $210.00
B55874 $375.00
C69851 $622.69
D12458 $770.00
E52242 $1020.00

GLOBAL VARIABLES THAT ARE NOT CONSTANTS ARE PROHIBITED! 50 POINTS WILL BE DEDUCTED FOR EACH NON-CONSTANT GLOBAL VARIABLE!

Functions that will only read the contents of an array should NOT be allowed to modify its contents!

Loops must be used to process the arrays and files!

The output must be aligned and nicely formatted!

With the code that I have until now I am not sure I have all the requirements, so I appreciate if somebody could check it for me please, especially the global variable #define SIZE 5 that I am not sure it correct, so any alternative is welcome. The output that I got now don't have the correct format like the example. it is like this:

Employee ID Gross Salary M5555 $30.00 Employee ID Gross Salary N6666 $793.00 Employee ID Gross Salary P7777 $180.00 Employee ID Gross Salary Q8888 $20750.00 Employee ID Gross Salary R9999 $144.00

Thanks

#include

#include

#include

#include

using namespace std;

#define SIZE 5

// Prototypes

void getInputFile(ifstream &inFile);

void getOutputFile(ofstream &outFile);

void readdata(ifstream &inFile, string data[]);

int main(){

ifstream inFile;

ofstream outFile;

string id[SIZE];

double hours[SIZE];

double payrate[SIZE];

double gSalary;

getInputFile(inFile);

getOutputFile(outFile);

readdata(inFile,id);

const int COL1_W = 20, COL2_W = 12;

for (int i = 0; i

cout << "Enter the hours worked and pay rate for the employee with id " << id[i] << ": ";

cout << "Hours: ";

cin >> hours[i];

cout << "Payrate: $";

cin >> payrate[i];

outFile << fixed << setprecision(2);

outFile << left << setw(COL1_W) << "Employee ID" << setw(COL2_W) << "Gross Salary" << endl;

outFile << left<< setw(COL1_W) << id[i] << " $" << setw(COL2_W) << gSalary;

gSalary = payrate[i] * hours[i];

}

outFile.close();

cout << " ==>Gross salary where successfully saved to the output file. ";

system("pause");

return 0;

}

void getInputFile(ifstream &inFile){

string nm;

while(true){

cout << "Enter input file path name:";

cin >> nm;

inFile.open(nm.c_str());

if (!inFile){

cout << "Error opening file ";

}

else

break;

}

}

void getOutputFile(ofstream &outFile){

string nm;

while(true){

cout << "Enter output file path name:";

cin >> nm;

outFile.open(nm.c_str());

if (!outFile){

cout << "Error opening file ";

}

else

break;

}

}

void readdata(ifstream &inFile, string data[]){

for (int i = 0; i

inFile >> data[i];

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Does a cpu rub off ALU, RAM, and a processor

Answered: 1 week ago