将数组传递给函数时出现问题
Trouble with passing array to a 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;
}
正如我所说,第一个输出应该说一个月的实际名称,并且应该按顺序排列。例如,提示应首先要求用户输入 1 月份的总降雨量,用户输入一个数字。然后,提示继续要求用户输入 2 月的数字,依此类推,直到 12 月。相反,我看到提示要求用户输入"1"的总降雨量,用户输入一个数字,然后提示要求用户输入"2"的降雨量,直到达到 12。该程序进行计算并输出正确的总数和平均值,但是当它应该输出"降雨量最高(或最低)的月份:(月份名称)"时,它会给我一个随机数,例如01201686。
综上所述,字符串数组输出月份名称,用户输入存储在单独的数组中进行计算。这些计算是针对总量和平均值输出的,但需要将降雨总量与具有相应实体的月份进行比较,并且最高和最低的输出需要是一个字符串而不是一个数字。
这是对名称的简单混淆。您拥有函数的名称(将打印为"随机"数字),而不是您正在使用的变量的名称。
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
是一个数字)。要获取月份名称,您必须使用该数字作为月份名称数组的索引。
要完成这项工作,您还需要在getMonthlyRainfall
函数中提供monthNames
(目前它仅在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
函数以返回降雨量最低的月份的指数。您还应该更改此函数的名称,因为它现在正在执行与原始函数不同的事情,并且旧名称不能准确描述新函数,但为了清楚起见,我将保持不变。您可以稍后决定新名称。
// 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;
- Visual Studio中的函数声明和函数定义问题
- C++quit()函数中可能存在作用域问题
- 类似于strcat()的函数出现问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 类C++中的函数问题(LNK2019和LNK1120错误)
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 关于简单C++函数(is_palindrome)的逻辑的问题
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- constexpr构造函数需要常量成员函数时出现问题
- 添加存储在向量中的大整数的函数出现问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 关于 c++ 函数中指针赋值的简单问题
- 如何在标头中声明(或定义)函数的问题
- 有关 c++ 构造函数的问题
- 模板类转换问题 - 无法推断调用的函数
- 从 C++ 中的函数返回数组地址问题
- 关于复制构造函数的一个棘手问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题