Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What's wrong in my code when using structures with arrays?

–3 votes
asked Nov 24, 2020 by npatel300 (1,440 points)
edited Nov 25, 2020 by npatel300
#include <iostream>
#include <string>

using namespace std;

const int months = 12;
char typedef mystring[months];
int main()
{
    int const size = 12;
    struct Weatherdata
{
    double total_rainfall_month;
    int high_temp;
    int low_temp;
    double avg_temp;

};

    Weatherdata info[size];
    Weatherdata avg_monthly_rainfall, total_rainfall_month, high_temp, low_temp, avg_temp, total_rainfall_for_year, monthly_avg_temp, avg_monthly_avg_temp;
    
    string monthnames[12] = {"january", "februray", "march", "april", "may", "june", "july",
                        "august", "september", "october", "november", "december"};

    
for(int i = 0; i < 12; i++)
{
    cout<<"enter the data for month" << monthnames[i]<<endl;
    
    cout<<"enter the total rainfall:";
    cin>>info[i].total_rainfall_month;
    cout<<"enter the highest temperature of the month:";
    cin>>info[i].high_temp;
    while(info[i].high_temp < -100 || info[i].high_temp > +140)
    {
        cout<<"only accept the temperature within range of -100 and +140 degrees farenheit. enter again";
        cin>>info[i].high_temp;
    }
    cout<<"enter the lowest temperature of the month:";
    cin>>info[i].low_temp;
    while(info[i].low_temp < -100 || info[i].low_temp > +140)
    {
        cout<<"only enter the temperature within range of -100 and +140 degrees farenheit. enter again";
        cin>>info[i].low_temp;
    }
        //calculate the average temperature
        info[i].avg_temp = ((info[i].high_temp + info[i].low_temp) / 2);
        cout<<"the average temperature is"<<info[i].avg_temp<<endl;
        
        //calculate the average monthly rainfall
        avg_monthly_rainfall += info[i].total_rainfall_month;
        cout<<"the average monthly rainfall is"<< avg_monthly_rainfall<<endl;
    
}
        
        //calculate the total rainfall for the total_rainfall_for_year
        total_rainfall_for_year = ((avg_monthly_rainfall + total_rainfall_month) / 12);
        cout<<"the total rainfall for the year is"<<total_rainfall_for_year<<endl;
        
        //find the average of all monthly average temperatures
        {
            avg_monthly_avg_temp = (avg_temp / months);
            cout<<"The average of all monthly average temperature is"<<avg_monthly_avg_temp<<endl;
        }
        
        info[i].high_temp = Weatherdata_array[0];
        for(int i = 1; i < months; i++)
        {
            if(Weatherdata_array[i] > info[i].high_temp)
            info.high_temp = Weatherdata_array[i];
        }
        
    cout<<"the highest temperature of the year is"<<info.high_temp<< endl;

        info[i].low_temp = Weatherdata_array[0];
        for(int i = 1; i < months; i++)
        {
            if(Weatherdata_array[i] < info[i].low_temp)
            
            info[i].low_temp = Weatherdata_array[i];
            
         }
     cout<<"the lowest temperature of the year is"<<info.low_temp<< "and the name of month is"<< endl;

    
        
return 0;
      
}

1 Answer

0 votes
answered Nov 25, 2020 by Peter Minarik (86,580 points)
edited Nov 25, 2020 by Peter Minarik

Compile Your Code

If you compile your code, the compiler tells you what are the errors. You should at least make your code compile before you start asking questions.

Your array Weatherdata_array is not declared.

Even if it would be declared (define), you read Weatherdata_array, but you never write it. Hence the read value would be memory garbage.

You read it here:

high_temp = Weatherdata_array[0];

but the content of Weatherdata_array is uninitialized, undetermined, some random value from memory. Fill it with values before you try to read it.

If you want to initialise an array to "empty" you can do it like this:

Weatherdata Weatherdata_array[12] = { 0 };

A Better Solution

I have rewritten your code so it now works. Also, I'd like to point out that the average temperature probably has nothing to do with the the average of the minimum and maximum temperatures (min + max / 2). You can have 1 day that's cold, and 29 days that's hot in the month. The average will be close to hot, not in between the highest and lowers amount.

The whole project can be downloaded from here: https://onlinegdb.com/HyluRHjqD

Months.h

#pragma once

#include <string>

