Assignment:
Print the following heading at the top of the output page: Miser Corporation Payroll
Read an unknown number of employee data records as shown below. Each group of data will contain an employee's name, hours worked, rate of page and age. A typical group of data will be:
Duck, Donald 45 3.25 51
Print the data as it is read in, together with appropriate messages (e.g., the name is... the rate of pay is... etc.).
For each employee, compute and print the employee's base pay, which includes overtime (paid at one and a half times the normal rate) for each hour over 40. For example, if an employee earning $20.00 per hour works for 48 hours, then she will be paid for 40 hours at her normal rate plus 8 extra hours at $30.00 (one and a half times $20.00).
For each employee compute the tax paid by the employee, according to this formula: If the employee is 55 years old (or order), then the employee pays tax at a rate of 50% of the base pay; if the employee is below 55, then the tax is 10% of the base pay. Print the tax and the net pay after taxes.
Repeat this process until you have read the last employee. You must decide how to detect the end of the data (you should explain your method in a comment).
Print the name and age of the oldest employee. Do the same for the youngest employee.
Print the name and taxes of the employee who paid the most in taxes.
Print an alphabetically sorted list of all the company's employees.
Print a list of the employees sorted by net pay.
After all your results have been printed, print a message saying that the payroll program is complete.
Code:
//============================================================================
// Assignment : Homework #3
// Date : 11 Mar 2012
//============================================================================
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NAME_SIZE = 51;
struct records {
public:
char name[NAME_SIZE];
int hours;
double rate;
int age;
double basepay;
double tax;
double netpay;
};
int main() {
cout << "tttMiser Corporation Payrolln" << endl;
ifstream datafile; // Declare input file
datafile.open("datafile.txt", ios::in); // Open input file
// Check for error opening input file
if (datafile.fail())
{
cout << "Error opening file; program halted." << endl;
return 0;
}
records employee;
datafile.read(reinterpret_cast<char *>(&employee),sizeof(employee));
while (!datafile.eof()){
cout << "Name:t" << employee.name << endl;
cout << "Hours worked:t" << employee.hours << endl;
cout << "Rate of pay:t" << employee.rate << endl;
cout << "Age:t" << employee.age << endl;
cout << endl;
datafile.read(reinterpret_cast<char *>(&employee),sizeof(employee));
}
datafile.close(); // Close input file
cout << "The payroll program is complete.";
return 0; // Close main program function
}
Output:
> Miser Corporation Payroll
>
> Name: Duck, Donald
> 45.0 3.50 60
> Mouse, Mickey
> 35 6.50 55
> Brown, Charlie
> 35 3.00 20
> Oyle, Olive
> 40 4.50
> Hours worked: 1853321074
> Rate of pay: 6.80457e+199
> Age: 892537445
>
> Name: 60
> Man, He
> 45 7.50 20
> Ra, She
> 40 3.50 20
> Jetson, George
> 45 3.50 55
> Starr, Brenda
> 35 8.40 60
> Wom
> Hours worked: 1701278319
> Rate of pay: 1.82918e-76
> Age: 171259145
>
> The payroll program is complete.