Create an enumeration named Month that holds values for the months of the year, starting with
January equal to 1. Write a program that prompts the user for a month integer. Convert the
user’s entry to a Month value, and display it.
#include <iostream>
#include <string>
using namespace std;
//create a enum type for Month
enum Month {
Jan=1,
Feb,
Mar,
Apr,
May,
June,
July,
Aug,
Sept,
Oct,
Nov,
Dec
};
Month convertFromInt(int numMon)
{
switch(numMon)
{
case 1: return Month::Jan;
case 2:return Month::Feb;
case 3:return Month::Mar;
case 4:return Month::Apr;
case 5:return Month::May;
case 6:return Month::June;
case 7:return Month::July;
case 8:return Month::Aug;
case 9:return Month::Sept;
case 10:return Month::Oct;
case 11:return Month::Nov;
case 12:return Month::Dec;
default:return Month::Jan;
}
}
string convertToString(Month m)
{
switch(m)
{
case Month::Jan:return "Jan.";
case Month::Feb:return "Feb.";
case Month::Mar:return "Mar.";
case Month::Apr:return "Apr.";
case Month::May:return "May.";
case Month::June:return "June.";
case Month::July:return "July.";
case Month::Aug:return "Aug.";
case Month::Sept:return "Sept.";
case Month::Oct:return "Oct.";
case Month::Nov:return "Nov.";
case Month::Dec:return "Dec.";
}
}
int main()
{
cout<<"Please input number of month: ";
int num;
cin>>num;
Month m=convertFromInt(num);
cout<<convertToString(m)<<endl;
return 0;
}
Comments
Leave a comment