简体   繁体   English

将数组传递给函数时遇到问题

[英]Trouble with passing array to a function

I am supposed to use a string array to output the line "Enter rainfall for x", x being the name of a month, and having the input be sent to a separate array for calculations. 我应该使用一个字符串数组来输出“输入x的降雨量”行,x是一个月的名称,并将输入发送到单独的数组进行计算。 Currently, when I run my code I am seeing "Enter rainfall for 1", "Enter rainfall for 2", etc. instead of "Enter rainfall for January", "Enter rainfall for February". 当前,当我运行代码时,看到的是“输入1的降雨量”,“输入2的降雨量”等,而不是“输入1月的降雨量”,“输入2月的降雨量”。 Aside from this, the code also needs to show the total, average, and lowest and highest months of rainfall. 除此之外,该代码还需要显示总降雨量,平均降雨量以及最低和最高月份。 I am able to have the program output the correct total and average however, the highest and lowest month just outputs a random number instead of a month name. 我能够使程序输出正确的总数和平均值,但是,最高和最低月份仅输出一个随机数而不是月份名称。

I have attempted to create prototypes and call an array to a function but I think the issue might be contributed to the fact that I am having issues with my string array. 我试图创建原型并调用函数数组,但是我认为该问题可能是由于字符串数组出现问题而引起的。 I have tried using a for loop, I've tried changing my syntax to no avail. 我尝试使用for循环,尝试将语法更改为无效。 I don't currently get any errors in the debugging process only see incorrect output instead of a string output. 我目前在调试过程中没有收到任何错误,只能看到错误的输出,而不是字符串输出。

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

As I stated the first output should say the actual name of a month and it should be in order. 如我所说,第一个输出应该说出一个月的实际名称,并且应该井井有条。 For instance, the prompt should first ask the user to input total rainfall for the month of January, the user inputs a number. 例如,提示应首先要求用户输入一月份的总降雨量,然后用户输入一个数字。 The prompt then goes on to ask the user input a number for February, so on and so forth until December. 然后提示继续,要求用户输入2月的数字,依此类推,直到12月。 I am instead seeing the prompt ask the user to input total rainfall for "1", user inputs a number, the prompt then asks user to input rainfall for "2" until it gets to 12. The program does the calculations and outputs correct total and average but when it is supposed to output "Month with highest (or lowest) rainfall: (Month name)" it instead gives me a random number such as 01201686. 相反,我看到提示要求用户输入总降雨量为“ 1”,用户输入一个数字,然后提示要求用户输入降雨量为“ 2”,直到达到12。该程序进行计算并输出正确的总数平均值,但是当应该输出“降雨量最高(或最低)的月份:(月份名称)”时,它却给了我一个随机数字,例如01201686。

So to sum up, the string array outputs month name and the user input is stored in a separate array for calculations. 综上所述,字符串数组输出月份名称,而用户输入存储在单独的数组中进行计算。 Those calculations are outputted for total and average but the rainfall totals need to be compared to the month with the corresponding entity and the output for highest and lowest then needs to be a string not a number. 这些计算是针对总计和平均值输出的,但是需要将降雨量总计与具有相应实体的月份进行比较,然后针对最高和最低的输出必须是字符串而不是数字。

It's a simple confusion over names. 这是对名称的简单混淆。 You have the name of the function (which will print as a 'random' number) instead of the name of the variable you are using. 您具有函数的名称(将以“随机”数字打印)而不是您所使用的变量的名称。

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

should be 应该

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

As for your first quesiton, change 至于你的第一个问题,改变

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

to

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

Obviously the first version prints a number ( index + 1 is a number). 显然,第一个版本打印一个数字( index + 1是一个数字)。 To get the month name you have to use the number as an index to your array of month names. 要获得月份名称,您必须使用数字作为月份名称数组的索引。

To make this work you also will need to make monthNames available in the getMonthlyRainfall function (at the moment it's only visible in main ). 为了使这项工作有效,您还需要在getMonthlyRainfall函数中使monthNames可用(目前仅在main可见)。 You should pass monthNames to the getMonthlyRainfall function like this 您应该像这样将monthNames传递给getMonthlyRainfall函数。

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

and

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

and

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

EDIT 编辑

So to output both lowest monthly rainfall and the name of the month with the lowest rainfall you should change your getLowestAmount function to return the index of the month with the lowest rainfall. 因此,要输出最低的每月降雨量和降雨量最小的月份的名称,您应该更改getLowestAmount函数以返回降雨量最小的月份的索引 You should also change the name of this function because it's now doing something different from the original function and the old name does not accurately describe the new function, but for clarity I'll leave it the same. 您还应该更改此函数的名称,因为它现在正在执行与原始函数不同的操作,并且旧名称不能准确描述新函数,但为清楚起见,我将其保留不变。 You can decide on a new name later. 您可以稍后再确定一个新名称。

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

Here's the updated function 这是更新的功能

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;
}

See the function is the same except I've added lowestIndex and that's the value I return. 看到函数是一样的,除了我添加了lowestIndex ,这就是我返回的值。

Now in main you can use the lowest index to print both values that you want to see 现在,在main ,您可以用最低的指数来打印你想看到这两个值

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

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM