In c++ In this program you are asked to read a data file that contains multiple
ID: 3833451 • Letter: I
Question
In c++
In this program you are asked to read a data file that contains multiple records of employee last names, hours worked, and hourly pay rates. The single typical record may look like below:
Smith 39 10.23
Data File will have multiple records similar to above (There are no sentinel values in this file!).
A function called ReadFileAndFillArrays whose prototype is given below will take three arrays and an ifstream object as arguments. Your program will read the file one record at a time and fill all three arrays and return their logical length as a return statement from the function. The prototype is below:
int ReadFileAndFillArrays(string LastNames[], int HoursWorked[], double PayRates[], ifstream &in);
Assume in the main function that the input file is already successfully opened and is bonded to an input file that attaches to data file with records similar to as shown in Figure above (That means that you can assume that you have to write NO CODE to open the input file and do any error check on it. Just assume that ifstream object that is bonded to successfully opened data file is called in).
Then main function calls the function ReadFileAndFillArrays and supplies it the necessary actual arguments. That means that ReadFileAndFillArrays will fill the three arrays (lastNames, HoursWorked, and PayRates) and return the logical length of all three as a return value. Below is an example of the file being read (Notice that no sentinel values are there in the file):
Smith 39 10.23
Ghyam 45 30.45
Nilly 50 25.45
Willy 60 28.45
Jill 35 20.18
Quirky 38 12.22
After three arrays are filled by function ReadFileAndFillArrays and their logical length is returned by return value, the main function does the following:
Iterates through all three arrays to compute and print the last name of the employee and their salary to the console and a user provided file. The salary calculation rules are as follows:
If worker works up to 40 hours then their salary is hours worked multiplied by the pay rate.
If hours worked exceed 40 then overtime hours are paid at the rate of 1.5 times of the pay rate.
You have used the above salary calculation algorithm in assignment 6. The overall output from the program to console and output file for the file shown above would look like below:
-------------------------------------------------
Hello Smith
Your weekly salary is: $398.97
-------------------------------------------------
-------------------------------------------------
Hello Ghyam
Your weekly salary is: $1446.38
-------------------------------------------------
-------------------------------------------------
Hello Nilly
Your weekly salary is: $1399.75
-------------------------------------------------
-------------------------------------------------
Hello Willy
Your weekly salary is: $1991.50
-------------------------------------------------
-------------------------------------------------
Hello Jill
Your weekly salary is: $706.30
-------------------------------------------------
-------------------------------------------------
Hello Quirky
Your weekly salary is: $464.36
-------------------------------------------------
Other Requirements
The cosmetics of output are not important. However, getting the salary calculation math (algorithm) is crucial. Depending upon the seriousness, three to five points will be taken off for each compile error. Points taken off for logical errors may be higher. A short algorithm that shows how loops are used to process arrays, in function ReadFileAndFillArrays, and in main function is required. Thus in the documentation, only a description of the algorithm is required. It is not expected that anyone would make data type errors, but points would be deducted for data type errors. All salary output must be formatted to two decimal places and $ must be appended before the salary output.
Explanation / Answer
I implemented function and added printing code in main.
int ReadFileAndFillArrays(string LastNames[], int HoursWorked[], double PayRates[], ifstream &in){
int i = 0;
string name;
double rate;
int hours
while(in>>name>>hours>>rate){
LastNames[i] = name;
HoursWorked[i] = hours;
PayRates[i] = rate;
i++;
}
return i;
}
int main(){
string LastNames[100];
int HoursWorked[100];
double PayRates[100];
int n = ReadFileAndFillArrays(LastNames, HoursWorked, PayRates, in);
for(int i=0; i<n; i++){
double pay = 0;
if(HoursWorked[i] > 40){
pay = 40*PayRates[i] + 1.5*PayRates[i]*(HoursWorked[i]-40);
}else{
pay = HoursWorked[i]*PayRates[i];
}
cout<<"-------------------------------------------------"<<endl;
cout<<"Hello "<<LastNames[i]<<endl;
cout<<"Your weekly salary is: $"<<pay<<endl;
cout<<"-------------------------------------------------"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.