C ++ switch语句不起作用

时间:2013-11-03 20:49:45

标签: c++ opengl if-statement switch-statement

我目前正在参加C ++课程,并了解我们的教师希望我们编辑某些openGL 3D代码的主体的开关。我们应该将它从if if else改为switch。我做到了,但现在我的代码不会运行。这是代码:

void key(unsigned char k, int x, int y)
{
    k = tolower(k);

    switch(k)
    {
             case 'a' : b_animate = !b_animate;
                        if (b_animate)
                        {
                           glutTimerFunc(33, myTimer, 1);
                        }
                        break;
             case 'h' : b_showHints = !b_showHints;
                        glutPostRedisplay();
                        break;
             case 'f' : toggleFullScreen();
                        break;
             case 'o' : gi_projection_type = ORTHO_3D;
                        myReshape(g_windowWidth, g_windowHeight);
                        glutPostRedisplay();
                        break;
             case 'p' : gi_projection_type = PERSPECTIVE;
                        myReshape(g_windowWidth, g_windowHeight);
                        glutPostRedisplay();
                        break;
             case '1' :
             case '2' :
             case '3' :
             case '4' :
             case '5' : 
             case '6' :
             case '7' : 
             case '8' :
             case '9' : double alpha = (k - '0') * 0.1;
                        if (alpha == 0)
                        alpha = 1.0;
                        setAlphaChannel(alpha);   
                        glutPostRedisplay();     
                        break; 
             case '+' : per_angle += 2;
                        myReshape(g_windowWidth, g_windowHeight);
                        glutPostRedisplay();
                        break;
             case '-' : per_angle -= 2;
                        myReshape(g_windowWidth, g_windowHeight);
                        glutPostRedisplay();
                        break;
             case 'm' : printModelViewMatrix();
                        break;
             case 'w' : b_wireFrame = !b_wireFrame;
                        glutPostRedisplay();
                        break;
             case 'q' : b_useOpenGLtransform = !b_useOpenGLtransform;
                        glutPostRedisplay();
                        break;
             case 'n' : g_model = (g_model + 1)%8;
                        glutPostRedisplay();
                        break; 
             case 't' : b_texture = !b_texture;
                        glutPostRedisplay();
                        break;

             case 'l' : b_lighting = !b_lighting;
                        glutPostRedisplay();
                        break;

    }

这是原始代码(抱歉它搞砸了一点,我正在复制并粘贴到评论中):

   if(k == 'a'){
   b_animate = !b_animate;
   if (b_animate){
       glutTimerFunc(33, myTimer, 1);

   }else if (k == 'h'){
    b_showHints = !b_showHints;
    glutPostRedisplay();

     }else if(k == 'f'){
     toggleFullScreen();

     }else if (k == 'o'){
         gi_projection_type = ORTHO_3D;
         myReshape(g_windowWidth, g_windowHeight);
         glutPostRedisplay();
   }else if (k == 'p'){
        gi_projection_type = PERSPECTIVE;
         myReshape(g_windowWidth, g_windowHeight);
         glutPostRedisplay();
    }else if (k >= '0' && k <= '9'){
    double alpha = (k - '0') * 0.1;
    if (alpha == 0)
       alpha = 1.0;
    setAlphaChannel(alpha);   
    glutPostRedisplay();     
             }else if (k == '+'){
      per_angle += 2;
      myReshape(g_windowWidth, g_windowHeight);
      glutPostRedisplay();

           }else if (k == '-'){
      per_angle -= 2;
      myReshape(g_windowWidth, g_windowHeight);
      glutPostRedisplay();

      }else if (k == 'm'){
       printModelViewMatrix();

       }else if (k == 'w'){
      b_wireFrame = !b_wireFrame;
      glutPostRedisplay();

      }else if (k == 'q'){
      b_useOpenGLtransform = !b_useOpenGLtransform;
      glutPostRedisplay();

       }else if (k == 'n'){
      g_model = (g_model + 1)%8;
      glutPostRedisplay();

      }else if (k == 't'){
      b_texture = !b_texture;
      glutPostRedisplay();

      }else if (k == 'l'){
      b_lighting = !b_lighting;
      glutPostRedisplay();
         }
         }    
}

