我目前在C++課程中學習開關,我們的教師希望我們編輯一些openGL 3D代碼的主體。我們應該把它從一個如果其他的切換到一個切換。我做到了,但現在我的代碼無法運行。下面是代碼:C++ 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();
}
}
}
的,如果再else語句會仍然運行。我與交換機收到的錯誤是:前未申報的「雙重」
「阿爾法」 語法錯誤(在一次使用此功能)
在switch語句之前聲明'double alpha'。看到http://stackoverflow.com/questions/1231198/declaring-variables-inside-a-switch-statement –
「但現在我的代碼不會運行」 - 不管是什麼意思?請明確點。如果你不這樣做,你基本上懶惰或要求我們推測 – sehe