Write a program that does the following: Reads 5 employee identification numbers
ID: 3917522 • Letter: W
Question
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
Note: The above program run is just an example. The ids stored in the input file will be different!
Other Requirements:
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!
In addition, your program must use named constants to store values that will not change during the execution of the program. The program's code must be nicely formatted and easy to read. (i.e. follow the coding guidelines mentioned in class) Points will be deducted if the code is hard to read and it's not properly formatted. Insert a comment with your name at the top of your program.
How to Submit:
Upload ONLY your source file (.cpp). Please verify you uploaded the correct file. If you upload the WRONG FILE you will receive a ZERO. You may upload your file only once. You must name your source file: Lastname_Firstname (i.e. Brown_John). Remember that programs that do NOT COMPILE will receive at most 50 POINTS.
Explanation / Answer
ScreenShot
-------------------------------------------------------
Program
//Header files for I/O,string manipulation and file read
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//Function prototypes
void open_file(ifstream &in);
void read_file(string *empId, ifstream &in);
void user_input(string *empId, int *workingHours, double *hrlySalary);
void calculate_gross(int *workingHours, double *hrlySalary, double *grossSalary);
void open_fileWrite(ofstream &out);
void write_file(string *empId, double *grossSalary,ofstream &out);
int main()
{
//Variable for input and output files
ifstream in;
ofstream out;
//Arrays to store employee details
string empId[5];
int workingHours[5];
double hrlySalary[5];
double grossSalary[5];
//Read file open function call
open_file(in);
//Read file call
read_file(empId,in);
//User prompt to get hrs worked and pay scale
user_input(empId, workingHours, hrlySalary);
//gross salary calculation function
calculate_gross(workingHours, hrlySalary, grossSalary);
//write file open function call
open_fileWrite(out);
//Write into file function call
write_file(empId, grossSalary,out);
return 0;
}
//Open file function
void open_file(ifstream &in) {
string fname;
cout << "Enter the file name with correct path:";
cin >> fname;
in.open(fname);
while (!in) {
cout << "File not exist.Please enter proper name!!!" << endl;;
cout << "Enter the file name with correct path:";
cin >> fname;
in.open(fname);
}
}
//Read file
void read_file(string *empId, ifstream &in) {
string id = "";
int i = 0;
while (!in.eof()) {
in >> id;
*(empId + i) = id;
i++;
}
in.close();
}
//Usser prompt function
void user_input(string *empId, int *workingHours, double *hrlySalary) {
int hrs;
double pay;
for (int i = 0; i < 5; i++) {
cout << " Enter the hours worked and pay rate for the employee with id " <<*(empId+i)<<":" << endl;
cout << "Hours: ";
cin >> hrs;
while(hrs<0) {
cout << "Please enter hours >0!!!" << endl;
cout << "Hours: ";
cin >> hrs;
}
cout << "Pay rate: ";
cin >> pay;
while (pay<7.50) {
cout << "Please enter pay rate >7.50!!!" << endl;
cout << "Pay rate: ";
cin >> pay;
}
*(workingHours + i) = hrs;
*(hrlySalary + i) = pay;
}
}
//Gross salary calculation
void calculate_gross(int *workingHours, double *hrlySalary, double *grossSalary) {
for (int i = 0; i < 5; i++) {
*(grossSalary + i) = *(workingHours + i)*(*(hrlySalary+i));
}
}
//Open file for write
void open_fileWrite(ofstream &out) {
string fname;
cout << "Enter the file name with correct path for write data:";
cin >> fname;
out.open(fname);
while (!out) {
cout << "File not exist.Please enter proper name!!!" << endl;;
cout << "Enter the file name with correct path:";
cin >> fname;
out.open(fname);
}
}
//Write into file
void write_file(string *empId, double *grossSalary,ofstream &out) {
for(int i=0;i<5;i++) {
out << *(empId + i) << " $" << *(grossSalary + i) << endl;
}
out.close();
cout << "File Write Completed!!!" << endl;
}
------------------------------------------------------------
Output
Enter the file name with correct path:C:/Users/deept/Desktop/employee_ids.txt
Enter the hours worked and pay rate for the employee with id A12345:
Hours: 15
Pay rate: 10
Enter the hours worked and pay rate for the employee with id B55874:
Hours: 20
Pay rate: 12
Enter the hours worked and pay rate for the employee with id C69851:
Hours: 21
Pay rate: 14
Enter the hours worked and pay rate for the employee with id D12458:
Hours: 25
Pay rate: 12
Enter the hours worked and pay rate for the employee with id E52242:
Hours: 23
Pay rate: 16
Enter the file name with correct path for write data:C:/Users/deept/Desktop/employee_gross_salary.txt
File Write Completed!!!
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.