tags:

views:

330

answers:

2

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!

+6  A: 

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.

Tyler McHenry
In addition to that, the OP really don't want to compare 'selection' to the result of the comma operator.
nos
well when i add a closed parentheses it gives me this along with the the original error:\error C2059: syntax error : ')'this is what the line looks like when i add the closed parentheses: while (selection == ('a','b','c', 'd', 'e', 'f', 'g', 'h', 'i','A','B' 'C', 'D', 'E', 'F', 'G', 'H', 'I'));
further hint - while loop definition doesn't normally end with a ; unless you don't want it to do anything
Greg Domjan
so how should i write the line out without having to use all the commas. i need to include the letter A through I (lowercase and uppercase)
when i take ; out it says that im missing the semicolon. now im confused.
I suggest you find a good introductory book on C. You seem to be confused about the basic syntax of a `while` loop, which is somewhat below the level at which you can get useful help from the Internet.
Tyler McHenry
there is a missing , between 'B' and 'C' thus breaking the CSV list and causing the expectation of a ) before the 'C' - that would fix the syntax but not resolve process issue you have
Greg Domjan
+1  A: 

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.

JSBangs
YES!! thank you, thank you, thank you!!!
Just note that while this works on PCs, it can break on (for example) IBM mainframes. They use EBCDIC instead of ASCII, and EBCDIC has some non-alphabetic characters between some of the letters -- something like half a doze between 'i' and 'j', and the same number between 'r' and 's'. Likewise with the upper-case letters. [Warning: my memory of the exact letters could be a bit off].
Jerry Coffin
or make it e.g. : `while(strchr("abcdefghiABCDEFGHI",selection) != NULL) { ... } `
nos