//This program shows how to calculate weekly payment with overtime hours while u
ID: 3761558 • Letter: #
Question
//This program shows how to calculate weekly payment with overtime hours while using functions.
#include <iostream>
#include <iomanip>
#include <string>
#include<stdlib.h>
using namespace std;
void getEmployee(string &name, string &id, double &hoursWork, double &ratePerHour)
{
cout << "Enter full name: ";
getline(cin, name);
cout << "ID number: ";
cin >> id;
cout << "Hours worked: ";
cin >> hoursWork;
cout << "Enter hourly rate: ";
cin >> ratePerHour;
}
double calculateGrossPay(double hoursWork, double ratePerHour)
{
double weekly, total, addpay, extra, pay;
if (hoursWork > 40)
{
extra = hoursWork - 40;
pay = hoursWork * ratePerHour;
weekly = (40 * 15);
addpay = extra * (ratePerHour * 1.5);
total = weekly + addpay;
}
else
{
pay = hoursWork * ratePerHour;
extra = 0;
total = pay;
}
return total;
}
double calculateNetPay(double grossPay, double taxRate)
{
grossPay = (grossPay * taxRate) / 100;
return grossPay;
}
void printEmployeePayment(string name, string id, double netpay)
{
cout << "Name: " << name << endl;
cout << "ID# " << id << endl;
cout << "Net Pay: " << netpay << endl;
}
int main()
{
char ans = 'y';
double hours, pay, extra, addpay, rate;
double tax, wtpay, total, weekly;
string Name;
string IDnumber;
while (ans == 'y')
{
getEmployee(Name, IDnumber, hours, rate);
total = calculateGrossPay(hours, rate);
tax = calculateNetPay(total, rate);
wtpay = total - tax;
printEmployeePayment(Name, IDnumber, wtpay);
cout << "Do you want to add another employee?" << endl;
cin >> ans;
system("cls");
cin.get();
}
cout << "Thank you. Goodbye." << endl;
system("pause");
return 0;
}
Modify and rewrite to do the following tasks:
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.
4. There should be a .cpp file, a Employees.dat file, and Payments.dat file
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
#include<fstream>
#include<stdlib.h>
using namespace std;
void getEmployee(string &name, string &id, double &hoursWork, double &ratePerHour)
{
cout << "Enter full name: ";
getline(cin, name);
cout << "ID number: ";
cin >> id;
cout << "Hours worked: ";
cin >> hoursWork;
cout << "Enter hourly rate: ";
cin >> ratePerHour;
}
double calculateGrossPay(double hoursWork, double ratePerHour) {
double weekly, total, addpay, extra, pay;
if (hoursWork > 40) {
extra = hoursWork - 40;
pay = hoursWork * ratePerHour;
weekly = (40 * 15);
addpay = extra * (ratePerHour * 1.5);
total = weekly + addpay;
} else {
pay = hoursWork * ratePerHour;
extra = 0;
total = pay;
}
return total;
}
double calculateNetPay(double grossPay, double taxRate) {
grossPay = (grossPay * taxRate) / 100;
return grossPay;
}
void printEmployeePayment(string name, string id, double netpay) {
cout << "Name: " << name << endl;
cout << "ID# " << id << endl;
cout << "Net Pay: " << netpay << endl;
}
int main() {
char ans = 'y';
double hours, pay, extra, addpay, rate;
double tax, wtpay, total, weekly;
string Name;
string IDnumber;
//Declare out file variable
ofstream empOutFile ("Employees.dat");
ofstream paymentsOutFile ("Payments.dat");
empOutFile<<"Name IDnumber"<<endl;
paymentsOutFile<<"Name IDnumber NetPay"<<endl;
while (ans == 'y') {
getEmployee(Name, IDnumber, hours, rate);
total = calculateGrossPay(hours, rate);
tax = calculateNetPay(total, rate);
wtpay = total - tax;
printEmployeePayment(Name, IDnumber, wtpay);
//Writing employee details to Employees.dat
empOutFile<<Name<<" "<<IDnumber<<endl;
//Writing employee details to Payments.dat
paymentsOutFile<<Name<<" "<<IDnumber<<" "<<wtpay<<endl;
cout << "Do you want to add another employee?" << endl;
cin >> ans;
system("cls");
cin.get();
}
//Closing the files
empOutFile.close();
paymentsOutFile.close();
cout<<"Writing to Employees.dat is completed ";
cout<<"Writing to Payments.dat is completed ";
cout << "Thank you. Goodbye." << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.