Answer to Question #342081 in C# for Siyanda

Question #342081

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.


1
Expert's answer
2022-05-17T16:05:47-0400



#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;
}

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
New on Blog
APPROVED BY CLIENTS