In: Computer Science
Create a strongly typed enumerator for the days of the week, and put it in a header file enum.h
Create a test program with a switch statement that has a function that prints the day word based on numeric input from the user.
Utilize the local predefined variable __func__ to display the name of the current function.
Student Learning Outcomes:
Create and utilize strongly typed enumerators
Correctly implement scope resolution for strongly typed enumerators
Cast variable types for proper comparisons
Display the current function’s name
STYLE NOTE: enumerators are constant, so many people think that the entire enumerator value (e.g. sunday) should be in all caps. Others follow the rule that since the variable type is created by the programmer, it should start with an uppercase letter. You may choose either one, but be consistent. I happen to use the first as it gives a clear visual indicator that it is a special variable.
Program for the given question:
1) Creating a header file containing strongly typed enum. Create a file enum.h and write the line below it and save it in same folder where your c program is saved which is going to use this header file
enum.h
//creating the strongly typed enum
enum class daysOfWeek{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURADY };
---------********---------
2) Now, we write the code for main program which is going to use this header file
weeks.cpp
// Weeks.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include "enum.h"
using namespace std;
//execution of program starts from here
int main()
{
//variable to store user input
int user_input = 0;
cout << "Days Numbering are as follow\n";
cout << "1: Sunday\n2: Monady\n3: Tuesday\n4: Wednesday\n5: Thursday\n6: Friday\n 7: Saturday\n";
count << "Enter the day number you want to print : ";
//ask for user input
cin >> user_input;
//switch statement to print date based upon user input
switch (user_input-1)
{
case (int)daysOfWeek::SUNDAY:
printf("Today is Sunday");
printf("Called function is: %s\n", __func__);
break;
case (int)daysOfWeek::MONDAY:
printf("Today is Monday\n");
printf("Called function is: %s\n", __func__);
break;
case (int)daysOfWeek::TUESDAY:
printf("Today is Tuesday\n");
printf("Called function is: %s\n", __func__);
break;
case (int)daysOfWeek::WEDNESDAY:
printf("Today is Wednesday\n");
printf("Called function is: %s\n", __func__);
break;
case (int)daysOfWeek::THURSDAY:
printf("Today is Thursday\n");
printf("Called function is: %s\n", __func__);
break;
case (int)daysOfWeek::FRIDAY:
printf("Today is Friday\n");
printf("Called function is: %s\n", __func__);
break;
case (int)daysOfWeek::SATURADY:
printf("Today is Saturday\n");
printf("Called function is: %s\n", __func__);
break;
default:
printf("Wrong Input, try agian!\n");
break;
}
}
----------******---------
Note : As there is only one function i.e "main()", the __func__ will going to print the name of this function each time.
And we have to cast the strongly typed enum to int so that it can be used for matching inside switch statement.
----------******----------
Output for the above program: