Problems with case 'p' || 'P': syntax within a switch statement in C++

前端 未结 3 858
有刺的猬
有刺的猬 2021-01-13 23:33

I\'ve used the switch statement the following way:

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

   case \'r\' || \'R\':
           


        
3条回答
  •  隐瞒了意图╮
    2021-01-13 23:49

    || is a binary operator; 'P' || 'p' evaluates to true, because the left-hand operand of || is non-zero. Same thing for 'R' || 'r'. So both case statements are case true:, and that's what the compiler is complaining about. Separate the values:

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

提交回复
热议问题