Explore Enums
To get started with structuring data, explore working with an enumeration.
Days
Enumerations allow you to capture a list of options within a program. To test this out let’s create an enumeration to represent days of the week. We can call this enumeration day
, and it can have options for Monday, Tuesday, etc.
To test this out create a new C++ program and declare within this the new enumeration. For the moment, we need to declare the new type and use it to create a variable in main
.
Complete the following code by declaring the day
enumeration, and ensuring it has a value for each day. Remember that these values are like constants, so in C/C++ our coding convention would be to use UPPER_CASE. We can make use of our utilities to help with reading details from the user.
Converting to string
At the moment the output is less than ideal, as we can only have the numeric value of the enum. To change this we can develop a function to convert an enum value to a string.
Function | to string |
Parameters | d : The day to convert |
Returns | A string representation of the day to convert |
Description | Convert the day (d) to a string. |
We can implement to_string
using a switch statement, with one branch per value in the enumeration.
Naming this to_string
also means main
will not need to be changed. When you compile and run this it should output the day of the week, rather than the enum’s raw integer value.
While we are using this here to convert an enum value to a string, the same style of logic can be used elsewhere in your code. With the enum you know the potential range of values, so these will work well in conditional logic.
What about invalid day?
Each enumeration is implemented as an integer value, this means it is possible that the value does not match to one of the enum values. As a result you must always make sure to cover the case where your value does not match the expected options.
Reading data from the user
To wrap up this test, we can also look at converting data in the other direction. This function can be called to read a day from the user. This will display the list of days, and their values allowing the user to select the day they are after.
Function | read day |
Parameters | prompt : The message to display when reading the day |
Returns | The day entered by the user |
Description | Displays the prompt, and list of days for the user to select from. Data input is validated, and the selected day value is returned. |
The following is a start of the main
to test this, with read_day
being called to populate the today variable.
When implementing read_day
, you can use a loop to simplify printing each of the days manually. Inside the loop you can convert the integer to the enum value and then convert that to a string. This way you could apply the same style of logic to enumerations with any number of options.
Have a go at implementing this, and then test the ability to read and write enumeration values using the main below.
With these functions implemented you have seen the different ways of working with enumeration values. These provide convenient types for cases where a value should be selected from a list of options.
Test it out
Enumerations are fairly simple types, but they can be useful in helping make your code more readable. At this stage you could have a quick look at the mouse_input.h file that is part of the SplashKit library. Near the top of this code you will find the mouse_button
enumeration. Notice how this helps document the different buttons that are supported by the SplashKit library.