here is my header file:
#ifndef weeklyEmp_h
#define weeklyEmp_h
// SPECIFICATION FILE weeklyEmp.h
//
//----------------------------------------------------------------------
#include <string>
using namespace std;
class weeklyEmp{
public:
//constructors
weeklyEmp();
weeklyEmp (string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus);
//--modifiers
void set_hours(double thisWeeksHours);
//post: Set the hours worked for a given week
void set_rate(double thisWeeksRate);
//post: Change the employee's hourly rate of pay
//--accessors
double grosPay() 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;
};
#endif //weeklyEmp_h
這裏是一個包含.cpp文件:從程序中輸出錯誤。編譯沒有錯誤
#include <iostream>
#include <string>
#include "weeklyEmp.h"
using namespace std;
const double WEEKLY_ALLOWANCE = 39.42;
const double FICA_TAX_RATE = 0.0765;
weeklyEmp::weeklyEmp()
{
my_name = "?name?";
my_hours = 0.0;
my_rate = 0.0;
my_exemptions; 0;
my_filingStatus = "?filingStatus?";
}
weeklyEmp::weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus)
{
my_name; initName;
my_hours; initHours;
my_rate; initRate;
my_exemptions; initExemptions;
my_filingStatus; initFilingStatus;
}
//--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)
//post: Change the employee's hourly rate of pay
{
my_rate = thisWeeksRate;
}
//--accessors
double weeklyEmp::grosPay() const
//post: Return gross pay with overtime
{if(my_hours <=40) return my_hours * my_rate;
else
return (40*my_rate) + (my_hours-40) * 1.5 * my_rate;
}
double weeklyEmp::incomeTax() const
//post: Return the federal income tax
{
double result(0.0);
double taxableIncome(grosPay() - my_exemptions * WEEKLY_ALLOWANCE);
if(my_filingStatus == "S" || 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" || 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 */
/* include compfun.cpp for round function */
result = (result, 2);
return result;
}
double weeklyEmp::FICATax() const
//post: Return the social security tax
{
return grosPay() * FICA_TAX_RATE;
}
string weeklyEmp::name() const
//post: Return the employee's name
{
return my_name;
}
最後主CPP文件:
#include <iostream>
#include <string>
#include "weeklyEmp.h"
#include "COMPFUN.H"
using namespace std;
int main()
{
string name;
double rate;
double hours;
int exemptions;
string filingStatus;
cout <<"Name: ";
cin >> name;
cout << "Hourly Rate:";
cin >> rate;
cout << "Hours Worked:";
cin >> hours;
cout << "Exemptions: ";
cin >> exemptions;
cout<< "S) ingle/M) arried: ";
cin >> filingStatus;
cout << endl;
weeklyEmp emp(name, hours, rate, exemptions, filingStatus);
double net = emp.grosPay() - emp.incomeTax() - emp.FICATax();
cout << "Employee: " << name << endl;
cout << "Gross Income FICA Net" << endl;
decimals(cout, 2);
cout << emp.grosPay() << emp.incomeTax() <<emp.FICATax() <<net << endl;
cout.width(9); cout << emp.grosPay();
cout.width(13); cout << emp.incomeTax() << endl;
system("pause");
return 0;
}
這是垃圾輸出我收到。我認爲錯誤是在.cpp文件中(不是主要的,但包括在內)。我無法找到它。
姓名:Mark 每小時收費:10 工作小時數:10項 豁免:1個 S)英格爾/ M)arried:S
員工:馬克 總收入FICA淨 00000000000000000000000000000000000000000000.002.0065539732969740393000000000000 00000000000000.00791188802582421710000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000.00 按任意鍵繼續。 。 。
什麼是'my_name; initName;'等應該這樣做?導致它做什麼,幾乎沒有什麼。 :P我有點以爲你想要一個'='而不是'''兩者之間。 – cHao
您期望的輸出是什麼? –