variable of enums as an output

Hi. I'm having some troubles trying to convert my date from this format (ex.) 2/2/2015 into February 2, 2015. I'm suppose to change the month 2 into its month name from the enumeration. Is it right to use array to input the month, day, and year?


#include <iostream>
using namespace std;

enum monthType {JANUARY = 1, FEBRUARY, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};

int main()
{
monthType monthName;
char month[2], day[2], year[5];

cout<<"Enter the date (mm/dd/yyyy): ";
cin.get(month, '/');
cin.get(day, '/');
cin.get(year, 5);

monthName = static_cast<monthType>(month[]);

cout<<monthName;
return 0;
}
Last edited on
Enumeration are just a good way of using named constants instead of nondescript numbers. They are not names or something, nor they exist in compiled code.

You can use simple string array for that:
1
2
3
4
std::string months[] = {"Zeroember", "January", /*...*/ "December"};
int month;
//fill month
std::cout << months[month];
Ahhh... so in otherwords the identifiers of the enumeration are just to "pretty" up the integer values then?
Yes, to abstract from magic numbers and to give you some type safety (mostly for scoped enums)
Thanks for the clarification! Can't believe I spent too much time thinking and going around about the code haha.
Topic archived. No new replies allowed.