Answer to Question #23859 in C++ for emmanuel

Question #23859
define an enumeration called months of the year and use it to define a function which takes a month as argument and returns it as a constant string.
1
Expert's answer
2013-02-07T07:31:55-0500
#include <conio.h>
#include <iostream>

using namespace std;

enum MonthsOfTheYear
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};

const char* month_to_string(MonthsOfTheYear month)
{
static const char* month_names[12] = { "January", "February", "March", "April", "May", "June", "July",
quot;August", "September", "October", "November", "December" };
int index = (int)month;
return (index < 0 || index > 11) ? "Wrong month" : month_names[index];
}
& &
int main()
{
cout << month_to_string(March) << endl;
cout << month_to_string(November) << endl;
cout << month_to_string(July) << endl;

_getch();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS