2014-01-16 17 views
0

我现在正在使用我的类中的结构,并且要求我们创建一个具有列出的四个变量的结构,并允许用户输入每个我们记录和查找的数字总年降雨量,每月平均值,最高和最低总体(及其发生)。我的主要问题是从我的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

首先,您的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 
}