Please Write the program in C++ language using simple terms that can be understo
ID: 3720399 • Letter: P
Question
Please Write the program in C++ language using simple terms that can be understood by beginners. Thank you!
PLEASE DON'T GOOGLE IT !!
(Additions: Read employee info from file "employees.txt", write the output to the screen and also to output file "payroll.txt". Process at least 20 employees, print the average gross pay and the total gross pays. )
Multipurpose Payroll Write a program that calculates pay for either an hourly paid worker or a salaried worker. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are paid their regular salary plus any bonus they may have earned. The program should declare two structures for the following data: Hourly Paid: Hours Worked HourlyRate Salaried: Salary Bonus The program should also declare a union with two members. Each member should be a structure variable: one for the hourly paid worker and another for the salaried worker. The program should ask the user whether he or she is calculating the pay for an hourly paid worker or a salaried worker. Regardless of which the user selects, the appropri- ate members of the union will be used to store the data that will be used to calculate the pay Input Validation: Do not accept negative nunbers. Do not accept values greater than 80 for Hours WorkedExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
struct hemp{
int hoursworked;
double hourlyrate;
};
struct semp{
double salary;
double bonus;
};
union emp{
hemp mem1;
semp mem2;
};
int main(){
emp e;
string ch;
int n;
double hr;
double sal;
double bonus;
cout << "Please choose hourly paid employee or Salaried enployee (h/s):";
cin >> ch;
if (ch[0] == 'h'){
while(true){
cout << "Enter hours worked :";
cin >> n;
if (n < 0 || n > 80)
cout << "Invalid value: ";
}
while(true){
cout << "Enter hourly rate :";
cin >> hr;
if (hr < 0)
cout << "Invalid value: ";
}
e.mem1.hoursworked = n;
e.mem1.hourlyrate = hr;
cout << "Total gross pay: $" << hr * n << endl;
}
if (ch[0] == 's'){
while(true){
cout << "Enter salary :";
cin >> sal;
if (sal < 0)
cout << "Invalid value: ";
}
while(true){
cout << "Enter bonus :";
cin >> bonus;
if (bonus < 0)
cout << "Invalid value: ";
}
e.mem2.salary = sal;
e.mem2.bonus = bonus;
cout << "Total gross pay: $" << sal+bonus << endl;
}
}
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct hemp{
int hoursworked;
double hourlyrate;
};
struct semp{
double salary;
double bonus;
};
union emp{
hemp mem1;
semp mem2;
};
int main(){
emp e[100];
string ch;
int n;
double hr;
double sal;
double a1,a2;
double bonus;
ifstream fin("employees.txt"); // format should be, each line - h/s sal/hoursworked bonus/hourlyrate
if (!fin){
cout << "Error opening file ";
return 0;
}
int count = 0;
double sum = 0;
ofstream fout("payroll.txt");
while (fin >> ch >> a1 >> a2){
if (ch[0] == 'h'){
e[count].mem1.hoursworked = a1;
e[count].mem1.hourlyrate = a2;
cout << ch << " " << a1 << " " << a2 << " $"<< a1 * a2 << endl;
fout << ch << " " << a1 << " " << a2 << " $"<< a1 * a2 << endl;
sum = sum + a1 * a2;
}
if (ch[0] == 's'){
e[count].mem2.salary = a1;
e[count].mem2.bonus = a2;
cout << ch << " " << a1 << " " << a2 << " $"<< a1 + a2 << endl;
fout << ch << " " << a1 << " " << a2 << " $"<< a1 + a2 << endl;
sum = sum + a1 + a2;
}
}
cout << "Total gross pay: $" << sum << endl;
cout << "Average gross pay: $" << sum/count << endl;
fout << "Total gross pay: $" << sum << endl;
fout << "Average gross pay: $" << sum/count << endl;
fin.close();
fout.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.