In: Computer Science
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information:
First Name
Last Name
Job Title
Employee Number
Hours Worked
Hourly Wage
Number of Deductions Claimed
Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible.
Bonus Points:
Use an input file to read in an unknown number of records into your program: +5 points
Use Vectors instead of arrays in your struct: +5 points
employee.cpp file
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
//global variables for jobTime
const int jobTime = 5;
//global variables for tax rate
const double tax = 1;
//Employee definition of struct type
struct Employee{
string fname;
string lname;
string jobTitle;
int empNo;
int hoursWorked;
double hourlyWage;
int noDeductionClaimed;
};
//display() definiton for displaying the details of all employees
void display(vector<Employee>&emp){
cout<<"\nEmployee Details:";
for(int i=0;i<emp.size();i++){
cout<<"\nName: "<<emp[i].fname<<" "<<emp[i].lname;
cout<<"\nJob Title: "<<emp[i].jobTitle;
cout<<"\nEmployee Number: "<<emp[i].empNo;
cout<<"\nHours Worked: "<<emp[i].hoursWorked;
cout<<"\nHourly Wage: "<<emp[i].hourlyWage;
cout<<"\nNo of deduction claimed: "<<emp[i].noDeductionClaimed;
if(emp[i].hoursWorked>jobTime)
cout<<"\nOvertime: "<<emp[i].hoursWorked-jobTime<<" hours";
//calculating gross pay
double grossPay = emp[i].hoursWorked*emp[i].hourlyWage -emp[i].noDeductionClaimed;
cout<<"\nGross Pay: "<<grossPay;
cout<<"\nTax: "<<tax<<"%";
//calculating net pay
double netPay = grossPay - (grossPay)*tax/100;
cout<<"\nNet Pay: "<<netPay;
cout<<"\n";
}
}
//driver program, execution starts from here
int main(){
//input of Employee type is declared for taking inputs from file
Employee input;
//vector of Employee type is declared
vector<Employee> emp;
//inFile declared for reading from a file
ifstream inFile;
//path for path of file
string path = "emp.txt";
//open file
inFile.open(path);
//checking whether file open correctly or not.
if(!inFile.is_open()){
cout<<"\nFile not opened correctly.";
return 0;
}
//if file opened successfuly , read file until end of file is reached
while(!inFile.eof()){
//extracting data from file
inFile>>input.fname;
inFile>>input.lname;
inFile>>input.jobTitle;
inFile>>input.empNo;
inFile>>input.hoursWorked;
inFile>>input.hourlyWage;
inFile>>input.noDeductionClaimed;
//inserting data into vector emp
emp.push_back(input);
}
//closing file
inFile.close();
//displaying all employees details
//by calling display()
display(emp);
return 0;
}
emp.txt file
Ravi Kumar Salesman 1 6 1300.50 0
Amit Yadav Clerk 2 3 2000.50 1
output