2014-09-29 77 views
-3

到目前爲止,除了函數Pelly之外,我的代碼都能正常工作。它不會返回AdjustedGross,就像它假設的那樣。即時通訊非常確定數學是正確的,我認爲問題是如何調用函數。我不太擅長功能。任何幫助將不勝感激。函數調用錯誤

#include <iostream> 
using namespace std; 

int main() 
{ 
    double Federal = 0, PellGrant = 5730, AdjustedGross = 0, Total = 0; 
    int YesNo; 
    int const StaffordLoan = 9500; 

    cout << "Let me forecast your FAFSA" << endl; 
    cout << "Enter your adjusted Gross income: " << endl; cin >> AdjustedGross; 

    if (AdjustedGross >= 30000) 
    { 
     cout << "Sorry, your income is too high for this forecaster"; 
     return 0; 
    } 

    cout << "Can someone claim you as a dependent? [1 = yes/0 = no]: " << endl; cin >> YesNo; 
    if (YesNo == 1) 
    { 
     PellGrant -= 750; 
    } 

    Federal = 1465; 
    if (AdjustedGross >= 19000) 
    { 
     cout << "I'm sorry, but the Work-Study Award is not available to you" << endl; 
     Federal = 0; 
    } 

    double Pelly(AdjustedGross); 

    Total = Federal + StaffordLoan + PellGrant; 

    if (Federal != 0) 
    { 
     cout << "Your Work-Study Award (if available): " << Federal << endl; 
    } 
    cout << "Your Stafford Loan award (if needed): " << StaffordLoan << endl; 
    cout << "Your Pell Grant: " << PellGrant << endl; 

    return (0); 
} 

double Pelly(double x) 
{ 
    // x is AdjustedGross 
    if ((x > 12000) && (x < 20000)) // make sure adjusted gross is bettween 12000 & 20000 
    { 
     double a = x/1000; // for every 1000 in adjusted, subtract 400 
      a *= 400; 
     x -= a; 
    } 

    if (x > 20000) // check adjusted > 20000 
    { 
     double a = x/1000; // for every 1000 in adjusted, subtract 500 
     a *= 500; 
     x -= a; 
    } 
    return x; 
} 
+5

你實際上並沒有在任何地方調用該函數。更好地閱讀一本好的C++入門書。 – juanchopanza 2014-09-29 20:10:18

回答

1

品牌:

double Pelly(AdjustedGross); 

到:

double foo = Pelly(AdjustedGross); 

存儲值從Pellydouble變量foo返回。 使用的功能Pelly向前聲明,換句話說,這樣聲明main前:

double Pelly(double); 
1

您需要實際分配功能給一個變量的結果,然後使用這個結果。所以,你應該這樣做:

double pellyResult = Pelly(AdjustedGross); 

你也應該確保你宣佈你的功能上述主:

double pellyResult(double); 
1

你的方法的簽名應該要麼是

void Pelly(double& AdjustedGross) 

即沒有回報的值(這樣,AdjustedGross通過引用傳遞並直接在函數內部修改,調用該函數將會是

Pelly(AdjustedGross); 

或您的函數調用應該是

double foo = Pelly(AdjustedGross) 

在其他的答案說。