error C2143: syntax error : missing ')' before 'constant
I keep getting this error on my project开发者_高级运维 and i cant figure it out! please help!
error C2143: syntax error : missing ')' before 'constant'
the line is:
while (selection == ('a','b','c', 'd', 'e', 'f', 'g', 'h', 'i','A','B' 'C', 'D', 'E', 'F', 'G', 'H', 'I');
also i know there is an easier way to write that line out but im not sure how i can do it. im a beginner at this so can any of you pros edit this line for me!
How many open parentheses do you have?
How many closed parentheses do you have?
Are these the same number? If not, where is one missing?
Also, the syntax a == (b,c,d)
is not shorthand for a == b || a == c || a == d
like you seem to think. It's actually equivalent to a == d
due to the way the comma operator works.
To be completely explicit, what you actually want is this:
while (selection == 'a' || selection == 'b' ||
selection == 'c' || selection == 'd' ||
selection == 'e' || selection == 'f' ||
selection == 'g' || selection == 'h' ||
selection == 'i' || selection == 'A' ||
selection == 'B' || selection == 'C' ||
selection == 'D' || selection == 'E' ||
selection == 'F' || selection == 'G' ||
selection == 'H' || selection == 'I')
{
/* Do stuff */
}
Or, to be a lot more consice about it, you can take advantage of the fact that the letters are arranged alphabetically in the ASCII table, and write
while (tolower(selection) >= 'a' && tolower(selection) <= 'i')
{
/* Do stuff */
}
This requires inclusion of <ctype.h>
for the tolower
function.
Given your comments on Tyler's post, it seems like what you really want is:
while ((selection >= 'a' && selection <= 'i') || (selection >= 'A' && selection <= 'I'))
{
// loop
}
Characters can be compared as if they were numbers (because they are numbers in the CPU), which means that you can check for a range of characters using the < > <= >=
operators.
精彩评论