调用函数的问题,它接受三个参数,一个是int const

时间:2015-01-19 05:25:06

标签: c++

我发帖是因为我在弄清楚为什么我的"总援助可用"不打印pell补助金,斯塔福德贷款和勤工俭学贷款的总额。我曾经一次又一次地尝试修复我的功能(我在网上和参考书中使用过来源,但我不知道问题是我的功能是否会被调用,因为没有什么是打印提供全面的援助。

其他一切都很好,除了那一件事,它真的让我烦恼,因为无论我做出什么改变,我都处于同一状态。没有错误显示。我第一次使用microsoft visual studio作为编译器,所以我想知道这是不是问题。

这就是我所拥有的:

#include <iostream>
using namespace std;

double pell_Grant(double); // forward declaration on pell_Grant ( which is a       function for calculating pell grant)

double sum(double, int const, double); // declaration for sum function which   gives total for the total aid available

int main()
{

double workstudy = 0, pellgrant = 5730, grossincome = 0, Total = 0; //  variables 
int yes;
int const stafford = 9500; //const declaration
cout << "Lemme Forecast Your FAFSA" << endl;

cout << "Enter your adjusted gross income: " << endl; cin >> grossincome; //  input for gross income

if (grossincome >= 30000) // if gross income is higher than 30,000 then print message
{
    cout << "Sorry Charlie, your income is too high to run this  forecaster!";
    return 0;
}

cout << "Can someone claim you as a dependent? [1=yes/0=no]: " << endl; // input to claim dependent or not
cin >> yes;
if (yes == 1) // if 1 for yes is selected then pell grant gets reduced by 750, if 0 or no selected, then program goes by standard procedure
{
    pellgrant -= 750;
}

workstudy = 1465; // work study must be nationwide avergae 1,465
if (grossincome >= 19000) // if this condition is met then work study is not met and message is printed as follows...
{
    cout << "Your Work-Study Award is not available for your income level" << endl;
    workstudy = 0;
}

double foo = pell_Grant(grossincome); // value returned from pell_Grant stored here to give total

Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total

if (workstudy != 0) // if work study loan isn't more than 19,000 then it will be calculated and printed in next statement
{
    cout << "Your Work-Study Award (if available)= " << workstudy << endl;
}
cout << "Your Stafford Loan award (if needed)= " << stafford << endl; // prints stafford loan (const called)
cout << "Your Pell Grant= " << pellgrant << endl; // prints pell grant
cout << "Total Aid Available For You=" << Total << endl; // prints total

return (0);
}


double  pell_Grant(double x) // pell_Grant function that calculates pell grant which is assigned 5,730 
{
// x is gross income which is assigned 5,730. This is money received that does not need to be repaid.  
 if ((x > 12000) && (x < 20000)) // statement makes sure adjusted gross is  bettween 12000 & 20000
 {
    double a = x / 1000; // for every 1,000 in adjusted gross income... reduce/subtract 400 from it
        a *= 400;
    x -= a;
 }

if (x > 20000) // check if gross income is more than 20000
{
    double  a = x / 1000; // for every 1,000 in gross income, subtract 500
    a *= 500;
    x -= a;
}
return x;
}


double sum(double workstudy , int const stafford, double pellgrant) // function for adding up workstudy loan, stafford loan, and pellgrant loan
{
double Total;

Total = workstudy + stafford + pellgrant; 

return (Total); // returns total
} 

2 个答案:

答案 0 :(得分:2)

根据其声明,方法sum()接受3个参数。

double sum(double, int const, double);

但是在打电话给你的时候只传递了一个参数:

Total = sum(workstudy + stafford + pellgrant);

相反,您需要传递3个参数,如下所示:

Total = sum(workstudy, stafford, pellgrant);

但是,我不明白为什么你没有收到任何错误!您正在尝试调用不存在的函数。您必须得到编译器错误。

答案 1 :(得分:0)

您正在错误地调用sum()函数。这是你的代码:

Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total

但是你的sum()函数有三个参数。调用该函数的正确形式是:

Total = sum(workstudy, stafford, pellgrant); // sum function is called and stores result in Total