C++ - 将结构类型的数组传递给用户定义的函数



我是Stack Overflow的新手,所以如果我违反了任何规则或错误地提出了问题,请原谅我(我也希望了解如何正确提出关于堆栈溢出的问题的建议(。

无论如何,所以我收到此错误

*"类型'data *'的参数与类型'data '的参数不兼容

当我在 int main(( 中调用用户定义的函数 weather 时。如果有人可以查看我的代码并指导我找到导致错误的原因,我将不胜感激。正如您所知,我目前正在学习编程基础知识入门课程,我还没有学习课程和指针。

谢谢!

#include <iostream>
#include <string>

using namespace std;
const int SIZE = 2;
void weather(struct data array[], int SIZE, string months[]);
int main()
{
string months[12] = { "January", "February", "March", "April", "May", "June"
"July", "August", "September", "October", "November", "December" };

struct data {
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};
data annualWeather[SIZE]; 
//Asks user for total rainfall, high and low temperature, and calculates
//avg temperature of each month
for (int i = 0; i < SIZE; i++)
{
cout << "What was the total rainfall in " << months[i]
<< "?" << endl;
cin >> annualWeather[i].totalRainfall;
//Prompts user for temperature high
do
{
cout << "What was the temperature high in " << months[i]
<< "?" << endl;
cin >> annualWeather[i].highTemp;
} while (annualWeather[i].highTemp > 140);
//Prompts user for temperature low
do
{
cout << "What was the temperature low in " << months[i]
<< "?" << endl;
cin >> annualWeather[i].lowTemp;
} while (annualWeather[i].lowTemp < -100);
annualWeather[i].avgTemp = (annualWeather[i].highTemp + annualWeather[i].lowTemp) / 2;
}
**weather(annualWeather, SIZE, months); <--THIS IS WHERE I SEE ERROR**
return 0;
}
void weather(struct data array[], int SIZE, string months[])
{
double avgRainFall = 0;
double totalAnnualRainFall = 0;
double annualHigh = 0;
double annualLow = 0;
double avgMonthlyTemp = 0;
//Calculates sum annual rain fall, total annual rain fall, and total average monthly temperature
for (int i = 0; i < SIZE; i++)
{
avgRainFall += annualWeather[i].totalRainfall;
totalAnnualRainFall += annualWeather[i].totalRainfall;
avgMonthlyTemp += annualWeather[i].avgTemp;
}
//Calculates average annual rain fall and average annual monthly temperature
avgRainFall = avgRainFall / SIZE;
avgMonthlyTemp = avgMonthlyTemp / SIZE;
//Selection Sort of annualWeather[].highTemp
for (int index = 0; index < SIZE - 1; index++)
{
//Find location of smallest element
int smallestIndex = index;
for (int location = index + 1; location < SIZE; location++)
if (annualWeather[location].highTemp < annualWeather[smallestIndex].highTemp)
smallestIndex = location;
//Swap smallest element of annualWeather[].highTemp to front
int tempSales = annualWeather[smallestIndex].highTemp;
annualWeather[smallestIndex].highTemp = annualWeather[index].highTemp;
annualWeather[index].highTemp = tempSales;
//Swap smallest element of months[] to front
string tempMonths = months[smallestIndex];
months[smallestIndex] = months[index];
months[index] = tempMonths;
}
//Displays average monthly rainfall, total annual rainfall, annual high, annual low,
//And average of the monthly temperature averages
cout << "The average monthly rainfall is " << avgRainFall << "." << endl
<< "The total rainfall for the year is " << totalAnnualRainFall << "."
<< endl << "The high temperature of the year is " << annualHigh << " during "
<< months[0] << endl << "The low temperature of the year is " << annualLow << " during "
<< months[2] << endl << "The average temperature of all the monthly averages is "
<< avgMonthlyTemp << ".";
cin.get(); cin.get();
}

main函数中声明的struct dataweather函数原型中使用的struct data不同

struct data的定义从main移到全局命名空间,在使用它声明weather之前的其他位置。

首先,请在全局空间中指定您的个人数据类型。 您的问题不是发送数组以运行。函数内部的问题 您使用名称数组向它们发送数组,并在内部尝试从 Main 按名称访问它们。只需重命名函数参数即可。

void weather(Data array[], int SIZE, string months[])
{
double avgRainFall = 0;
double totalAnnualRainFall = 0;
double annualHigh = 0;
double annualLow = 0;
double avgMonthlyTemp = 0;
//Calculates sum annual rain fall, total annual rain fall, and total average monthly temperature
for (int i = 0; i < SIZE; i++)
{
avgRainFall += array[i].totalRainfall;
totalAnnualRainFall += array[i].totalRainfall;
avgMonthlyTemp += array[i].avgTemp;
}
//Calculates average annual rain fall and average annual monthly temperature
avgRainFall = avgRainFall / SIZE;
avgMonthlyTemp = avgMonthlyTemp / SIZE;
//Selection Sort of annualWeather[].highTemp
for (int index = 0; index < SIZE - 1; index++)
{
//Find location of smallest element
int smallestIndex = index;
for (int location = index + 1; location < SIZE; location++)
if (array[location].highTemp < array[smallestIndex].highTemp)
smallestIndex = location;
//Swap smallest element of annualWeather[].highTemp to front
int tempSales = array[smallestIndex].highTemp;
array[smallestIndex].highTemp = array[index].highTemp;
array[index].highTemp = tempSales;
//Swap smallest element of months[] to front
string tempMonths = months[smallestIndex];
months[smallestIndex] = months[index];
months[index] = tempMonths;
}
//Displays average monthly rainfall, total annual rainfall, annual high, annual low,
//And average of the monthly temperature averages
cout << "The average monthly rainfall is " << avgRainFall << "." << endl
<< "The total rainfall for the year is " << totalAnnualRainFall << "."
<< endl << "The high temperature of the year is " << annualHigh << " during "
<< months[0] << endl << "The low temperature of the year is " << annualLow << " during "
<< months[2] << endl << "The average temperature of all the monthly averages is "
<< avgMonthlyTemp << ".";
cin.get(); cin.get();
}

最新更新