#include <iostream>
enumclass day
{
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,
};
char get_char(day d)
{
switch (d)
{
case day::monday:
return'M';
case day::tuesday:
return'T';
case day::wednesday:
return'W';
case day::thursday:
return'X';
case day::friday:
return'F';
case day::saturday:
return'S';
case day::sunday:
return'Y';
}
return'?';
}
int main()
{
day d = day::monday;
char c = get_char(d);
std::cout << "Character of the day: " << c << '\n';
}
enum days
{
monday = 'M', // the char m is an integer in c++.
tuesday = 'T',
…
};
getchar() becomes
ans = (char)(monday);
but this breaks the enum's sequential values if you relied upon that, or wanted to use them as array index, or something, then you can't use this trick.
if you wanted to have distinct T's for tu/th you can do this (even more mad hax)..
tuesday = 'T'*10+1,
thursday = 'T'*10+2,
ans=(char) (anyday/10); //make the integer math work for you. They still won't be sequential or array friendly enums.