const int nMonths = 12; // Number of Months
const std::string monthNames[nMonths] = { "January", "Februray", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

WeatherData.h

#pragma once

struct WeatherData
{
    double total_rainfall;
    double high_temp;
    double low_temp;
    double avg_temp;
    
    WeatherData();
    virtual void Print() const;
};

WeatherData.cpp

#include "WeatherData.h"

#include <iostream>

WeatherData::WeatherData() :
    total_rainfall(0.0),
    high_temp(0.0),
    low_temp(0.0),
    avg_temp(0.0)
{
}

void WeatherData::Print() const
{
    std::cout << "Total rainfall: " << total_rainfall << " mm" << std::endl;
    std::cout << "Highest temperature: " << high_temp << " C" << std::endl;
    std::cout << "Lowest temperature: " << low_temp << " C" << std::endl;
    std::cout << "Average temperature: " << avg_temp << " C" << std::endl;
}

WeatherDataSummary.h

#pragma once

#include "WeatherData.h"
    
struct WeatherDataSummary : public WeatherData
{
    int high_temp_month;
    int low_temp_month;
    
    WeatherDataSummary();
    WeatherDataSummary(const WeatherData & weatherData);
    void Print() const override;
};

WeatherDataSummary.cpp

#include "WeatherDataSummary.h"

#include "Months.h"

#include <iostream>

WeatherDataSummary::WeatherDataSummary() :
    WeatherData(),
    high_temp_month(0),
    low_temp_month(0)
{
}
        
WeatherDataSummary::WeatherDataSummary(const WeatherData & weatherData)
{
    total_rainfall = weatherData.total_rainfall;
    high_temp = weatherData.high_temp;
    low_temp = weatherData.low_temp;
    avg_temp = weatherData.avg_temp;
}
        
void WeatherDataSummary::Print() const
{
    std::cout << "Total rainfall: " << total_rainfall << " mm" << std::endl;
    std::cout << "Highest temperature: " << high_temp << " C [" << monthNames[high_temp_month] << "]" << std::endl;
    std::cout << "Lowest temperature: " << low_temp << " C [" << monthNames[low_temp_month] << "]" << std::endl;
    std::cout << "Average temperature: " << avg_temp << " C" << std::endl;
}

WeatherDataStatistics.h

#pragma once

#include "Months.h"
#include "WeatherDataSummary.h"

#include <string>

class WeatherDataStatistics
{
public:
    WeatherDataStatistics();
    
    void ReadDataFromUser();
    void Calculate();
    void Print() const;

private:
    WeatherData monthlyData[nMonths];
    WeatherData monthlyAverage;
    WeatherDataSummary yearlySummary;

    double ReadValidated(const std::string & message, double min, double max) const;
    void Print(const WeatherData & data) const;
};

WeatherDataStatistics.cpp

#include "WeatherDataStatistics.h"

#include <iostream>

WeatherDataStatistics::WeatherDataStatistics() :
    monthlyData(),
    monthlyAverage(),
    yearlySummary()
{
}

double WeatherDataStatistics::ReadValidated(const std::string & message, double min, double max) const
{
    double value;
    do
    {
        std::cout << message << " ([" << min << ", " << max << "]): ";
        std::cin >> value;
    } while (value < min || value > max);
    return value;
}

void WeatherDataStatistics::ReadDataFromUser()
{
    for (int i = 0; i < nMonths; i++)
    {
        std::cout << "Enter the data for " << monthNames[i] << "." << std::endl;
        monthlyData[i].total_rainfall = ReadValidated("Enter the total rainfall", 0, 100000); // TODO: you can come up with a reasonable number here. Google search puts the highest average rainfall around 12000 m
        monthlyData[i].high_temp = ReadValidated("Enter the highest temperature", -140, 140);
        monthlyData[i].low_temp = ReadValidated("Enter the lowest temperature", -140, 140);
        monthlyData[i].avg_temp = ReadValidated("Enter the average temperature", -140, 140);
        std::cout << std::endl;
    }
}

void WeatherDataStatistics::Calculate()
{
    monthlyAverage = monthlyData[0];
    yearlySummary = monthlyData[0];
    
    for (int i = 1; i < nMonths; i++) // we've already "processed" the 1st month
    {
        yearlySummary.total_rainfall += monthlyData[i].total_rainfall;
        if (yearlySummary.high_temp < monthlyData[i].high_temp)
        {
            yearlySummary.high_temp = monthlyData[i].high_temp;
            yearlySummary.high_temp_month = i;
        }

        if (yearlySummary.low_temp > monthlyData[i].low_temp)
        {
            yearlySummary.low_temp = monthlyData[i].low_temp;
            yearlySummary.low_temp_month = i;
        }
            
        yearlySummary.avg_temp += monthlyData[i].avg_temp;

        monthlyAverage.total_rainfall += monthlyData[i].total_rainfall;
        monthlyAverage.high_temp += monthlyData[i].high_temp;
        monthlyAverage.low_temp += monthlyData[i].low_temp;
        monthlyAverage.avg_temp += monthlyData[i].avg_temp;
    }
    
    yearlySummary.avg_temp /= 12;

    monthlyAverage.total_rainfall /= 12;
    monthlyAverage.high_temp /= 12;
    monthlyAverage.low_temp /= 12;
    monthlyAverage.avg_temp /= 12;
}

void WeatherDataStatistics::Print() const
{
    std::cout << "Montly average (for a full year):" << std::endl;
    Print(monthlyAverage);
    
    std::cout << std::endl << "Yearly summary:" << std::endl;
    Print(yearlySummary);
}

void WeatherDataStatistics::Print(const WeatherData & data) const
{
    std::cout << "Total rainfall: " << data.total_rainfall << " mm" << std::endl;
    std::cout << "Highest temperature: " << data.high_temp << " C" << std::endl;
    std::cout << "Lowest temperature: " << data.low_temp << " C" << std::endl;
    std::cout << "Average temperature: " << data.avg_temp << " C" << std::endl;
}

main.cpp

#include "WeatherDataStatistics.h"

int main()
{
    WeatherDataStatistics weatherDataStatistics;
    
    weatherDataStatistics.ReadDataFromUser();
    weatherDataStatistics.Calculate();
    weatherDataStatistics.Print();

    return 0;
}
commented Nov 25, 2020 by npatel300 (1,440 points)
I had compiled the code before asking the question, but I couldn't find that error, because I got some other operator errors like no match for operator + and other errors which I am not able to fix.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...