为什么函数原型中的参数数量与定义中的参数不匹配?

时间:2015-06-23 19:36:51

标签: c++

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string> 
using namespace std;

//Function Prototypes
void getInfo(string, double, int, int);
double getAverage(double, int);
void displayTable(string, double, int, int, double);

int main()
{
int i;
int size;
double average, sum;
size = 10;
double income[9];
int members[9];
string names[9];
getInfo(names[i], income[i], members[i], size);
getAverage(income, size, average, sum, i);
displayTable(names, income, members, size, average, i);
}

//getInfo function definition

void getInfo(string names[], double income[], int members[], int size,   int i)
{
    cout << "Please enter the name of the Head of Household, the total income, and the number of household members";
    for (i = 0; i < 10; i++)
        cin >> names[i] >> income[i] >> members[i]; 
}

//getAverage function definition

double getAverage(double income[], int size, double average, double sum, int i)
{
    sum=0;
    for (i = 0; i < 10; i++)
        sum += income[i]; 
        average = sum / size;
}

//displayTable function definition

void displayTable(string names[], double income[], int members[], int size, int i, double average)
{
    cout << "Household Names" << setw(20) << "Annual Income" << setw(20) << "Household Members" << endl;
    cout << "-------------------------------------------------------------------------------------------------------------------------" << endl;
    for (i = 0; i < 10; i++)
        cout << names[i] << setw(20) << setprecision(2) << income[i] << setw(20) << members[i] << endl;
    cout << "Average Income" << setw(20) << average << endl;
}

对不起,这是一个混乱/基本的代码。我只是一个初学者,而且我正在进行一个州外的在线课程,所以除了互联网之外,我没有其他帮助。 我收到以下错误消息:“getAverage函数不接受5个参数”和“displayTable不接受6个参数”。我正在使用c ++,我已经在MS Visual Studio中编写了代码。感谢那些选择帮助的人

2 个答案:

答案 0 :(得分:1)

void getInfo(string, double, int, int);
double getAverage(double, int);
void displayTable(string, double, int, int, double);

原型应该与下面的函数定义具有相同的参数,就像在main()中一样,它假定您正在调用原型中声明的函数。

应该是:

double getAverage(double[], int, double, double, int);
void displayTable(string[], double[], int[], int, int, double);

答案 1 :(得分:0)

我认为您的问题是您已将变量i添加到所有定义中。该变量应该是这样的本地:

void getInfo(string names[], double income[], int members[], int size)  // Removed int i
{
    int i;  // Added int i
    cout << "Please enter the name of the Head of Household, the total income, and the number of household members";
    for (i = 0; i < 10; i++)
        cin >> names[i] >> income[i] >> members[i]; 
}

对displayTable执行相同操作,即不传递变量i,但将其设为本地。

编辑: 仔细查看代码,我发现getAverage有更多问题。它应该是:

double getAverage(double income[], int size)  // Remove double average, double sum, int i
{
    double average;  // Make them local variables
    double sum;
    int i;

    sum=0;
    for (i = 0; i < 10; i++)
        sum += income[i]; 
    average = sum / size;

     return average;   // Remember to return the result.
}

注意return语句。在c ++(和c)中,函数可以返回0或1个值。关键字void表示0返回值,您不需要return语句。函数前的任何其他关键字表示1返回值,您的代码必须执行:

return valueOfCorrectType;

在功能结束时。

P.S。如果函数需要更新多个变量,则必须传递引用或指针。但那是下一步。