I have this code from C++ and these are the instructions Write a program that do
ID: 3917824 • Letter: I
Question
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:
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];
}
}
Employee ID Gross Salary A12345 $210.00 B55874 $375.00 C69851 $622.69 D12458 $770.00 E52242 $1020.00Explanation / Answer
All the requirements of the question have been satisfied. Since your program did not contain classes and object I have also refrained from using it.
Global variables are variables that are declared outside all the functions and hence accessible to all functions in the program. The question states that if global variables are to be used they must be declared as constants. The program below contains no global variables thus the condition of global variables is satisfied. Appropriate comments haave been added for beter understanding.
Code:
#include<iostream>
#include<string>
#include<fstream>
#include <iomanip>
using namespace std;
#define SIZE 5
void getInputFile(ifstream &inFile)//Accepts input file name from user and opens it
{
string nm;
while(true)
{
cout << "Enter input file path name:";
cin >> nm;
inFile.open(nm.c_str());
if (!inFile)//Checks if file is correct
{
cout << "Error opening file ";
}
else
break;
}
}
void getOutputFile(ofstream &outFile)//Accepts output file name from user and opens it
{
string nm;
while(true)
{
cout << "Enter output file path name:";
cin >> nm;
outFile.open(nm.c_str());
if (!outFile)//Checks if file is correct
{
cout << "Error opening file ";
}
else
break;
}
}
void readdata(ifstream &inFile, string data[])//Reads data from input file
{
for (int i=0;i<5;i++)
inFile >> data[i];//Data extracted from file is put in array
for (int i=0;i<5;i++)
{
cout<<data[i]<<endl;
}
}
void getdata(string id[],double hours[],double payrate[])//Accepts hours and payment from user
{
int i;
for (i=0;i<5;i++)
{
cout << "Enter the hours worked and pay rate for the employee with id " << id[i] << ": ";
do
{
cout <<"Hours: ";
cin >> hours[i];
if(hours[i]<0)
cout<<"Error: The hours should be greater or equal to 0. ";
}while(hours[i]<0);//Checks if hours are less than 0. If hours less than 0 then enter again
do
{
cout << "Payrate: $";
cin >> payrate[i];
if(payrate[i]<7.5)
cout<<"Error: The payrate should be greater or equal to 7.5. ";
}while(payrate[i]<7.5);//Checks if payrate is less than 7.5. If payrate less than 7.5 then enter again
}
}
void calculate(double hours[],double payrate[],double gSalary[])//Calculates gross salary and saves it in gSalary[]
{
int i;
for(i=0;i<5;i++)
{
gSalary[i] = payrate[i] * hours[i];
}
}
void writeop(ofstream &outFile,string id[],double gSalary[])//Writes the entire op to the output file
{
outFile << fixed << setprecision(2);
outFile << left << setw(20) << "Employee ID" << setw(12) << "Gross Salary" << endl;
//Inserts the header line. Placing this line inside the for loop will lead to the line being inserted multiple times
int i;
for(i=0;i<5;i++)
{
outFile << left<< setw(20) << id[i] << " $" << setw(12) << gSalary[i]<<endl;//Insert Each Record
}
cout<< " Output Written Successfully! ";
}
int main()
{
//FileStreams
ifstream inFile;
ofstream outFile;
//Declaring all Variables
string id[SIZE];
double hours[SIZE];
double payrate[SIZE];
double gSalary[SIZE];
//Function Calls
getInputFile(inFile);
getOutputFile(outFile);
readdata(inFile,id);
getdata(id,hours,payrate);
calculate(hours,payrate,gSalary);
writeop(outFile,id,gSalary);
inFile.close();
outFile.close();
}
Output:
Enter input file path name:E: estemployee_id.txt
Error opening file
Enter input file path name:E: estemployee_ids.txt
Enter output file path name:E: estop.txt
M5555
N6666
P7777
Q8888
R9999
Enter the hours worked and pay rate for the employee with id M5555:
Hours: -3
Error: The hours should be greater or equal to 0.
Hours: -4
Error: The hours should be greater or equal to 0.
Hours: 3
Payrate: $10
Enter the hours worked and pay rate for the employee with id N6666:
Hours: 5
Payrate: $6
Error: The payrate should be greater or equal to 7.5.
Payrate: $7.5
Enter the hours worked and pay rate for the employee with id P7777:
Hours: 9
Payrate: $9
Enter the hours worked and pay rate for the employee with id Q8888:
Hours: 10
Payrate: $10
Enter the hours worked and pay rate for the employee with id R9999:
Hours: 11
Payrate: $10
Output Written Successfully!
op.txt:
Employee ID Gross Salary
M5555 $30.00
N6666 $37.50
P7777 $81.00
Q8888 $100.00
R9999 $110.00
Please comment if you need any further help or clarification.