2015-02-07 29 views
0

所以我的教授要求我們制定一個計劃,根據投入的工資率和工作時間來計算預算。百分比由教授給出。另外,我對C++很陌生,所以請原諒我,如果這個垃圾。爲什麼這些簡單的函數給我天文大小的數字?

#include <iostream> 

using namespace std; 

int main() 

{ 
    int hours; 
    double Pay; 

    const double Net  = hours * Pay; 
    const double Tax  = Net * .14; 
    const double afterTax = Net - Tax; 
    const double Clothes = .1 * afterTax; 
    const double Supplies = .01 * afterTax; 
    const double Bonds = .25 * afterTax; 
    const double Parents = Bonds/2; 

    cout << "Please enter hours worked \n"; 
    cin >> hours; 
    cout << "Please enter hourly rate \n"; 
    cin >> Pay; 

    cout << "Total Number of hours worked: " << hours << endl; 
    cout << "Total income before taxes: " << Net << endl; 
    cout << "Net Income: " << afterTax << endl; 
    cout << "Money Spent on clothes and accessories: " << Clothes << endl; 
    cout << "Money spent on school supplies: " << Supplies << endl; 
    cout << "Money spent on savings bonds: " << Bonds << endl; 
    cout << "Money spent by parents for savings bonds: " << Parents << endl; 
    cout << "Remaining: " << afterTax - Clothes - Supplies - Bonds << endl; 

    return 0; 
} 
+0

對貨幣值使用'double'是錯誤的:[Money Pattern](http://www.di-mare.com/adolfo/p /money.htm)。 – 2015-02-07 20:12:37

回答

3

hours和其他一些變量是未初始化的,但是你用自己的值來初始化其他變量。您需要重新排列計算,以便在用戶輸入必要項目後執行計算。

+0

非常感謝。我移動了變量,現在它可以工作。 – Cuzi 2015-02-07 20:19:17

-2

當您第一次創建變量時,沒有小時或薪酬的價值。舉例來說,net = hours * pay聲明應該等於零,這樣可以解釋爲什麼你的結果可能不準確。嘗試將輸入函數切換到變量賦值之前,看看是否有竅門

+0

「應該等於零」......否,變量「小時」和「付費」不屬於執行零初始化的任何類別。 – 2015-02-07 20:17:24

+0

我不知道C++,所以我不確定他們的初始化規則... – edit 2015-02-07 20:26:46

相關問題