I was wondering what would be better:
An enumeration declaration or a string array:
enum MonthName{January, February, March, April, May, June, ...)
or
string MonthName[12] = {"January", "February", "March", "April", "May", "June", ...)
because to me it doesn’t seem like it matters.
This variable will only be accessed by one function, to display month based on the number input.
If you want to display the name of the month, an
enumwon’t do you any good at all. The identifiers in an enumeration type definition are visible only to the compiler; you can’t print them at run time.You need the array of strings (or perhaps an array of
const char*s).The names of the months are also available via the standard library, using the
strftime()function declared in<ctime>(from the C standard library’s<time.h>header). Getting the month name for a given month number usingstrftime()is not straightforward (you’d have to build astruct tmobject first). It does have the advantage of using month names for the current locale rather than being hardwired to English.And don’t forget that array indices start at 0; December is at index 11, not 12.