Problems with the 'p' || 'P': syntax inside switch statement in C ++

I used the switch statement as follows:

   switch (ch){
   case 'P' || 'p': 
        goto balance;
        break;

   case 'r' || 'R':
        goto menu;
        break;

   default:
           cout<<"\t\tInvalid Choice!!"<<endl;
           system ("\t\tpause");
           system ("cls");
           goto menu;
           break;
           }

But there seems to be something wrong with the following syntax:

case 'r' || 'R'

The compiler complains about the "duplicate case value". What happened to my code?

+4
source share
3 answers

Change it to

case 'P':
case 'p': 
    goto balance;
    break;

Usage is gotousually not a good idea.


case 'P' || 'p': case 1, || 0, 1 . , case, 'p' || 'P', 'r' || 'R', 1, .

+10
case 'P' || 'p': 
    ...

:

case 'P':
case 'p':
    ...

, ( ) , :

switch ( std::tolower(ch) ) {
case 'p': 
     ...
     break;
case 'r':
     ...
     break; 

default:
     ...
}

#include <cctype>

+8

|| ; 'P' || 'p' true, || . 'R' || 'r'. , case: case true:, , . :

case 'P':
case 'p':
    menu(); // function call recommended instead of `goto`
    break;
+3

All Articles