Enums

Is it possible to display (Easy, Normal, Hard) instead of 0, ,1 ,2
 
  enum difficulty {Easy, Normal, Hard};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <map>

enum difficulty {Easy, Normal, Hard};

const char* diff_names[3] = {"Easy", "Normal", "Hard"}; //1

const std::map<difficulty, const char*> difficulty_name = { //2
    {Easy, "Easy"}, {Normal, "Normal"}, {Hard, "Hard"},
};

//3 Not the smartest idea. Bad.
#define _tostr(X) (#X)
#define tostr(X) _tostr(X)

int main()
{
   std::cout << diff_names[Normal] << '\n';
   std::cout << difficulty_name.at(Easy) << '\n';
   std::cout << tostr(Hard);
}
Last edited on
I'm not familiar with map, it looks to me like some sort of casting?
No, it similar to array, but its keys are not integers, but some other type.
There is no automatic way to get the name of the enums. You will have to write the code to convert the enum/int to a string yourself. MiiNiPaa has shown you two different ways of doing it (ignore the third one). If you don't know maps you can probably at least grasp the array method. If you want you can use std::string instead of const char*.
 
const std::string diff_names[3] = {"Easy", "Normal", "Hard"};

Last edited on
yea the array method I can get, need to read up on maps imtereted in them now :D
Topic archived. No new replies allowed.