Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Conversion of C++ String to Enumeration Type
Conversion of C++ String to Enumeration Type
You want to enter a character from the screen and then convert it to the corresponding enumeration. For example, if you enter' a' from the screen, it will be converted to the corresponding a in the collection enumeration. If that's what you mean, I've already realized it for you. The source code is as follows (see comments, you can ask me if you don't understand):

# include & ltiostream & gt

Use namespace std

Enumeration sets {a, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, z };;

void char2enum(char ch,set & ampem){

//Implement character-to-enumeration, where the parameter em is the reference type of set, thus passing the result of ch conversion.

char start = ' a

em =(set)((int)ch-(int)start);

}

void main()

{

Character string;

Set the temperature;

Cout & lt& lt Please enter a character: ";

CIN & gt; & gtstr// Get the input characters from the screen.

char2enum(str,temp); //Convert a string to temp of an enumeration.

Switch(temp) // Output according to enumeration value.

{

Case a:

Cout & lt& lta'<& ltendl breaks;

Case b:

Cout & lt& ltb'<& ltendl breaks;

Case c:

Cout & lt& ltc'<& ltendl breaks;

Case d:

Cout & lt& ltd'<& ltendl breaks;

Case e:

Cout & lt& lte'<& ltendl breaks;

Case f:

Cout & lt& ltf'<& ltendl breaks;

//Case G: ... Add these codes yourself.

//Case H: ...

}

}

Test results:

Please enter a character:

return

A // is the output content.