How to Use an Enum Value in a Switch Statement in C++

Using enum type in a switch statement

No case labels. You've got goto labels now. Try:

switch(check){
case NEG_INF: printf("neg inf"); return 1;
case ZERO: printf("zero"); return 2;
case POS_INF: printf("pos inf"); return 3;
default: printf("not special"); break;
}

Enum usage with switch-case in C

From the C Standard (6.7.2.2 Enumeration specifiers)

4 Each enumerated type shall be compatible with char, a signed integer
type, or an unsigned integer type. The choice of type is
implementation-defined,128) but shall be capable of representing the
values of all the members of the enumeration.

It means that internally an object of an enumeration type is not necessary stored as an object of the type int or unsigned int. It can be stored internally as an object of the type char.

So this call of scanf

scanf("%u", &choice);

invokes undefined behavior.

You need to use an intermediate variable of the type unsigned int and after the call of scanf with this variable assign its integer value to the object choice.

Another approach is for example to declare an object of the type char and ask the user to enter either 'm' for male of 'f' for female After that you can convert it either to the value 0 or 1.

For example

char c = 0;
scanf( "%c", &c );

if ( c == 'm' ) c = 0;
else if ( c == 'f' ) c = 1;
else c = 2;

choice = c;

How to compare enum values in switch-case?

Use break statement in switch case so that when a break statement is reached, the switch terminates and the flow of control jumps to the next line following the switch statement.

switch (myPtr->value) {
case VALUE_ONE:
...;
break;

case VALUE_TWO:
...;
break;

default:
...;
}

How to assign an enum for a switch case from UserInput string in C++

There's no need for an enum here. switch works with char because char is convertible to int. So if we define a char as such:

char c = 'a';

..and then switch on it:

switch (c)

This works because 'a' can be converted to int (ASCII value). So this can also be written as:

switch (int(c)) // Same as switch (c)

Example:

#include <iostream>

int main(int argc, char** argv)
{
char input; std::cin >> input;

switch (input)
{
case 'a':
std::cout << 'a' << std::endl;
break;
case 'b':
std::cout << 'b' << std::endl;
break;
case 'c':
std::cout << 'c' << std::endl;
break;
default:
std::cout << "Some other key" << std::endl;
break;
}
}

The above example works. For your case, I would rewrite your program as:

#include <iostream>

int main(int argc, char** argv) {

while (true) {
std::cout << "enter valid input" << std::endl;

char charInput;
printf("Enter a command: ");
std::cin >> charInput;

switch (charInput) {
case 'i':
printf("This is case i");
exit(0); //case i
default:
std::cout << "invalid command, try again!\n";
break;
}
};
}

Also, consider not using the following in your code:

using namespace std;

..as it's considered as a bad practice. For more info on this, look up why is "using namespace std" considered as a bad practice.

C switch statement and enum explicit cast

In this declaration

    enum
{
PROP_A = 1,
PROP_B,
N_PROPERTIES
} Property;

you declared an object Property of an unnamed enumeration type.

So in the switch statement

switch ((Property) a)

you are using a syntactically incorrect expressions with two objects Property and a.

It seems you mean

    typedef enum
{
PROP_A = 1,
PROP_B,
N_PROPERTIES
} Property;

Pay attention that you may just write

switch ( a )

In C enumerators have the type int. That is these enumerators PROP_A, PROP_B, N_PROPERTIES all have the type int. So the casting that you tried to apply (if the enumeration would be declared correctly) is redundant.

Usage of enum and char in switch statements

C and C++ can implicitly convert between lots of types. In this case, the integral types int and char and your enum, etc. It's perfectly permissible to convert your enum value to a char and back again, so long as the values of your enum don't go over 127 (which they do not).

In C and C++, char and "8-bit integer" are basically the same thing. And it isn't an error to convert between bit-widths like int8_t (signed char), int16_t, int32_t, and int64_t.



Related Topics



Leave a reply



Submit