2014-01-25 104 views
0

這是一項家庭作業,我在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; 
} 
+0

壓痕位發生了什麼? –

+3

準備[最小完整示例](http://sscce.org)是一項非常有價值的技能,非常值得學習。 – Beta

+0

將魔術數字(如40)存儲在以數字本身命名的變量中是錯誤的。給它一個合理的名字,例如'hoursPerWeek'。將幻數存儲在變量中而不是使用數字本身的要點在於它將來可能會發生變化。在將來的更新中,'forty = 50'將沒有任何意義,但'hoursPerWeek = 50'會(希望你也從我的例子中注意到「四十」不是一個單詞)。 – JBentley

回答

0

你工作的時間爲0,四十是40.在你的第一個分支你有weekly_hours - 四十,這將給你-40。我不知道你在找什麼,但你應該從那裏開始

0

你有兩個costructors。一個默認(不帶任何參數),另一個帶參數。

MyEmployee::MyEmployee() 

這裏要設置很多值設置爲默認(初始化到你想要的值)

MyEmployee::MyEmployee(int e, string n, string s, string p, double h, double w) 

這裏你只設置幾個參數,保持所有其他以未初始化又名垃圾一樣FOURTYOVERTIME_RATEFEDERAL_TAX,STATE_TAX等。

而當你創建如下對象時,參數構造函數被調用,保持許多事情未被初始化。

MyEmployee emp1(1, "Dale Nelson", "480 UVU lane, Orem, UT 84058", "801-863-8000", 10, 45); 

因此,在您的constructor中初始化其他變量。

0

有一個變量FOURTY其值應該是40是可怕的。作爲一個會員變量(就像它的價值可能對每個員工不同)更是如此(儘管在軟件工程中並非完全不切實際)。但是,您只能在兩個構造函數的一個中設置它的值。在構造函數中,使用,它的值仍未定義,這會導致出現問題。