0

我真的很困惑。我必须为一堂课制作这个实验室,我似乎无法让搜索只显示一个结果,而是一年中的所有月份。当我在一年中的月份输入 0 时,我似乎也无法弄清楚为什么它不显示 TotalRainfall。谢谢你。

#include <iostream>
#include <fstream>

const int MaxSize = 12; //How many weather lines will be available.

using namespace std;

struct WeatherInformation
{
    int Month;                            //Months of the year
    float TotalMonthsRainfall;            //Total amount of rainfall
    float HighTemp;                       //The Highest temperature of the month.
    float LowTemp;                        //The Lowest temperature of the month.
    float AverageTemp;                    //The Average temperature of the month.
};

WeatherInformation WeatherArray[MaxSize]; //Declaring a month array of MaxSize

void ReadFile(ifstream& MyinFile, WeatherInformation WeatherArray[]);
void WeatherMonthSearch (WeatherInformation WeatherArray[]);


int main()
{
    float TotalRainfall = 0;
    int count = 1;          //Counts how many times the for loop goes.
    int MonthOfWeather;     //User input of the month.
    char ProgramRedo;       //User input if they want to reuse the program.
    char exit_char;         //User input to exit the program.
    ifstream MyinFile;      //Variable that uses file.

    ReadFile (MyinFile, WeatherArray);       //Call ReadFile Function
    WeatherMonthSearch (WeatherArray);       //Call WeatherMonthSearch Function

    MyinFile.close(); //Closes file.
}
//Brett Holmes
//4/30/2013
//PreCondition:You need a file labeled weather.dat
//PostCondition: It puts the file variables into an array.
void ReadFile(ifstream& MyinFile, WeatherInformation WeatherArray[])
{
    float TotalRainfall = 0;
    char exit_char;
    int count = 0;
    int Month = 0;

    cout << "Your Weather Machine" << endl << endl;
    MyinFile.open("weather.dat");
    if (!MyinFile)
    {    //no
        cout << "Can't open input file." << endl; //Tests the right file.
        char exit_char;                         //End Program
        cout << "Press any key to exit" << endl;
        cin >> exit_char;
    }
    for(count = 1; count < MaxSize; count++) //Puts the file variables in the array.
    {
        WeatherArray[count].Month = WeatherArray[count].Month + 1;
        MyinFile >> WeatherArray[count].TotalMonthsRainfall;
        MyinFile >> WeatherArray[count].HighTemp;
        MyinFile >> WeatherArray[count].LowTemp;
        (WeatherArray[count].AverageTemp = ((WeatherArray[count].HighTemp + WeatherArray[count].LowTemp)/2));
        (TotalRainfall = TotalRainfall + WeatherArray[count].TotalMonthsRainfall);
    }
}

//Brett Holmes
//4/30/13
//PreCondition:You need to have the months already put into an array in a struct.
//PostCondition:Outputs the rainfall stats the user puts in then asks to run again.
//Outputs a error message if they type in the month wrong.
void WeatherMonthSearch (WeatherInformation WeatherArray[])
{
    float TotalRainfall;
    int months;
    int MonthOfWeather;
    char ProgramRedo;
    do
    {
        bool MonthFound = false;

        cout << "Please input the number of the Month. Ex. 1=Jan. 2=Feb. etc \n\n";
        cin >> MonthOfWeather;

        for(int i = 1; i <= MaxSize; i++)
        {
            months = WeatherArray[i].Month;
            if(months == MonthOfWeather ) //Finds the artist and outputs the results
            {
                cout << "\nTotal Months Rainfall: " << WeatherArray[i].TotalMonthsRainfall << "   \n";
                cout << "Highest Temperature: " << WeatherArray[i].HighTemp << "   \n";
                cout << "Lowest Temperature: " << WeatherArray[i].LowTemp << "   \n";
                cout << "Average Temperature: " << WeatherArray[i].AverageTemp << "   \n";
                MonthOfWeather = true;
            }
        }
        if(MonthOfWeather == 0)
        {
            cout << "The total rainfall for the year is: " << TotalRainfall << ".";
        }
        if(MonthFound == false)
        {
            cout << "\nMonth Number error. Month not found. Try again.\n\n";
            MonthOfWeather = false;
        }
        cout << "Would you like to look up another month of weather?\n";
        cout << "Enter a 'Y' if yes and 'N' if no.\n";
        cin >> ProgramRedo;
    }while(ProgramRedo == 'Y');
}
4

2 回答 2

1

几个明显的问题:

  1. C++ 中的数组是从 0 开始的,所以你的 for 循环是一对一的。在您的搜索功能中,for(int i = 1; i <= MaxSize; i++)应该是for(int i = 0; i < MaxSize; i++). 同样,在您的读取函数中,for(count = 1; count < MaxSize; count++)应该是for(count = 0; count < MaxSize; count++)(如果您想跳过索引 0,因为您将其用作信号值,那么您应该设置MaxSize为 13 并让循环从 1 开始。)

  2. 你为什么要分配一个布尔值MonthOfWeather?你的意思是MonthFound

  3. 您阅读的功能未正确设置月份。WeatherArray[count].Month = WeatherArray[count].Month + 1;应该是WeatherArray[count].Month = count;如果您使用的是基于 1 的循环,或者WeatherArray[count].Month = count + 1;该循环是基于 0 的。

  4. 您在读取函数中计算了总降雨量,但结果存储在局部变量中,因此在读取完成时会丢失。要么将 TotalRainfall 设为全局变量,要么在搜索函数中进行计算。

  5. 有很多多余的变量定义:例如,您的天气数据数组是全局的,因此没有理由实际传递它;exit_char在您的读取函数中声明了两次;main()您从未使用过的声明变量的前五行。

此外,您的读取功能实际上不会在失败时退出程序 - 它甚至仍会尝试从流中读取,然后调用您的搜索功能!如果需要进行错误检查,您应该让 read 函数返回一个布尔值并在调用搜索函数之前检查 read 函数是否成功,或者只是std::exit在之后调用cin >> exit_char;

于 2013-09-10T00:20:27.303 回答
0

因此,您遇到的一个问题是您的局部变量出现在多个位置,但看起来您希望它们实际上包含相同的信息。

例如,我看到三个不同的TotalRainFall. 一个 in main,就在那里,不用于任何东西,一个 inReadFile是计算的,一个 in WeatherMonthSearch,它没有设置为任何东西。

我怀疑你想让这三个人真正做点什么。实现这一点的一种方法是删除ReadFileand中的本地WeatherMonthSearch,而是将其中的一个从main(作为对 的引用ReadFile)传入。

还有一些地方你使用变量而不初始化它们。养成初始化一切和无处不在的习惯!

在你的编译器中启用警告——如果你有任何形式或相当新的编译器(gcc 或最近的 MS Visual Studio),它应该至少告诉你其中一些事情。

于 2013-09-10T00:08:47.247 回答