How to get next value of enum
c++, enums
Solution
I find that an explicit look up table works best, for both converting from enum to text and text to enum:
enum Language_Enum
{
LANGUAGE_FIRST = 0,
LANGUAGE_GERMAN = LANGUAGE_FIRST,
LANGUAGE_ENGLISH,
LANGUAGE_HOPI,
LANGUAGE_WELSH,
LANGUAGE_TEXAN,
LANGUAGE_DUTCH,
LANGUAGE_LAST
};
struct Language_Entry
{
Language_Enum id;
const char * text;
};
const Language Entry language_table[] =
{
{LANGUAGE_GERMAN, "German"},
{LANGUAGE_HOPI, "Hopi"},
{LANGUAGE_DUTCH, "Dutch"},
// ...
};
const unsigned int language_table_size =
sizeof(language_table) / sizeof(language_table[0]);
Specifying the `enum` along with the text, allows for the enum order to change with minimal effect to the search engine.
The `LANGUAGE_FIRST` and `LANGUAGE_LAST` identifiers allow for iteration of the enum:
Language_Enum l;
for (l = LANGUAGE_FIRST; l < LANGUAGE_LAST; ++l)
{
// ...
}
Problem
I have the following problem: ``` enum Language { English, French, German, Italian, Spanish }; int main() { Language tongue = German; tongue = static_cast<Language>(tongue + 1); cout << tongue; } ``` //it returns 3.....but i want to get the language name on index 3.....