无法将数组传递给函数

时间:2019-07-13 06:34:34

标签: c++ arrays function

我应该使用一个字符串数组来输出“输入x的降雨量”行,x是一个月的名称,并将输入发送到一个单独的数组进行计算。当前,当我运行代码时,看到的是“输入1的降雨量”,“输入2的降雨量”等,而不是“输入1月的降雨量”,“输入2月的降雨量”。除此之外,该代码还需要显示总降雨量,平均降雨量以及最低和最高月份。我能够使程序输出正确的总数和平均值,但是,最高和最低月份仅输出随机数而不是月份名称。

我试图创建原型并调用函数数组,但是我认为问题可能是由于字符串数组出现问题。我尝试使用for循环,尝试将语法更改为无效。我目前在调试过程中没有任何错误,只能看到错误的输出,而不是字符串输出。

#include <iostream>
#include <string>

using namespace std;

// Function Prototypes
void getMonthlyRainfall(double[], int);
double getTotal(const double[], int);
double getHighestAmount(const double[], int);
double getLowestAmount(const double[], int);

int main()
{
    const int MONTHS = 12;
    string monthNames[MONTHS] = { "January", "February", "March", "April", 
    "May", "June", "July", "August", "September", "October", "November", 
    "December" };
     double rainfall[MONTHS], // Array
        total,
        average,
        lowestAmount,
        highestAmount;

    //Get rainfall input from user
    getMonthlyRainfall(rainfall, MONTHS);

    // Get the total amount of rain for the year
    total = getTotal(rainfall, MONTHS);

    // Get the average rainfall
    average = total / MONTHS;

    // Get the month with the lowest rainfall
    lowestAmount = getLowestAmount(rainfall, MONTHS);

    // Get the month with the highest rainfall
    highestAmount = getHighestAmount(rainfall, MONTHS);

    cout << "Total rainfall: " << total << endl;
    cout << "Average rainfall: " << average << endl;
    cout << "Least rainfall in: " << getLowestAmount << endl;
    cout << "Most rainfall in: " << getHighestAmount << endl;
    return 0;
}

void getMonthlyRainfall(double rain[], int size) 
{
    int index;
    for (index = 0; index < 12; index++)
    {
        cout << "Enter rainfall for " << (index + 1) << ": ";
        cin >> rain[index];
    }
}

double getTotal(const double numbers[], int size) 
{
    double total = 0; // Accumulator
    for (int count = 0; count < size; count++)
        total += numbers[count];
    return total;
}

double getHighestAmount(const double numbers[], int size) 
{
    double highest; // Holds highest value
    // Get array's first element
    highest = numbers[0];
    // Step through array
    for (int count = 0; count < size; count++) 
    {
        if (numbers[count] > highest)
            highest = numbers[count];
    }
    return highest;
}

double getLowestAmount(const double numbers[], int size) 
{
    double lowest; // Holds lowest value
    // Get array's first element
    lowest = numbers[0];
    // Step through array
    for (int count = 0; count < size; count++)
    {
        if (numbers[count] < lowest)
            lowest = numbers[count];
    }
    return lowest;
}

正如我所说,第一个输出应该说出一个月的实际名称,并且应该井井有条。例如,提示应首先要求用户输入一月份的总降雨量,然后用户输入一个数字。然后提示继续,要求用户输入2月的数字,依此类推,直到12月。相反,我看到提示要求用户输入总降雨量为“ 1”,用户输入一个数字,然后提示要求用户输入降雨量为“ 2”,直到达到12。该程序进行计算并输出正确的总数平均值,但是当应该输出“降雨量最高(或最低)的月份:(月份名称)”时,它会给我一个随机数字,例如01201686。

总而言之,字符串数组输出月份名称,而用户输入存储在单独的数组中进行计算。这些计算是针对总计和平均值输出的,但是需要将降雨量总计与具有相应实体的月份进行比较,然后针对最高和最低的输出必须是字符串而不是数字。

1 个答案:

答案 0 :(得分:1)

这是对名称的简单混淆。您拥有函数的名称(将以“随机”数字打印),而不是您正在使用的变量的名称。

cout << "Least rainfall in: " << getLowestAmount << endl;

应该是

cout << "Least rainfall in: " << lowestAmount << endl;

关于您的第一个问题,请更改

cout << "Enter rainfall for " << (index + 1) << ": ";

cout << "Enter rainfall for " << monthNames[index] << ": ";

很明显,第一个版本打印一个数字(index + 1是一个数字)。要获取月份名称,您必须使用数字作为月份名称数组的索引。

要执行此操作,您还需要在monthNames函数中使用getMonthlyRainfall(目前仅在main中可见)。您应该像这样将monthNames传递给getMonthlyRainfall函数

void getMonthlyRainfall(double[], string[], int);

//Get rainfall input from user
getMonthlyRainfall(rainfall, monthNames, MONTHS);

void getMonthlyRainfall(double rain[], string monthNames[], int size) 
{
    ...

编辑

因此,要同时输出最低的每月降雨量和降雨量最小的月份名称,您应该更改getLowestAmount函数以返回降雨量最小的月份的 index 。您还应该更改此函数的名称,因为它现在正在执行与原始函数不同的操作,并且旧名称不能准确描述新函数,但为清楚起见,我将其保留不变。您可以稍后再确定一个新名称。

// this function returns a month index, not a rainfall amount,
// so it's return type is int not double
int getLowestAmount(const double[], int);

这是更新的功能

int getLowestAmount(const double numbers[], int size) 
{
    double lowest; // Holds lowest value
    int lowestIndex; // Holds the index of the lowest value
    // Get array's first element
    lowest = numbers[0];
    lowestIndex = 0;
    // Step through array
    for (int count = 0; count < size; count++)
    {
        if (numbers[count] < lowest)
        {
            lowest = numbers[count];
            lowestIndex = count;
        }
    }
    return lowestIndex;
}

除了我添加了lowestIndex之外,该函数是相同的,这就是我返回的值。

现在在main中,您可以使用最低索引来打印两个要查看的值

// Get the month with the lowest rainfall
lowestIndex = getLowestAmount(rainfall, MONTHS);

...

cout << "Least rainfall in: " << monthNames[lowestIndex] << 
    " is " << rainfall[lowestIndex] << endl;