Do OR in Switch-Case in C ++

How do you do this in C ++? For example, I try to start a program exit if the user presses ESC or "q" or "Q".

I tried looking for it, but I did not find syntax for it in C ++. I know how to do this with if-else, but is it possible with a switch? Of course, I can just make a function and call it from two separate cases, but is there a way to do this only with the combined case statement?

For example, what I'm looking for (of course, doesn't work):

void keyboard( unsigned char key, int x, int y ) { switch( key ) { case ( 27 || 'q' || 'Q' ): exit( 0 ); break; case 'a': ... case 'b': ... } } 
+8
c ++ visual-studio-2010
source share
4 answers

Cases fail without a break:

 case 27: //could be 27 case 'q': //could be 27 or 'q' case 'Q': //could be 27, 'q', or 'Q' exit(0); break; 
+35
source share

I think it's just

 switch(key){ case 'a': case 'b': /*code*/ break; ... 

Cases A and B will execute the same code.

+1
source share
 void keyboard( unsigned char key, int x, int y ) { switch( key ) { case 027: case 'q': case 'Q': exit( 0 ); break; case 'a': ... case 'b': ... } } 
0
source share

the syntax becomes

  void keyboard( unsigned char key, int x, int y ) { switch( key ) { case 27: case 'q': case 'Q': exit( 0 ); break; case 'a': ... case 'b': ... } } 
0
source share

All Articles