到目前为止,除了函数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;
}
答案 0 :(得分:1)
请:
double Pelly(AdjustedGross);
成:
double foo = Pelly(AdjustedGross);
将Pelly
返回的值存储在double
变量foo
中。
在函数Pelly
上使用前向声明,换句话说,在main
之前声明它:
double Pelly(double);
答案 1 :(得分:1)
您需要将函数的结果实际分配给变量,然后使用此结果。所以你应该这样做:
double pellyResult = Pelly(AdjustedGross);
您还应该确保将函数声明为main:
double pellyResult(double);
答案 2 :(得分:1)
您的方法的签名应该是
void Pelly(double& AdjustedGross)
即。根本没有返回值(这样,AdjustedGross通过引用传递并直接在函数内部修改,然后调用函数
Pelly(AdjustedGross);
或您的函数调用应该是
double foo = Pelly(AdjustedGross)
如其他答案中所述。