Im trying to learn C++ and came across these questions and would love to see how
ID: 3725875 • Letter: I
Question
Im trying to learn C++ and came across these questions and would love to see how to go about it, thank you in advance for any help!
(i) Declare an enum type called Month for representing the months of the year: ( Jan, Feb, Mar, ...... , Oct, Nov, Dec).
(ii) Write a function with the prototype: void GetMonth(Month &Mth); that asks the user to enter a month represented with an integer (1..12) and sets Mth appropriately.
(iii) Write a function with the prototype: void PrintMonth(Month Mth); that prints on the screen the name of the month passed to the function.
(iv) Write a main() for testing both functions.
Explanation / Answer
#include<iostream>
using namespace std;
enum Month {January = 1 ,February = 2, March = 3, April = 4, May = 5,
June = 6, July = 7, August = 8, September = 9, October = 10,
November = 11, December = 12};
void GetMonth(Month &Mth){
int m ;
cout << "Enter a month: ";
cin >> m;
Mth = (Month) m;
}
void PrintMonth(Month Mth){
cout << "Month is ";
cout <<Mth << endl;
}
int main()
{
Month month;
GetMonth(month);
PrintMonth(month);
}