For the assignment I'm currently working on, I have defined an enum class for differing class semesters.
enum class Semester {
FALL,
SPRING,
SUMMER
};
In my overloaded <<
operator, I am to print out the lowercase string equivalent of Semester
type. I've done so by creating a map with Semester
keys and std::string
pairs as such:
std::map<Semester, std::string> to_string
{
{Semester::FALL, "fall"},
{Semester::SPRING, "spring"},
{Semester::SUMMER, "summer"}
};
std::stream& operator <<(std::stream& ost, enum Semester semester)
{
return ost << ::to_string[semester];
}
Already unsure if the previous solution was optimal, I moved onto the next task which was defining Semester load_semester(std::istream& ist)
. This function receives input from the user as std::string
(figuring I'd use std::getline()
) and converts it to Semester
type. I imagine I could use a map similar to before albeit reversed, but I was curious what a 'proper' way to approach this problem would be.
EDIT: I appreciate the feedback! I will be implementing functions as @casey suggested.
I am implementing enum classes for both semesters and sections (i.e. MATH, READING, SCIENCE, etc.) I will possibly be required to implement more in the future. Could I implement these functions in such a way that I could reuse them?