简体   繁体   中英

Using enum and struct?

I have a question about using enum and struct on my homework that I'm having a little trouble understanding.

First, here is the code sample that our teacher gave us.

enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
struct WeatherType
{    
    int avgHiTemp;
    int avgLoTemp;
    float actualRain;
    float recordRain;
};

The homework then says to declare a one-dimensional array type, WeatherListType , of WeatherType components, to be indexed by values of type MonthType . When I tried to do this by coding:

typedef WeatherType WeatherListType[MonthType];

I got these errors:

problem5.cpp:19:47: error: expected primary-expression before ']' token 
typedef WeatherType WeatherListType[MonthType];
                                           ^
problem5.cpp:21:2: error: 'WeatherListType' was not declared in this scope
WeatherListType yearlyWeather;

I'm just confused as to what the question is asking, and if I'm on the right track, what exactly am I doing wrong?

The problem here is that in

typedef WeatherType WeatherListType[MonthType];

the thing between the brackets [] has to be a number (it's the number of elements) but MonthType isn't a number, it's a type.

You have two main options here:

  1. Just use 12.

  2. Add another enum item: enum MonthType {..., NOV, DEC, NUM_MONTHS};

    and then use NUM_MONTHS.

    This is a common trick with enums - because they start at 0 and count upwards, NUM_MONTHS will be the 13th value, so it gets the value 12.

    The reason you'd do this is so that if you add another value, the compiler will automatically adjust NUM_MONTHS for you. I don't think we're likely to get new months any time soon, so in this case it doesn't matter.

(Side note: Arrays are always indexed by integers in C. You can't index one by an enum. But enums are convertible to integers and back, so it's not a problem)

Here i have given the completer program for your clarification in that i didnot mention the input values i haven given in the output because for 12 months each month 4 data totally 48 data has to be given. Assume that the values are given.but while you are running you have to give all 48 values.where you use JAN it is conatant 0 ,in FEB it is constant 1 and so on for DEC it is constant 11.hope u understand and this will be useful for you

#include<iostream>
using namespace std;
enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
struct WeatherType
 {    
    int avgHiTemp;
    int avgLoTemp;
    float actualRain;
    float recordRain;
 };
int main()
 {
     WeatherType WeatherListType[12];
     for (int i=JAN;i<=DEC;i++)
       { 
          cout<<"\n High temperature :";
          cin>>WeatherListType[i].avgHiTemp;
          cout<<"\n Low temperature :";
          cin>>WeatherListType[i].avgLoTemp;
          cout<<"\n Actual Rain :";
          cin>>WeatherListType[i].actualRain;
          cout<<"\n Record Rain :";
          cin>>WeatherListType[i].recordRain;
      }
     cout<<"\n Month \t HiTemp\t LoTemp\t ActualRain\t RecordRain\n";

    for (int i=JAN;i<=DEC;i++)
      {
        cout<<i<<"\t"<<WeatherListType[i].avgHiTemp;
        cout<<"\t"<<WeatherListType[i].avgLoTemp;
        cout<<"\t"<<WeatherListType[i].actualRain;
        cout<<"\t"<<WeatherListType[i].recordRain<<"\n";
   }
}

  OUTPUT

  High temperature :40 

  Low temperature :28                                                                                                          

  Actual Rain :34                                                                                                              

  Record Rain :56 
  .
  .
  .
 Month HiTemp  LoTemp  ActualRain  RecordRain                                                                                   
 0       40      28      34          56                                                                                            
 1       45      32      45          67             
 2       34      23      56          76                                                                                            
 3       34      32      45          43                                                                                            
 4       67      12      45          43                                                                                            
 5       78      65      45          32                                                                                            
 6       56      54      34          23                                                                                            
 7       45      34      23          23                                                                                            
 8       3       1       45          43                                                                                            
 9       34      45      23          23                                                                                            
 10      12      3       34          43                                                                                            
  11      56      54      43         34   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM