1. Save all employees entered on the keyboard into a file called “Employees.dat”
ID: 3761968 • Letter: 1
Question
1. Save all employees entered on the keyboard into a file called “Employees.dat”. 2. After the calculation of each employee payment is done, instead of printing all of the employee payments on screen save him or her information into a file called “Payments.dat”. 3. Enter five to ten samples of employees.
Write a program that calculates weekly payment. The program will ask the user full name, ID number (make one up), and hours worked. An hourly worker’s gross pay is basically his/her work hours that week multiplied by his/her regular hourly pay rate. However, after the first 40 work hours of the week, each additional work hour is paid at an overtime rate that is 1.5 times of the regular hourly rate. For example if the user’s hourly rate is $15 and worked for 48 hours, the additional 8 hours will be paid at $22.50/hour. Use 5% as tax deduction. The program will calculate and display the net payment. Use loop to allow users to enter multiple employees. Your program should include four functions as shown below: //This function gets employee information void getEmployee(string &name, string &id, double &hoursWork, double &ratePerHour); //This function calculates and returns gross pay double calculateGrossPay(double hoursWork, double ratePerHour); // This function calculates and returns net pay double calculateNetPay(double grossPay, int taxRate); // This function prints name, id, and net pay void printEmployeePayment(string name, string id, double netpay); Your main() should utilize the four functions above by calling them in your program to input, calculate and print the weekly payment. can you show that it works and runs. I do not ant to use float can you use double thanks.
This is wht i have so far
#include
#include
#include
using namespace std;
// This function will obtain employees information
void getEmployee(string &fullname, string &id, double &hw, double &rph)
Explanation / Answer
Its a wise decision, to maintain the id as int, instead of string, as you want the id to be incremented for every employee. Just go through the code herewith:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//This function gets employee information
void getEmployee(string &name, int &id, double &hoursWork, double &ratePerHour)
{
ofstream fp;
fp.open("Employees.dat", ios::app); //Open a file Employees.dat in append mode.
cout<<"Enter the name of the employee: ";//Read employee name.
cin>>name;
id++; //Update employee id.
cout<<"Enter the number of hours worked: ";//Read employee work hours.
cin>>hoursWork;
cout<<"Enter the hourly wage: "; //Read employee hourly wage.
cin>>ratePerHour;
fp<<"Name: "<<name<<" "; //Save the details to file Employees.dat.
fp<<"ID: "<<id<<" ";
fp<<"HoursWorked: "<<hoursWork<<" ";
fp<<"ratePerHour: "<<ratePerHour<<endl;
}
//This function calculates and returns gross pay
double calculateGrossPay(double hoursWork, double ratePerHour)
{
double grossPay;
if(hoursWork <= 40) //If work hours are less than 40.
grossPay = hoursWork * ratePerHour; //Just calculate the grossPay, with no bonus.
else //Else calculate grosspay with bonus.
grossPay = (40 * ratePerHour) + 1.5 * (hoursWork - 40) * ratePerHour;
return grossPay;
}
// This function calculates and returns net pay
double calculateNetPay(double grossPay, int taxRate)
{
return grossPay * (1 - (double)taxRate/100); //Calculate 95% of the grosspay and return as nett pay.
}
// This function prints name, id, and net pay
void printEmployeePayment(string name, int id, double netpay)
{
ofstream fp;
fp.open("Payments.dat", ios::app); //Open the file in append mode.
cout<<"Name: "<<name<<" "; //Print the data to screen.
cout<<"ID : "<<id<<" ";
cout<<"Nett Pay: "<<netpay<<endl;
fp<<"Name: "<<name<<" "; //Also write the data to file.
fp<<"ID : "<<id<<" ";
fp<<"Nett Pay: "<<netpay<<endl;
}
int main()
{
char choice;
string name;
double hoursWork, ratePerHour, grossPay, nettPay;
const int taxRate = 5;
int id = 0;
ofstream fp;
fp.open("Employees.dat");
//ofstream fp;
fp.open("Payments.dat");
while(true)
{
cout<<"Do you want to enter the employee information Y/N: ";
cin>>choice;
if(choice == 'Y')
{
getEmployee(name, id, hoursWork, ratePerHour);
grossPay = calculateGrossPay(hoursWork, ratePerHour);
nettPay = calculateNetPay(grossPay, taxRate);
printEmployeePayment(name, id, nettPay);
}
else
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.