【问题标题】:How would you call data from main and pass it to a function? (C++)您将如何从 main 调用数据并将其传递给函数? (C++)
【发布时间】:2013-10-29 11:34:56
【问题描述】:

所以我无法从主函数(包含您输入的变量的数组)调用数据,并且不知道如何将其传递给 float getTotal 函数。这是我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(float [], int )
   {
     double total = 0;
     for (int i=1; i<ARRAYSIZE; i++)
        {
           total += inputs[i];
        }
     cout << "The total rainfall for the year is " << total << "inches." << endl;
     return total;
   }

float getAverage(float [], int)
   {
     //code goes here
   }

int main()
   {
     const int ARRAYSIZE = 13;
     int inputs[ARRAYSIZE], i=1;
     do
        {
           cout << "Enter the rainfall (in inches) for month #" << i << ": ";
           cin >> inputs[i];
           if ( inputs[i] < 0 )
              {
                  cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                  cin >> inputs[i];
              }
           i++;
        }
     while (i < 13);

     float getTotal(float inputs[], int ARRAYSIZE);
     float getAverage(float inputs[], int ARRAYSIZE);
   }

所以我想从 main 调用数组数据并计算 getTotal 部分中的总数。我尝试了各种方法,都没有奏效。

【问题讨论】:

  • 您遇到了哪些问题,在main 中填充数组,或将数组传递给函数?
  • 如果可能,请使用 std::vector 而不是数组。现在,您尝试在getTotal 中使用ARRAYSIZE,但它是main 的本地对象,因此在getTotal 中不可见。您还想为参数命名,以便可以在 getTotal 中访问它们。
  • @上面我在将数组传递给函数时遇到问题,所以我可以在getTotal函数中计算总数@Jeffry,你能给我一个例子吗?

标签: c++ arrays function methods


【解决方案1】:
float getTotal(float inputs[], int ARRAYSIZE)
   {
     double total = 0;
     for (int i=1; i<ARRAYSIZE; i++)
        {
           total += inputs[i];
        }
     cout << "The total rainfall for the year is " << total << "inches." << endl;
     return total;
   }

float getAverage(float inputs[], int ARRAYSIZE)
   {
     //code goes here
   }

int main()
   {
     const int ARRAYSIZE = 13;
     int inputs[ARRAYSIZE], i=1;
     do
        {
           cout << "Enter the rainfall (in inches) for month #" << i << ": ";
           cin >> inputs[i];
           if ( inputs[i] < 0 )
              {
                  cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                  cin >> inputs[i];
              }
           i++;
        }
     while (i < 13);

     float fTotal = getTotal(inputs, ARRAYSIZE);
     float fAverage = getAverage(inputs, ARRAYSIZE);
   }

【讨论】:

  • 这是我的新代码和更新代码,也会粘贴我得到的错误。我不确定为什么会这样?代码:puu.sh/4VqJv.png 我得到的错误可以在这个 puu.sh 链接上找到。 puu.sh/4VqHi.png
  • 编辑:错误在这里,更新并修复了一个小错误。 puu.sh/4VqNI.png
  • 您没有在函数 getTotal 中声明 i。您的错误是因为您不了解范围。这是一个快速的reference。一般规则是变量只在 { } 块内可见,因此其他函数不能使用主函数块中声明的任何变量。
【解决方案2】:

我们的源代码中有很多错误。例如“int 输入[ARRAYSIZE]”。你的阵列 是 int 数据类型,但您将此数组作为浮点数据类型传递。有一个编译器
错误。其次,您应该在主函数之外声明函数及其数据类型,但是
你在主函数里面做 浮动getTotal(浮动输入[],int ARRAYSIZE); 还有另一个编译器错误。调用函数时不需要指定数据 参数类型。所以我写了一个可能的代码,希望对你有帮助。

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(int *inputs, int ARRAYSIZE )
  {
   double total = 0;
     for (int i=1; i<=ARRAYSIZE; i++)
      {
        total += inputs[i];
      }
      cout << "The total rainfall for the year is " << total << "inches." << endl;
  return total;

}

 float getAverage(int *inputs, int ARRAYSIZE)
    {
     //code goes here

    return 1.4;
   }

 int main()
    {
     const int ARRAYSIZE = 3;
     int inputs[ARRAYSIZE], i=1;
     do
       {
       cout << "Enter the rainfall (in inches) for month #" << i << ": ";
       cin >> inputs[i];
       if ( inputs[i] < 0 )
          {
             cout << "Please enter a non-negative number for rainfall in month " << i     
                 << " :";
              cin >> inputs[i];
          }
       i++;
    }
 while (i <= 3);

     getTotal(inputs, ARRAYSIZE);
     getAverage(inputs, ARRAYSIZE);

return 0;

}

     While calling function, the return value you can store in variable but data type of   

      variable should be the same of return type of the function 

      e.g  float a = getTotal(inputs, ARRAYSIZE);
     float b = getAverage(inputs, ARRAYSIZE);
      or you can directly call this function in cout output command statement.like 

cout

【讨论】:

  • 嗯,除了将参数 1 从 int[13] 转换为 float[] 之外,我一切正常。有什么帮助吗?
  • 我认为您需要查看此链接。希望它对您有用link
【解决方案3】:

替换你的函数声明:

float getTotal(float [], int )
float getAverage(float [], int)

到这里

float getTotal(int* inputs, int ARRAYSIZE)
float getAverage(int* inputs, int ARRAYSIZE)

而 main() 函数中的这一行:

float getTotal(float inputs[], int ARRAYSIZE);
float getAverage(float inputs[], int ARRAYSIZE);

到这里

float getTotal(inputs, ARRAYSIZE);
float getAverage(inputs, ARRAYSIZE);

【讨论】:

  • 我做了,出错了,所以我用这个 float fTotal = getTotal(inputs, ARRAYSIZE); 更改了代码浮动 fAverage = getAverage(输入,ARRAYSIZE);现在出现“无法将参数 1 从 int[13] 转换为 float[] 错误。
  • 对不起,我已经编辑了我的答案,如果编译器仍然吐出错误,请告诉我。我目前没有编译器:-(
  • 没问题!!我很感激帮助!它似乎有工作,但我必须在确认之前修复以前的错误。我现在得到的错误是错误 C2078: too many initializers Only error left this is on line float getTotal(inputs, ARRAYSIZE);
  • 用int替换函数声明中的浮点数;我(再次)更新了我上面的答案:-)
【解决方案4】:

嗨@user2901840 对代码稍作修改就可以正常运行:

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(float floatarray[], int size)
{
    float total = 0.0f;
    for (int i=0; i<size; i++)
    {
        total += floatarray[i];
    }
    return total;
}

float getAverage(float floatarray[], int size)
   {
        return (getTotal(floatarray, size))/size;
   }

int main()
   {
        int ARRAYSIZE = 12, i=0;
        float inputs[ARRAYSIZE];
        do
        {
            cout << "Enter the rainfall (in inches) for month #" << i << ": ";
            cin >> inputs[i];
            if ( inputs[i] < 0.0 )
                {
                    cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                    cin >> inputs[i];
                }
            i++;
        }
        while (i < ARRAYSIZE);
        cout << "\n\nThe total rainfall for the year is " << getTotal(inputs, ARRAYSIZE) << " inches." << endl;
        cout << "The average rainfall per month during the year was: " << getAverage(inputs, ARRAYSIZE) << " inches."<< endl;
   }

您需要正确地预定义类,并且需要修改调用。为了澄清我的代码: - 重新编号 i 值以匹配常见的 C++ 约定(对于 12 个值运行 0 到 11) - 重新利用函数以最小化代码 - 将数组重新声明为浮点数组(您最初将其作为整数数组)

希望这会有所帮助:)

如果您需要更多细节或解释,请告诉我,我可以填写更多信息。

【讨论】:

    猜你喜欢
    • 2022-08-03
    • 2011-12-14
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2020-12-29
    • 1970-01-01
    相关资源
    最近更新 更多