How to use enum? Example,

enum stat {WIN,LOSE,CONT}; switch (stat) { case WIN: break; case LOSE: break; case CONT: break; } 

And this code does not work, because as it turns out stat , this is the type of variables WIN,LOSE,CONT So how then do I implement a switch with an enumeration?

  • one
    In this case, you just created a new type stat, but to use it you need to declare a variable of this type and initialize it with an initial value. - brightside90
  • And there is such a function in Linux: stat, so I recommend that you rename your enum so that there are no name conflicts. - vladimir_ki
  • Well :) Itself under Windows, but I will take note. - Vlad Malakhin

1 answer 1

For example:

 #include <exception> #include <iostream> enum Stat { WIN, LOSE, CONT }; int main(int argc, char* argv[]) { Stat value = LOSE; switch (value) { case WIN: throw std::invalid_argument("'WIN' is invalid here."); break; case LOSE: std::cout << "Everything is fine."; break; case CONT: throw std::invalid_argument("'CONT' is invalid here."); break; } } 
  • Thank you very much, I understood everything. - Vlad Malakhin