Sayed Alesawy
Sayed Alesawy

Reputation: 445

C2196 error in a switch statement

i am a newbee in C++ , i am trying to write a code to evaluate the value entered by user of a variable typed char and its name is "response" using a switch statement that will work with 2 cases

case 1 : if the response is y(lowercase) or Y(uppercase) it will print a message of the console window

case 2 : if the response is n(lowercase) or N(uppercase) it will print another message to the console window

but i have encountered a problem with my code that generated C2196 error , i searched it on msdn and i understand what does it mean but yet and can't fix that error

#include<iostream>
using namespace std; 
int main()
{
    char response;
    cout << " Enter your response " << endl;
    cin >> response;
    switch (response)
    {
    case 'y' || "Y":
        cout << "You chose y or Y " << endl;
        break;
    case 'n' || 'N': 
        cout << "You chose n or N " << endl;
        break;
    default:
        cout << "You didn't choose a valid option " << endl;
        break;
    }
    return 0;
}   

Upvotes: 2

Views: 1203

Answers (6)

tmlen
tmlen

Reputation: 9092

In a code like

switch(x) {
case a:
    ...
    break;
case b:
    ...
    break;
}

Control flow only jumps into case a when x == a. So with case 'Y' || 'y' the compiler really evaluates the expression 'Y' || 'y' to true || true and then to true. Then it would jump into this label only when response == (char)true == (char)1, which is never the case.

switch statements cannot be used for more complex conditions, and also it works only with integral values (ints, chars). It allows the compiler to generate more efficient code, but with modern compilers the result is likely always the same as when using if / else if.

switch(response) {
case 'Y':
case 'y':
    ...
    break;
}

works correctly instead. Because the first case 'Y' has no break statement, it falls through and executed the same code as the case 'y'.

Upvotes: 0

Van Tr
Van Tr

Reputation: 6093

firstly, please remove enter code here in your question.

About the problem, C2196:

A switch statement uses the same case value more than once.

Because ('y' || "Y") = ('n' || 'N') = true

You actually need to do like this:

#include<iostream>
using namespace std; 
int main()
{
    char response;
    cout << " Enter your response " << endl;
    cin >> response;
    switch (response)
    {
    case 'y' :
    case 'Y':
        cout << "You chose y or Y " << endl;
        break;
    case 'n':
    case 'N': 
        cout << "You chose n or N " << endl;
        break;
    default:
        cout << "You didn't choose a valid option " << endl;
        break;
    }
    return 0;
}   

Upvotes: 0

maddin45
maddin45

Reputation: 747

There are multiple problems with you code.

You can only test for one value in each case statement. You should write

switch (response)
{
case 'y':
case 'Y':
    cout << "You chose y or Y " << endl;
    break;
case 'n': 
case 'N':
    cout << "You chose n or N " << endl;
    break;
default:
    cout << "You didn't choose a valid option " << endl;
    break;
}

to test for different values that will have the same effect.

Also, what you are actually testing is if response == true. In a boolean expression everything that is not 0 is treated as true, so both 'y' and "Y" are true and 'y'||"Y" is evaluated to true as well. The same goes for 'n' and 'N'. What you are currently testing is

switch (response)
{
case (char)true:
    cout << "You chose y or Y " << endl;
    break;
case (char)true:
    cout << "You chose n or N " << endl;
    break;
default:
    cout << "You didn't choose a valid option " << endl;
    break;
}

In both cases you test if response is (char)true, thus you get the error C2196 which means "case value 'value' already used" (see the microsoft documentation).

Last but not least, you are mixing up char and const char *. 'y' is a char, while "Y" will give you a const char *, a pointer to a C-string. You should use the former to compare single characters.

Upvotes: 0

songyuanyao
songyuanyao

Reputation: 172864

case 'y' || "Y" doesn't do what you expected. You're using logical || here, which will always return true, and for char type it means 1. And it's same for the case 'n' || 'N', so you wrote two case 1: indeed here, that's why you got the error message:

error C2196: case value '1' already used

You might want

switch (response)
{
case 'y':
case 'Y':
    cout << "You chose y or Y " << endl;
    break;
case 'n':
case 'N': 
    cout << "You chose n or N " << endl;
    break;
default:
    cout << "You didn't choose a valid option " << endl;
    break;
}

Upvotes: 0

ZivS
ZivS

Reputation: 2124

a switch control flow looks for a constant values among its cases. See here.

To accomplish what you are trying to do, you can use a switch-case like this:

enter code here
#include<iostream>
using namespace std; 
int main()
{
    char response;
    cout << " Enter your response " << endl;
    cin >> response;
    switch (response)
    {
    case 'y':
    case 'Y':
        cout << "You chose y or Y " << endl;
        break;
    case 'n':
        //since there is no 'break;' statement here, either cases will perform the code 
    case 'N':
        cout << "You chose n or N " << endl;
        break;
    default:
        cout << "You didn't choose a valid option " << endl;
        break;
    }
    return 0;
}   

Upvotes: 0

SergeyA
SergeyA

Reputation: 62553

case labels are not if statements. You can't or them. Instead, you can have multiple case labels with the same code. For example:

  case 'y':
  case 'Y':
        cout << "You chose y or Y " << endl;
        break;

Remember, switch() is actually a goto in disguise. case labels are effectively goto labels, so you can not have any logical operations on them. It also allows you to 'combine' case labels together like I've shown - once control has been transferred to the first label, it continues through the second, until it sees break.

On a side note, you can't use double quotes to represent a single character, as you do with your capital Y.

Upvotes: 2

Related Questions