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.

why I am getting this error in my code? error: invalid types ‘main()::Weatherdata [12][double]’ for array subscript.

+2 votes
asked Nov 26, 2020 by npatel300 (1,440 points)
#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;
    double high_temp;
    double low_temp;
    double avg_temp;

};

    Weatherdata info[size];
    double 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[months].total_rainfall_month;
 
        //calculate the total rainfall for the total_rainfall_for_year
        
        total_rainfall_for_year = ((avg_monthly_rainfall + total_rainfall_month) / 12);

        //find the average of all monthly average temperatures
        
            avg_monthly_avg_temp += avg_temp;

        
        //Find the highest and lowest temperatures for the year
        double highest = info[0].high_temp;
        double highest_temp = 0;
        for(int i = 1; i < months; i++)
        {
            if(info[i].high_temp > highest)
            {
                highest = info[i].high_temp;
            }

        }
        
        double lowest = info[0].low_temp;
        double lowest_temp = 0;
        for(int i = 1; i < months; i++)
        {
            if(info[i].low_temp < lowest)
            {
                lowest = info[i].low_temp;
            }

         }

   //Display the weather statistics for a year
    cout<<"the average monthly rainfall is"<< avg_monthly_rainfall<<endl;
    cout<<"the total rainfall for the year is"<<total_rainfall_for_year<<endl;
    cout<<"The average of all monthly average temperature is"<<avg_monthly_avg_temp<<endl;
    cout<<"The highest temperature of the year is"<<info[highest_temp].high_temp<<endl;
    cout<<"The lowest temperature of the year is"<<info[lowest_temp].low_temp<<endl;

        
        
return 0;
      
}

1 Answer

0 votes
answered Nov 26, 2020 by Peter Minarik (86,040 points)

Compile the code

Compilation failed due to following error(s).

main.cpp: In function ‘int main()’:
main.cpp:93:70: error: invalid types ‘main()::Weatherdata [12][double]’ for array subscript
     cout<<"The highest temperature of the year is"<<info[highest_temp].high_temp<<endl;
                                                                      ^
main.cpp:94:68: error: invalid types ‘main()::Weatherdata [12][double]’ for array subscript
     cout<<"The lowest temperature of the year is"<<info[lowest_temp].low_temp<<endl;
                                                                    ^

Understand the error message

The compiler tells you exactly where the problem is (do you see the ^ sign?). Also, it's explained in words: "invalid types for array subscript". The problem for the compiler is that you are trying to index an array (info) with a double  (lowest_temp). Since the items in an array can be only on non-negative integral numbers (0, 1, 2, 3, etc) trying to index with a floating point number (e.g. 1.234) makes absolutely no sense, so the compiler raises an error.

What you should have done instead is to save the index of the month with the lowest temperature, and use this index to access elements in info.

You can look at my proposed solution in your previous post (http://question.onlinegdb.com/8702/whats-wrong-in-my-code-when-using-structures-with-arrays?show=8704#a8704). That is just one way how to do things, but it gives you some idea who certain things can be achieved.

commented Nov 26, 2020 by npatel300 (1,440 points)
Ok thanks, I figured out where was the mistake and now I have fixed it and it works.
commented Nov 27, 2020 by Peter Minarik (86,040 points)
Great! :) I'm glad you're sorted.
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.
...