函数调用错误
Function Call Error
到目前为止,除了函数 Pelly 之外,我的代码都正常工作。它不会像预期的那样返回调整后的毛额。 我非常确定数学是正确的,我认为问题在于函数的调用方式。我不太擅长功能。任何帮助将不胜感激。
#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;
}
制作:
double Pelly(AdjustedGross);
到:
double foo = Pelly(AdjustedGross);
将从Pelly
返回的值存储在double
变量foo
中。在函数Pelly
上使用前向声明,换句话说,在main
之前这样声明它:
double Pelly(double);
您需要实际将函数的结果分配给变量,然后使用此结果。所以你应该做这样的事情:
double pellyResult = Pelly(AdjustedGross);
您还应该确保在 main 上方声明您的函数:
double pellyResult(double);
方法的签名应该是
void Pelly(double& AdjustedGross)
即根本没有返回值(这样,AdjustedGross 通过引用传递并直接在函数内部修改,然后调用函数将是
Pelly(AdjustedGross);
或者你的函数调用应该是
double foo = Pelly(AdjustedGross)
如其他答案所述。
相关文章:
- 错误:调用'getline'没有匹配函数
- 使用 std::find 时没有匹配的函数调用错误
- 错误:调用"es_queue::set_rpc_vector(std::vector >&, std::__cxx11::string)"没有匹配函数
- 使用成员初始值设定项列表时没有匹配的函数调用错误
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 错误:调用'strcmp'没有匹配函数(尝试设置显示用户信息功能)
- 第 5 行:字符 54:错误:调用"min(int,std::__cxx11::basic_string<char>::size_type)"没有匹配函数
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:调用'function namel'没有匹配函数
- C ++ DLL导入:函数调用返回错误的结果
- 函数调用中出现意外编译错误 (C++)
- C++错误:调用"print_size"没有匹配函数
- 为什么将"<<"运算符作为函数调用会导致"ambiguous call"编译器错误?
- 使用 boost::thread 运行函子,但析构函数被错误地调用
- C ++尝试访问映射中的元素会给我一个不匹配的函数调用错误
- getHighest 函数调用在产生 0 的C++会导致错误
- 错误:调用'begin(long double [nPoints])'没有匹配函数;使用硬编码的 int 与整数变量初始化向量
- 错误:函数调用中有两个参数
- 错误:函数调用中从int到int(*)[4]的转换无效
- 错误 - 函数调用不正确