Your program will calculate employees\' wages from data read from a file combine
ID: 3787140 • Letter: Y
Question
Your program will calculate employees' wages from data read from a file combined with data inputted by the user. The file contains employee records, and each record consists of an employee's ID and hourly rate. The program will read the file once to determine the number of records. Then it will perform a dynamic memory allocation for two parallel arrays, one to store the employee IDs and another to store the hourly rates, setting the array sizes to be equal to the number of records. Your program will read the file a second time and store the employee IDs and hourly rates in the parallel arrays. Your program prompts the user to type in an employee ID, the number of hours worked by that employee, and searches for a match in the employee ID array. If a match is found, the program displays the hourly rate, calculates and displays the wage. The wage is calculated as hourly rate number of hours worked. The program performs input validation: the employee ID must be 1 and NUM RANGE, and the number of hours must be 0. If the input is invalid, the user is prompted to reenter the data Additional Requirements a) Functions Your program must implement the following functions: getNumber Records: The function returns the actual number of employee records read from the file. If the file cannot be opened, your program should output an error message and exit getData: This function takes as arguments an ID array and a rate array, reads from the input file and populates the parallel arrays. If the file cannot be opened, your program should output an error message and exit This function takes as arguments an employee ID, an array, the actual number of employees search and searches the array for a match. The function returns the index of the element where a match is found, or -1 if no match is found. For this assignment, implement the linear search You are encouraged to implement additional functions to make your program more modular. b) Assumptions You can assume that each employee has a unique ID, and the possible IDvalues range from 1 to NUM RANGE 999999 inclusive. Employee ID and rate are of type i You can hard code the file name employee File2.txt". The file is posted on elearning.Explanation / Answer
PROGRAM CODE:
/*
* employeeWages.cpp
*
* Created on: 04-Feb-2017
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
using namespace std;
#define NUM_RANGE 999999
string filename;
int getNumberRecords()
{
ifstream infile(filename);
string line;
int counter = 0;
if(!infile.is_open())
{
cout<<"The file could not be opened.";
exit(0);
}
while(getline(infile, line))
{
counter++;
}
infile.close();
return counter;
}
void getData(int employeeID[], int rate[])
{
ifstream infile(filename);
string line;
int counter = 0;
if(!infile.is_open())
{
cout<<"The file could not be opened.";
exit(0);
}
while(getline(infile, line))
{
istringstream iss(line);
iss>>employeeID[counter]>>rate[counter];
counter++;
}
infile.close();
}
int search(int emplyeeID[], int id, int totalRecords)
{
for(int i=0; i<totalRecords; i++)
{
if(emplyeeID[i] == id)
{
return i;
}
}
return -1;
}
int main()
{
filename = "employeeFile2.txt";
int totalRecords = getNumberRecords();
int choice = 1;
int employeeID[totalRecords];
int rate[totalRecords];
getData(employeeID, rate);
cout<<"Actual Number of Records: "<<totalRecords<<endl;
cout<<"-----------------------------"<<endl<<endl;
while(choice != 0)
{
int id, hours;
cout<<"Input employee ID. 0 to quit: ";
cin>>id;
if(id == 0)
exit(0);
while(id<0 || id>NUM_RANGE)
{
cout<<"Invalid entry. ID must be between 1 and 999999. Please reenter: ";
cin>>id;
}
cout<<"Input the number of hours: ";
cin>>hours;
while(hours<0)
{
cout<<"Invalid entry. Hours must be positive. Please reenter: ";
cin>>hours;
}
cout<<"ID = "<<id<<" hours = "<<hours<<endl;
int searchresult = search(employeeID, id, totalRecords) ;
if(searchresult == -1)
cout<<"Employee not found";
else cout<<"Employee "<<id<<", rate = "<<rate[searchresult]<<", hours = "<<hours<<", wage = "<<rate[searchresult] * hours;
cout<<" It took "<<(searchresult==-1?totalRecords:searchresult+1)<<" comparisons ";
}
return 0;
}
INPUT: (EmployeeFile2.txt)
123 30
145 35
134 54
874 43
472 23
324 22
124 34
125 33
126 38
128 33
129 33
130 40
131 34
132 32
135 42
136 32
137 44
138 43
139 45
140 34
141 32
142 34
143 42
144 32
146 44
147 43
148 45
149 34
150 34
151 32
152 42
153 32
154 44
155 43
156 45
157 34
158 32
159 34
160 42
161 32
162 44
163 43
164 45
165 34
OUTPUT:
Actual Number of Records: 44
-----------------------------
Input employee ID. 0 to quit: 9876432
Invalid entry. ID must be between 1 and 999999. Please reenter:
132
Input the number of hours: 9
ID = 132 hours = 9
Employee 132, rate = 32, hours = 9, wage = 288
It took 14 comparisons
Input employee ID. 0 to quit: 150
Input the number of hours: -6
Invalid entry. Hours must be positive. Please reenter:
8
ID = 150 hours = 8
Employee 150, rate = 34, hours = 8, wage = 272
It took 29 comparisons
Input employee ID. 0 to quit: 145
Input the number of hours: 10
ID = 145 hours = 10
Employee 145, rate = 35, hours = 10, wage = 350
It took 2 comparisons
Input employee ID. 0 to quit: 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.