這是一項家庭作業,我在C++方面很新,但我需要爲員工計算工資。而不是在1000美元以下的明顯答案,它給了我一個1.5 * 10^23的負數或類似的數字。我做錯了什麼?下面是我對代碼:計算類不工作
//class .cpp file
#include <string>
#include <fstream>
#include "MyEmployee.h"
using namespace std;
MyEmployee::MyEmployee()
{
EMPLOYEE_NUMBER = 0;
NAME = "";
STREET_ADDRESS = "";
PHONE_NUMBER = "";
HOURLY_WAGE = 0;
WEEKLY_HOURS = 0;
GROSS_PAY = 0;
STATE_TAX = .075;
FEDERAL_TAX = .2;
OVERTIME_RATE = 1.5;
NET_PAY = 0;
FOURTY = 40;
OVERTIME_PAY = 0;
}
MyEmployee::MyEmployee(int e, string n, string s, string p, double h, double w)
{
EMPLOYEE_NUMBER = e;
NAME = n;
STREET_ADDRESS = s;
PHONE_NUMBER = p;
HOURLY_WAGE = h;
WEEKLY_HOURS = w;
}
int MyEmployee::getEmployeeNumber() const
{
return EMPLOYEE_NUMBER;
}
int MyEmployee::setEmployeeNumber(int e)
{
EMPLOYEE_NUMBER = e;
return EMPLOYEE_NUMBER;
}
string MyEmployee::getName() const
{
return NAME;
}
string MyEmployee::setName(string n)
{
NAME = n;
return NAME;
}
string MyEmployee::getStreetAddress() const
{
return STREET_ADDRESS;
}
string MyEmployee::setStreetAddress(string s)
{
STREET_ADDRESS = s;
return STREET_ADDRESS;
}
string MyEmployee::getPhoneNumber() const
{
return PHONE_NUMBER;
}
string MyEmployee::setPhoneNumber(string p)
{
PHONE_NUMBER = p;
return PHONE_NUMBER;
}
double MyEmployee::getHourlyWage() const
{
return HOURLY_WAGE;
}
double MyEmployee::setHourlyWage(double h)
{
HOURLY_WAGE = h;
return HOURLY_WAGE;
}
double MyEmployee::getWeekHours() const
{
return WEEKLY_HOURS;
}
double MyEmployee::setWeekHours(double w)
{
WEEKLY_HOURS = w;
return WEEKLY_HOURS;
}
double MyEmployee::calcPay()
{
if (WEEKLY_HOURS > FOURTY)
{
OVERTIME_PAY = ((WEEKLY_HOURS - FOURTY) * HOURLY_WAGE) * OVERTIME_RATE;
GROSS_PAY = OVERTIME_PAY + (FOURTY * HOURLY_WAGE);
NET_PAY = GROSS_PAY - (GROSS_PAY * FEDERAL_TAX) - (GROSS_PAY * STATE_TAX);
}
else
{
GROSS_PAY = WEEKLY_HOURS * HOURLY_WAGE;
NET_PAY = GROSS_PAY - (GROSS_PAY * FEDERAL_TAX) -(GROSS_PAY * STATE_TAX);
}
return NET_PAY;
}
//This is my "driver" or main code:
#include <string>
#include <iostream>
#include <iomanip>
#include "MyEmployee.h"
using namespace std;
void printCheck(MyEmployee&);
int main()
{
MyEmployee emp1(1, "Dale Nelson", "480 UVU lane, Orem, UT 84058", "801-863-8000", 10, 45);
MyEmployee emp2(2, "John Smith", "161 South 550 East, Enterprise, Utah 84725", "801-867-5309", 12.5, 30);
printCheck(emp1);
system("PAUSE");
system("CLS");
printCheck(emp2);
system("PAUSE");
return 0;
}
void printCheck(MyEmployee& employ1)
{
cout.precision(2);
cout << "-------------------Fluff Shuffle Electronics----------------------" << endl;
cout << "\nPay to the order of " << employ1.getName() << " ............................$" << fixed << setprecision(2) << employ1.calcPay() << endl;
cout << "United Bank of Orem" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "Hours worked: " << employ1.getWeekHours() << endl;
cout << "Wages: $" << fixed << employ1.getHourlyWage() << endl;
}
壓痕位發生了什麼? –
準備[最小完整示例](http://sscce.org)是一項非常有價值的技能,非常值得學習。 – Beta
將魔術數字(如40)存儲在以數字本身命名的變量中是錯誤的。給它一個合理的名字,例如'hoursPerWeek'。將幻數存儲在變量中而不是使用數字本身的要點在於它將來可能會發生變化。在將來的更新中,'forty = 50'將沒有任何意義,但'hoursPerWeek = 50'會(希望你也從我的例子中注意到「四十」不是一個單詞)。 – JBentley