c++ - 我的数组中 Double 之前的主要表达式?

标签 c++ arrays

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

#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);

最佳答案

首先,我将从一些错误开始,因为此代码无法编译。决不。 您在 int sum 处缺少分号第 21 行。第 23 行的 for 循环使用逗号而不是分号。您正在使用 undefined variable 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;
}

关于c++ - 我的数组中 Double 之前的主要表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19805672/

相关文章:

c - 我在标量初始值设定项中得到和多余的元素

php - 从 foreach 循环更新数据库 php 不工作

php - 如何将项目列表转换为 php 数组?

c++ - 删除使用 const_iterator 的 vector 的元素

c++ - 是否有任何 C 函数或 API 来获取在当前登录用户下运行的进程列表

c++ ofstream write_to_log.open ("relative path + array");

javascript - 如何使用 JavaScript 将长数组拆分为更小的数组

c++ - inotify 的非阻塞选择

c++ - 不同方式的动态数组声明

javascript - 类型错误 : myArr[i]. splice 不是函数