Anu Jacob
Anu Jacob

Reputation: 1

How to pass enum name to switch directly, vc++ mfc

I am searching to know how to pass the enum name directly to switch case. Eg:

enum Flower { rose, jasmine };

switch (Flower)
{
case rose : //Code for rose
case jasmine: //Code for jasmine
}

Someone please help me how to do this way? I found this code portions from C. But I need the same code portions in C++. Is it possible in c++? Please help me. Thanks all

Upvotes: 0

Views: 162

Answers (2)

hankym
hankym

Reputation: 149

In C and C++ Programmer,switch(Expression),the Expression's result must be a integer value. enum Flower{rose,jasmine}; Flower is just a Type,not a value, like int rose = 0,int jasmine=1,you can't use it in switch expression. You can just define like enum Flower fname;

switch(fname)
{
   case rose:
    ...
   break;
   case jasmine:
    ...
   break;
   default:
   break;
}

Upvotes: 0

Jabberwocky
Jabberwocky

Reputation: 50775

You probably want this:

Flower f = rose;
...
switch (f)
{
  case rose : //Code for rose
  case jasmine: //Code for jasmine
}

Upvotes: 1

Related Questions