I'm currently in a C++ course and to learn about switches our instructor wanted us to edit the body of some openGL 3D code. We were supposed to change it from a if then else to a switch. I did that, but now my code won't run. Here is the code:
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;
}
Here is the original code (sorry it got screwed up a little bit, I was copying and pasting into a comment):
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();
}
}
}
The if then else statement will still run. The errors I'm getting with the switch are: syntax error before "double"
'alpha' undeclared (first use in this function)