Hi Iam new to c++ programing and I have this project where I must write a program that takes in data from the employee.dat. I have the program started but now Iam stuck any help would be greatly appreciated. heres my instructions.
• Note there are only 4 employees in the file, but the number of employees can change. Your program should read and process any number of employees.
• This program reads data from an input file and displays each weeklyEmp object in a simple payroll report.
• includes needed: fstream, iostream, string, using namespace std, "weekemp.h" and "compfun.h"
• in the main()
1. Create variables to hold lastName, firstName, name, hours, rate exemptions filingStatus and a weeklyEmp see page 352-353
2. Initialize an input stream with a disk file as the source, the file name is employee.dat and show an error message if the file "employee.dat" is not found on the disk
3. Set output format to be two decimal places and use cout to create the report heading as displayed below:
Pay Hours Gross Income FICA Net Employee
Rate Worked Pay Tax Tax Pay Name
==== ====== ===== ====== ==== ====== ==============
4. Initialize all total variables used to zero. You will also need variables to hold the totValue of hours rate, etc. Each variable must be initialized, so for example totHours = 0.0;
5. Process data until end of file by using a while loop, see page 352-353 for an example of using the end of file event to terminate the loop
o Take a look at the EMPLOYEE.DAT file and you will need to read in first and last name first and not last as shown in the while loop
6. Each time through the loop, initialize a weeklyEmp object with new data you just read in from the file. Note the name needs to be submitted lastName, comma, firstName
7. Output individual employees
8. Remember that you will need to calculate the net before outputting it in the report column The calculation was given back in week 6:
o net = emp.grossPay() - emp.incomeTax() - emp.FICATax(); .
9. Increment each total variable to add in new employee stats, for example totGross += emp.grossPay( );
10. End your while loop
11. format your output using cout.width and output all totals to the screen
About cout.width(int)
Pay Hours Gross Income FICA Net Employee
Rate Worked Pay Tax Tax Pay Name
==== ====== ===== ====== ==== ====== ==============
To determine your column widths, start counting at the left most location for the first column, to the last dash in the first column. The first column width is 6. From the end of the first column to the last dash in the second column is 7 (you must include the spaces following the first column as part of the second column. Starting at the end of the second column to the last dash in the third column is 8. Using the same method to determine your column widths.
For the last column you can just put out " " for the 4 spaces following the net pay column and then the name will be left aligned.
Output:
Pay Hours Gross Income FICA Net Employee
Rate Worked Pay Tax Tax Pay Name
===== ====== ====== ====== ===== ====== ================
10.45 38.00 397.10 26.16 30.38 340.56 Greene, Ross
12.00 42.00 516.00 89.42 39.47 387.11 Kristner, Mary
9.99 30.50 304.69 36.34 23.31 245.05 Nicholson, Mellisa
11.57 40.00 462.80 63.49 35.40 363.91 Woodley, Samuel
====== ====== ====== ===== ======
Totals 150.50 1680.59 215.41 128.57 1336.62
Note if the net pay for employee Mellisa Nicholson is a penny off ($245.05 vs. $245.04) or the FICA Tax Total for all four employees is a penny off ($128.57 vs. $128.56), it is probably being caused by how the compiler is rounding numbers - Not a problem.
here is my code so far.
#include <fstream>
#include <iostream>
#include "weekemp.h"
#include "compfun.h"
int main()
{
weeklyEmp anEmp;
string fName, lName, name;
double hours, rate;
int exempts;
string status;
ifstream inFile("employee.dat");
if(! inFile)
{
cout << "**Error opening file 'employee.dat'" << endl;
}
else
{
cout.fill('*');
decimals(cout, 2);
cout << " Pay Hours Gross Income FICA Net Employee" << endl;
cout << "Rate Worked Pay Tax Tax Pay Name" << endl;
while(inFile >> fName >> lName >> hours >> rate >> exempts >> status)
{
name = lName + ", " + fName;
anEmp = weeklyEmp(name, hours, rate, exempts, status);
cout.width(9);
cout << anEmp.grossPay() << " " << anEmp.name() << endl;
}
cout << "====== ===== ===== ====== ===== ======="
}
return 0;
}
here is the other supplied code.
the employee.dat file
Ross Greene 38.0 10.45 4 M
Mary Kristner 42.0 12.00 0 S
Mellisa Nicholson 30.5 9.99 1 S
Samuel Woodley 40.0 11.57 1 S
#include <iostream> // for cout, cin, istream, ostream
#include <cmath> // for floor
#include "string" // for class string
#include "compfun.h" // for decimals(cout, 2) and round (tax, 2)
#include "weekemp.h"
// The value of WEEKLY_ALLOWANCE has changed almost every year so the
// named constant must be checked every year for proper maintenance
// Use IRS publication Circular E, Employer's Tax Guide each new year.
const double WEEKLY_ALLOWANCE = 39.42;
const int MAX_EXEMPTIONS = 99;
const double FICA_TAX_RATE = 0.0765;
//--constructors
weeklyEmp::weeklyEmp()
{ // Default constructor used to allow for arrays.
my_name = " ?name? ";
my_hours = 0.00;
my_rate = 0.00;
my_exemptions = 0;
my_filingStatus = "?status?";
}
weeklyEmp::weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initStatus)
{ // Five argument constructor
my_name = initName;
my_hours = initHours;
my_rate = initRate;
my_exemptions = initExemptions;
if((my_exemptions < 0) || (my_exemptions > MAX_EXEMPTIONS))
{
cout << "**Error** Exemptions for "
<< initName << " == " << initExemptions << endl;
cout << "Exemptions must be in the range of 0.." << MAX_EXEMPTIONS
<< endl;
cout << "**Program Terminated**"
<< endl;
}
if(initStatus == "s" || initStatus == "S")
my_filingStatus = "S";
if(initStatus == "m" || initStatus == "M")
my_filingStatus = "M";
if ( ! (my_filingStatus == "S" || my_filingStatus == "M") )
{
cout << "Filing Status for " << initName << " == " << my_filingStatus
<< endl;
cout << "Status must be one of these four strings \"s\", \"S\", \"m\", or \"M\""
<< endl;
cout << "Terminating program"
<< endl;
}
}
//--modifiers
void weeklyEmp::set_hours(double thisWeeksHours)
{ // post: set the hours worked for a given week
my_hours = thisWeeksHours;
}
void weeklyEmp::set_rate(double thisWeeksRate)
{
my_rate = thisWeeksRate;
}
// -- accessors
double weeklyEmp::grossPay() const
{
double result(0.0);
if(my_hours <= 40)
result = my_hours * my_rate;
else
result = 40 * my_rate + 1.5 * my_rate * (my_hours - 40);
// round to the nearest penny
result = round(result, 2);
return result;
}
double weeklyEmp::incomeTax() const
{
double result(0.0);
double taxableIncome(grossPay() - my_exemptions * WEEKLY_ALLOWANCE);
if(my_filingStatus == "S")
{
if (taxableIncome <= 23.00)
result = 0.00;
else if(taxableIncome <= 397.00)
result = 0.15 * (taxableIncome - 23.00);
else if(taxableIncome <= 928.00)
result = 56.10 + 0.28 * (taxableIncome - 397.00);
else if(taxableIncome <= 2121.00)
result = 204.78 + 0.33 * (taxableIncome - 928.00);
else
result = 598.47 + 0.28 * (taxableIncome - 2121.00);
}
if(my_filingStatus == "M")
{
if(taxableIncome <= 65.00)
result = 0.00;
else if(taxableIncome <= 689.00)
result = 0.15 * (taxableIncome - 65.00);
else if(taxableIncome <= 1573.00)
result = 93.60 + 0.28 * (taxableIncome - 689.00);
else if(taxableIncome <= 3858.00)
result = 341.12 + 0.33 * (taxableIncome - 1573.00);
else
result = 1095.17 + 0.28 * (taxableIncome - 3858.00);
}
// round to the nearest penny
result = round(result, 2);
return result;
}
double weeklyEmp::FICATax() const
{
double result(FICA_TAX_RATE * grossPay());
// round to the nearest penny
result = round(result, 2);
return result;
}
string weeklyEmp::name() const
{
return my_name;
}
bool operator < (const weeklyEmp& left, const weeklyEmp& right)
{
return (left.name() < right.name()); // string defines <
}
bool operator == (const weeklyEmp& left, const weeklyEmp& right)
{
return (left.name() == right.name()); // string defines ==
}
bool operator <= (const weeklyEmp& left, const weeklyEmp& right)
{
return (left.name() <= right.name()); // string defines <=
}
bool operator >= (const weeklyEmp& left, const weeklyEmp& right)
{
return (left.name() >= right.name()); // string defines >=
}
bool operator > (const weeklyEmp& left, const weeklyEmp& right)
{
return (left.name() > right.name()); // string defines >
}
bool operator != (const weeklyEmp& left, const weeklyEmp& right)
{
return (left.name() != right.name()); // string defines >
}
here is the header file
#ifndef WEEKEMP_H
#define WEEKEMP_H // ensure the class definition is compiled only once
#include <iostream> // for class ostream and istream
#include "string" // for class string
#include "bool"
////////////////////////////////////////////////////////////////////
//// Define class weeklyEmp ////////////////////////////////////////
////////////////////////////////////////////////////////////////////
class weeklyEmp {
public:
//--constructors
weeklyEmp();
weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus );
// post: A weeklyEmp object is initialized with 5 arguments like this:
// weeklyEmp anEmp("Hall, Rob", 40.0, 9.75, 3, "M");
// The fourth argument must be in the range of 0 to 99
// The last argument is either "M" for married or "S" for single
weeklyEmp(const weeklyEmp & source);
// This copy sonctructor is discussed in Chapter 10: Pointers
//--modifiers
void set_hours(double thisWeeksHours);
// post: set the hours worked for a given week
void set_rate(double thisWeeksRate);
// post: change the employees hourly rate of pay
//--accessors
double grossPay() const;
// post: return gross pay with overtime
double incomeTax() const;
// post: return the federal income tax
double FICATax() const;
// post: return the social security tax
string name() const;
// post: return the employee's name
private:
string my_name;
double my_hours;
double my_rate;
int my_exemptions;
string my_filingStatus;
};
//--auxilary functions
bool operator < (const weeklyEmp& left, const weeklyEmp& right);
// Return true if left's name alphabetically precedes right's name
bool operator == (const weeklyEmp& left, const weeklyEmp& right);
// Return true if left's name is equal to right's name (case sensitive)
// The file weekemp.cpp includes compfun so you'll also get
// the other four relational operators >, <=, >=, and !=
#endif