我正在使用我班级的结构,在此我们要求创建一个列出四个变量的结构,并允许用户为我们记录的每个变量输入数字,并找出每年的总降雨量,平均值月份,最高和最低总体(及其出现次数)。我的主要问题是从我的getHot函数的getInfo函数中获取数据,以找到具有高温的月份。
#include <iostream>
using namespace std;
//weather structure
struct WeatherData
{
double rainfall;
double highestTemperature;
double lowestTemperature;
double averageTemperature;
};
//prototypes
WeatherData getInfo();
double getHot(WeatherData *);
double getCold();
int main()
{
WeatherData seattle;
enum monthName { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
double totalRainfall = 0;
double maxTemperature;
double minTemperature;
double averageTemperature;
seattle = getInfo();
for (int index = JANUARY; index <= DECEMBER; index++)
{
totalRainfall += seattle.rainfall;
}
seattle.averageTemperature = (totalRainfall) / 12;
getHot(& seattle);
system("pause");
return 0;
}
// My getInfo function records all the information from user
WeatherData getInfo()
{
WeatherData tempData;
double totalRainfall = 0;
enum monthName { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
for (int index = JANUARY; index <= DECEMBER; index++)
{
cout << "Enter the total rainfall in month " << (index+1) << " (greater than or equal to zero) : " << endl;
cin >> tempData.rainfall;
cout << "rainfall " << tempData.rainfall;
while (tempData.rainfall < 0)
{
cout << "This is an invalid input, enter a number equal to or greater than zero : " << endl;
cin >> tempData.rainfall;
}
cout << "Enter the highest temperature in month " << (index + 1) << " (less than or equal to 140) : " << endl;
cin >> tempData.highestTemperature;
while (tempData.highestTemperature > 140)
{
cout << "This is an invalid input, enter a number equal to or less than 140 : " << endl;
cin >> tempData.highestTemperature;
}
cout << "highest " << tempData.highestTemperature;
cout << "Enter the lowest temperature in month " << (index + 1) << " (greater than or equal to -100) : " << endl;
cin >> tempData.lowestTemperature;
while (tempData.lowestTemperature < -100)
{
cout << "This is an invalid input, enter a number equal to or greater than -100 : " << endl;
cin >> tempData.lowestTemperature;
}
cout << "lowest " << tempData.lowestTemperature;
totalRainfall += tempData.rainfall;
}
tempData.averageTemperature = (tempData.lowestTemperature + tempData.highestTemperature) / 2;
return tempData;
}
//having difficulty here figuring out how to get the contents of getInfo and return it to the main
double getHot(WeatherData * getInfo)
{
double highTemp = 0;
for (int index = 0; index <= 12; index++)
{
if (getInfo->highestTemperature > highTemp)
highTemp = getInfo->highestTemperature;
}
//return highTemp to function
cout << " The maximum temperature is : " << highTemp << endl;
return highTemp;
}
答案 0 :(得分:0)
首先,你的getInfo方法并没有记录所有12个月的数据:它会一直覆盖它们。
因此,您需要更改您的西雅图变量,以便它可以保存所有12个月的数据,如下所示:
WeatherData seattle[12];
...
GetInfo(&seattle);
....
void GetInfo(WeatherData* theData){
...
for (int index = JANUARY; index <= DECEMBER; index++)
{
cout << "Enter the total rainfall in month " << (index+1) << " (greater than or equal to zero) : " << endl;
cin >> theData[index].rainfall;
(etc)
修复getHot:
double getHot(WeatherData * getInfo)
{
double highTemp = 0;
double highIndex = -1;
for (int index = 0; index <= 12; index++)
{
if (getInfo->highestTemperature > highTemp) {
highTemp = getInfo->highestTemperature;
highIndex = index;
}
}
cout << " The maximum temperature is : " << getInfo[highIndex].highTemp << endl;
cout << " the hottest month is: " << highIndex << endl;
cout << " The rainfall in the hottest month is: " << getInfo[highIndex].rainfall << endl;
return highTemp; //if you still want to return highTemp
}