1

它一直问我在 Double 之前的主要表达式,它在我的 int 数组中,我不知道该怎么做。我明天有期中考试啊,这伤害了我的大脑。所有其他输入都会有帮助谢谢:)

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

void get_input(double array[50], int& sizearray1)
{
    cout << "How many doubles do you wish to add?" << endl;
    cin >> sizearray1;
    while(sizearray1 < 1 || sizearray1 > 50) {
        cout << "Error: That is an invalid number! You must enter a value between 1 and 50.\nHow many doubles do you wish to add?" << endl;
        cin >> sizearray1;
    }
    for(int n=1;n<sizearray1;n++){
        cout << "Enter Double " << n << endl;
        cin >> array[n];
    }
}

double calcSum(double array[50],int sizearray1)
{
    int sum
cout << "The sum is ";
for(n=1,n<sizearray1,n++){
    sum += array[n]
}
cout << sum << endl;
}


void printArray(double c[],int sizearray1)
{
    cout << "The array contains:\n";

    for(int n=1;n<sizearray1;n++){
            cout << setprecision(2) << showpoint << fixed  << c[n] << "       ";
    }
    calcSum(double array[50],int sizearray1);
}


int main()
{
    double array1[50];
    int sizearray1 = 0;
    get_input(double array[50],int sizearray1);
    printArray(double array[50],int sizearray1);
4

2 回答 2

1

首先,我将从一些错误开始,因为此代码无法编译。没门。您在int sum第 21 行缺少分号。您在第 23 行的 for 循环使用逗号而不是分号。你n = 1在同一个循环中使用了一个未定义的变量,你必须在这样做之前声明它for(n=1,n<sizearray1,n++)。再次在第 24 行的同一个 for 循环中,您忘记了另一个分号sum += array[n]

回到你的问题:你不能给函数一个类型参数,你实际上会尝试在那里声明一个变量。另外请不要声明这样的函数,double calcSum (double array[50], int sizearray1)因为这真的不是你想要的......改用它double calcSum (double array[/*Nothing here*/], int sizearray1)

请检查您的整个代码并修复所有错误。这是我的一个修复。当然不行,你的代码有点乱,检查所有的函数并给它们正确的参数。

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

void get_input (double array[], int& sizearray1)
{
    cout << "How many doubles do you wish to add?" << endl;
    cin >> sizearray1;
    while (sizearray1 < 1 || sizearray1 > 50)
    {
        cout << "Error: That is an invalid number! You must enter a value between 1 and 50.\nHow many doubles do you wish to add?" << endl;
        cin >> sizearray1;
    }
    for (int n = 1; n < sizearray1; n++)
    {
        cout << "Enter Double " << n << endl;
        cin >> array[n];
    }
}

double calcSum (double array[], int sizearray1)
{
    int sum;
    cout << "The sum is ";
    for (int n = 1; n < sizearray1; n++)
    {
        sum += array[n];
    }
    cout << sum << endl;
}


void printArray (double c[], int sizearray1)
{
    cout << "The array contains:\n";

    for (int n = 1; n < sizearray1; n++)
    {
        cout << setprecision(2) << showpoint << fixed  << c[n] << "       ";
    }
    calcSum (array[50], sizearray1); //This still can't work, the variable "array" has not been declared
}


int main()
{
    double array1[50];
    int sizearray1 = 0;
    get_input (array[50], sizearray1); //Again "array" has not been declared
    printArray (array[50], sizearray1); //Again "array" has not been declared
    return EXIT_SUCCESS;
}
于 2013-11-06T07:13:06.290 回答
0

调用函数时不要包含以下类型:

calcSum(double array[50],int sizearray1);

但只需使用变量名:

calcSum(array[50],sizearray1);

与您的get_input()printArray()功能相同。

在您的printArray()函数中,我认为您正在将传递的数组接收到变量c中。所以应该是:

calcSum(c[50],sizearray1);

而且我认为您正在尝试传递整个数组,然后您只需要使用数组名:

calcSum(c,sizearray1);

c[50]只会传递数组中第 50 位的元素。

于 2013-11-06T06:47:32.600 回答