mercredi 22 janvier 2020

Not displaying lowest temperature month number and value of lowest temperature Only a garbage value is shown

Weather Statistics Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures. Input Validation: Only accept temperatures within the range between –100 and +140 degrees Fahrenheit.

#include <iostream>
#include<string>
using namespace std;
struct weather_statistics
{
    long int Yearly_Total_Temperature;
    long int Monthly_Temperature;
    long int Monthly_Rainfall;
    long int Total_Rainfall;
    long int High_Temperature;
    long int Low_Temperature;
    long int Average_Temperature;
};
int main()
{
    const int size = 12;
    weather_statistics rainfall;
    weather_statistics temperature;
    weather_statistics data[size];
    rainfall.Total_Rainfall = 0;
    temperature.Yearly_Total_Temperature = 0;
    int lowest = 0;
    int highest = 0;
    temperature.High_Temperature = data[0].Monthly_Temperature;
    temperature.Low_Temperature = data[0].Monthly_Temperature;
    for (int i = 0; i < size; i++)
    {
        cout << "Enter the total rainfall for " << (i + 1) << " month\t";
        cin >> data[i].Monthly_Rainfall;
        rainfall.Total_Rainfall = rainfall.Total_Rainfall + data[i].Monthly_Rainfall;
        cout << "Enter the temperature(between minus 100 and 140 degrees Fahrenheit) for " << (i + 1) << " month\t";
        cin >> data[i].Monthly_Temperature;
        while (((data[i].Monthly_Temperature) < (-100)) || ((data[i].Monthly_Temperature)>(140)))
        {
            cout << "ERROR! Enter the temperature between range of -100-140 farenheit\n\n";
            cin >> data[i].Monthly_Temperature;
        }
        cout << "\n\n-----------------------------------------------------------------------------------\n";
        temperature.Yearly_Total_Temperature = temperature.Yearly_Total_Temperature + data[i].Monthly_Temperature;
        if (data[i].Monthly_Temperature < temperature.Low_Temperature)
        {
            temperature.Low_Temperature = data[i].Monthly_Temperature;
            lowest = (i+1);
        }
        if (data[i].Monthly_Temperature > temperature.High_Temperature)
        {
            temperature.High_Temperature = data[i].Monthly_Temperature;
            highest = (i+1);
        }
    }
    temperature.Average_Temperature = temperature.Yearly_Total_Temperature / 12;

        cout << "The lowest temperature of the year is in month "<<lowest<<" and is "
            << temperature.Low_Temperature << endl;
        cout << "The highest temperature of the year is in month "<<highest<<" and is "
            << temperature.High_Temperature << endl;

    cout << "The total rainfall for the complete year is ='" << rainfall.Total_Rainfall << "'\n";
    cout << "The average monthly temperature is =" << temperature.Average_Temperature << endl;
}

Aucun commentaire:

Enregistrer un commentaire