Error c2051: Case expression not constant?

Source: Internet
Author: User

Today's programming. The following functions are required in the keyboard response function,

Void keyboard (unsigned char key, int X, int y)

{Switch (key) {Case "S": Case "S": anglex + = 15.0; fig (); break; Case "D": Case "D ": angley + = 15.0; fig (); break;

Case 27: // ESC in the upper left corner of the keyboard

Exit (0); break ;}

}

When compiling in VC, error c2051: Case expression not constant occurs.


I don't think so. Case "S ":

Case "S": it is a constant, character.

I searched the internet for half a day and found a problem with the case expression,

For example, 's' is a constant (single quotes), and "S" is a string (double quotation marks ).

So the solution is to replace the double quotation marks in all case expressions with single quotation marks as follows:

Void keyboard (unsigned char key, int X, int y) {Switch (key) {Case's ': anglex + = 15.0; glupostredisplay (); break; case 'D': angley + = 15.0; fig (); break; Case 27: // ESC in the upper left corner of the keyboard

Exit (0); break;

}

}


Compiled successfully!

I used to solve this problem before, but I couldn't remember it when I used it. I encountered it again today. So I wrote it down for future use!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.