> string print_month(int month)
> {
> string result;
> switch (month)
> {
> case 1:
> result = "January";
> break;
> case 2:
> result = "February";
> break;
> case 3:
> result = "March";
> break;
> case 4:
> result = "April";
> break;
> case 5:
> result = "May";
> break;
> case 6:
> result = "June";
> break;
> case 7:
> result = "July";
> break;
> case 8:
> result = "August";
> break;
> case 9:
> result = "September";
> break;
> case 10:
> result = "October";
> break;
> case 11:
> result = "November";
> break;
> case 12:
> result = "December";
> break;
> default:
> cout << "Invalid Value!\a";
> break;
> }
> return result;
You can shorten this by creating an array of constant strings and
return an indexed copy of the contents:
string print_month(int month) // base index=1 !!
{
const char* results[]={
"January", "Feb", "Mar", "Ap", "M", "Jn",
"Jl", "A", "S", "O", N", "D"
};
return string(results[month-1]);
}
Also, this function should be called "get_month_name" instead of
"print_month", since it doesn't actually print something
Bye,
-Gernot