// Access read_integer and read_string in utilities
* Genre data captures the list of genre's captured in the software.
// Get the number of genres. Cast the last
// one to an integer and add one as it start from 0
const int GENRE_COUNT = (int)NONFICTION + 1;
* Convert a genre to a string
* @param genre the data to convert
string to_string(genre_data genre)
return "science fiction";
// While we expect the above, it can be broken
// when we cast from an integer...
* Read in a genre value from the user.
* @param prompt a message to prompt the user with
genre_data read_genre(string prompt)
// Create a book variable, and initialise it
// Loop through the integers that relate to the genre
// Output as the list of options to choose from
for(raw = 0; raw < GENRE_COUNT; raw++)
// convert the raw integer value to a genre_data
// and then conver that to a string
write_line(to_string(raw + 1) + ": " + to_string((genre_data)raw));
// Read in the integer value of the genre
// - user enters 1 to GENRE_COUNT. So -1 needed.
// - provide a range if you can
raw = read_integer(prompt) - 1;
// Convert the raw integer data to a genre
// Output its name and integer value
write_line(to_string(test_genre) + " has value " + to_string((int)test_genre));
// Assign an invalid value - by casting an integer
test_genre = (genre_data)157;
// Output its name and integer value
write_line(to_string(test_genre) + " has value " + to_string((int)test_genre));
// Read in genre data from the user
test_genre = read_genre("Enter a genre: ");
// Output its name and integer value
write_line(to_string(test_genre) + " has value " + to_string((int)test_genre));