switch

Hi there, i am trying to test some code but i am stuck with an error message :

error C2196: case value '1' already used...

Here is the code:

switch (cha){
case 'd'||'D':
if (toBuy==true)
after=before*TODOLLAR;
else
after=before/TODOLLAR;
break;
case 'F'||'f':
if (toBuy==true)
after=before*TODOLLAR;
else
after=before/TODOLLAR;
break;
default:break;
}
the problem is that in the case statement if i write like 'F'||'f' it pops out an error message. Of course if there is only one character, say 'F', then it works fine. Why doesnt it work? Doesnt compiler see that these characters are different?
Because case 'd' || 'D': is not legal syntax.

http://www.cplusplus.com/doc/tutorial/control/ read the part on switches.
Last edited on
case 'F' || 'f': is the same as case 1: because || evaulates to an expression. || will look at both values on the left and right side of it and return 1 if either one of them are nonzero.

Since both 'F' and 'f' ar enonzero, 'F' || 'f' evaluates to 1, leaving you with case 1:

case 'D' || 'd': has the same effect and also results in case 1:, which is why you're getting that error.

To do what you're attempting, you need multiple cases:

1
2
3
4
5
6
7
case 'f': case 'F':
  //...
  break;

case 'd': case 'D':
  //...
  break;
thank you for clarification!
i didnt read all your code but instead of checking for both upper and lower case characters you can just use tolower to make it lower case. then you only have to check for lower case.
Topic archived. No new replies allowed.