Enumerated datatype : Months and seasons

Write a program to display the months falling in a particular season ? Use enumerated list to store 12 months. Program should continue till user inputs N.
Spring - March to June; Summer - June to September; Autumn - September to December; and, Winter - December to March


I have done till the step given below. How should I proceed further. Please give hint or tell what should I do next to display months? Also, how to ensure that program continues until user inputs N. Please help. I am a beginner in c++.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main() 
{
enum month{Jan,Feb,Mar,April,May,Jun,July,Aug,Sep,Oct,Nov,Dec};
month M;


return 0;
}
Last edited on
I admit I'm not sure I understand your assignment's requirements. It sounds like you are only asked to do a simple lookup? Or, is there any user input at all?

Enums start at zero by default; you want the first month to start at one:

    month { Jan=1,Feb,Mar,... };

Hope this helps.
... display the months falling in a particular season ...

using scoped enums with std::underlying_type since a scoped enum is not implictly convertible to its integer value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <type_traits>

enum class Seasons {Spring = 1, Summer, Autumn, Winter};

template<typename T>
std::ostream& operator <<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
{
    if (static_cast<typename std::underlying_type<T>::type>(e) == 1)
    {
         stream << "The spring months are: " << "Mar " << "Apr " << "May " << "Jun \n";
    }
    else if (static_cast<typename std::underlying_type<T>::type>(e) == 2)
    {
         stream << "The summer months are: " << "Jun " << "Jul " << "Aug " << "Sep \n";
    }
    else if (static_cast<typename std::underlying_type<T>::type>(e) == 3)
    {
        stream << "The autumn months are: " << "Sep " << "Oct " << "Nov " << "Dec \n";
    }
    else if (static_cast<typename std::underlying_type<T>::type>(e) == 4)
    {
         stream << "The winter months are: " << "Dec " << "Jan " << "Feb " << "Mar \n";
    }
    else
    {
        stream << "incorrect entry \n";
    }
    return stream;
}

int main()
{
    std::cout << Seasons::Spring;
    std::cout << Seasons::Winter;
}

//http://coliru.stacked-crooked.com/a/a3ac0cd92528808d
Thanks GunnerFunner for your help
could also use helper type std::underlying_type_t<> to avoid typing the entire typename et al every time:
http://coliru.stacked-crooked.com/a/2b8916612f0c78f3
I'm not convinced this is a correct answer to OP's question. It appears that he is being taught to use enum as a substitute for const/constexpr values.

The whole point of an enum is that you aren't supposed to care what the actual value is. The only time you should care is during serialization.

[edit] I don't see how the original assignment is supposed to teach this...
Last edited on
Maybe like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

int main()
{
  enum month { Jan, Feb, Mar, April, May, Jun, July, Aug, Sep, Oct, Nov, Dec };
  string season;
  while (true)
  {
    cout << "Enter season: ";
    cin >> season;

    if (season == "N")
      break;
    else if (season == "Spring")
    {
      // show the spring months
    }
    else if (season == "Sommer")
    {
      // show the sommer months
    }
    else if (season == "Autum")
    {
      // show the autum months
    }
    else if (season == "Winter")
    {
      // show the winter months
    }
  }
  return 0;
}
Topic archived. No new replies allowed.