if then else语句仍然会运行。我通过开关得到的错误是: “double”之前的语法错误

'alpha'未声明(首次使用此功能)

5 个答案:

答案 0 :(得分:2)

你不能在switch语句中声明变量,除非你把它们放在一个块中(用{}包围):

switch(k)
{
         case 'a' : b_animate = !b_animate;
                    if (b_animate)
                    {
                       glutTimerFunc(33, myTimer, 1);
                    }
                    break;
         case 'h' : b_showHints = !b_showHints;
                    glutPostRedisplay();
                    break;
         case 'f' : toggleFullScreen();
                    break;
         case 'o' : gi_projection_type = ORTHO_3D;
                    myReshape(g_windowWidth, g_windowHeight);
                    glutPostRedisplay();
                    break;
         case 'p' : gi_projection_type = PERSPECTIVE;
                    myReshape(g_windowWidth, g_windowHeight);
                    glutPostRedisplay();
                    break;
         case '1' :
         case '2' :
         case '3' :
         case '4' :
         case '5' : 
         case '6' :
         case '7' : 
         case '8' :
         case '9' : {                               // Note: begin of block
                    double alpha = (k - '0') * 0.1;
                    if (alpha == 0)
                    alpha = 1.0;
                    setAlphaChannel(alpha);   
                    glutPostRedisplay();     
                    break;
         }                                          // Note: end of block
         case '+' : per_angle += 2;
                    myReshape(g_windowWidth, g_windowHeight);
                    glutPostRedisplay();
                    break;
         case '-' : per_angle -= 2;
                    myReshape(g_windowWidth, g_windowHeight);
                    glutPostRedisplay();
                    break;
         case 'm' : printModelViewMatrix();
                    break;
         case 'w' : b_wireFrame = !b_wireFrame;
                    glutPostRedisplay();
                    break;
         case 'q' : b_useOpenGLtransform = !b_useOpenGLtransform;
                    glutPostRedisplay();
                    break;
         case 'n' : g_model = (g_model + 1)%8;
                    glutPostRedisplay();
                    break; 
         case 't' : b_texture = !b_texture;
                    glutPostRedisplay();
                    break;

         case 'l' : b_lighting = !b_lighting;
                    glutPostRedisplay();
                    break;

}

或者,在switch语句之前声明alpha

答案 1 :(得分:2)

您已成为“一体化”功能设计的受害者。你功能的身体是难以理解的一塌糊涂。

当然,你可以根据需要尽可能地挖掘这个烂摊子,直到你解决它。但更好地重新思考整体设计。将其拆分为较小的功能,每个键一个:

case 'a' : OnKeyA(); break;
case 'h' : OnKeyH(x, y, z); break
case 'f' : OnKeyF(x); break;

....

void OnKeyA()
{
   b_animate = !b_animate;
   if (b_animate)
   {
       glutTimerFunc(33, myTimer, 1);
   }
}

不是更干净吗?案例陈述成为单行,所有逻辑都归功于功能。你永远不会迷失在花括号中。

告诉您的导师“Separation of concerns”和“Single responsibility principle”,他会留下深刻的印象=)

快乐的编码!

答案 2 :(得分:1)

每当你需要在一个case中声明变量时,把它放在大括号之间.-

case '9' : { 
    double alpha = (k - '0') * 0.1;
    if (alpha == 0)
        alpha = 1.0;
    setAlphaChannel(alpha);   
    glutPostRedisplay();     
    break; 
}

答案 3 :(得分:0)

你应该在{}中包装案例处理程序,如

         case '9' :
                 {
                    double alpha = (k - '0') * 0.1;
                    if (alpha == 0)
                    alpha = 1.0;
                    setAlphaChannel(alpha);   
                    glutPostRedisplay();     
                    break; 
                 }

因为可以在switch-case结构内的范围内声明新变量

答案 4 :(得分:0)

您需要为double添加范围:

     case '9' : {
                   double alpha = (k - '0') * 0.1;
                   if (alpha == 0)
                      alpha = 1.0;
                   setAlphaChannel(alpha);   
                   glutPostRedisplay();     
                }
                break; 

您需要添加

     case '0':

之前的列表! (您在意外上只有'1''9'