In: Computer Science
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a half for the over time hours. So for 40.37 hours 0.37 hours are over time so that will be 0.37 * 12.38 * 1.5 Plus 40.0 * 12.38 The overtime amount is 6.87 $ and the 'straight' pay is 495.20 ( total is 502.07 ) So our first employee will show as : Smith, John 40.37 12.38 502.07 using visual studio 2019
Source Code in C++:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("EmployeeNameTime.txt"); //opening file for
reading
//declaring variables to store information for every employee
string firstName,lastName;
double hours,rate;
while(in >> firstName && in >> lastName
&& in >> hours && in >> rate) //while
record exist
{
double pay;
//calculating pay
if(hours<=40)
pay=hours*rate;
else
pay=40*rate+(hours-40)*1.5*rate;
//output
cout << lastName << ", " << firstName << "
" << hours << " " << rate << " " <<
pay << endl;
}
return 0;
}
Input file:
Output: