Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a switch case statement, it says "duplicate case value" comes up as an error. Anyone know why?

I am working on a rock paper scissors program, but this time the computer chooses rock half the time, scissors a third of the time, and paper only one sixth of the time. The way I did this was I enumerated six possible computer choice values:

enum choicec {rock1, rock2, rock3, scissors1, scissors2, paper};
choicec computer;

But then, after the computer makes its choice, I have to convert these enumerated values to either rock, paper, or scissors. I did this using a switch-case statement:

switch(computer) {
        case rock1 || rock2 || rock3:
            c = 1;
            break;
        case scissors1 || scissors2: //ERROR!
            c = 3;
            break;
        case paper:
            c = 2;
            break;
    }

one is rock, two is paper, and three is scissors. However, on the line where I have error written in as a comment, it gives me this error: [Error] duplicate case value.

I'm not sure why. Any ideas?

like image 667
Victor Odouard Avatar asked Jun 20 '13 22:06

Victor Odouard


People also ask

What is duplicate case value error?

Duplicate Case Error means you have defined two cases with the same value in the switch statement. You are probably looking at your code thinking "but they are all different." To you they are different. To the complier they look much different. You have defined case statements using the character notation.

What if none of the cases match the value in switch case statement?

The computer will go through the switch statement and check for strict equality === between the case and expression . If one of the cases matches the expression , then the code inside that case clause will execute. If none of the cases match the expression, then the default clause will be executed.

What is duplicate case label?

Go Up to Error and Warning Messages (Delphi) This error message occurs when there is more than one case label with a given value in a case statement.

What will happen if the switch statement did not match any cases?

If the switch condition doesn't match any condition of the case and a default is not present, the program execution goes ahead, exiting from the switch without doing anything. Show activity on this post.


1 Answers

I am not sure what you doing, but switch statement should look like this

switch(computer) 
{
    case rock1:
    case rock2:
    case rock3:
        c = 1;
        break;
    case scissors1:
    case scissors2:
        c = 3;
        break;
    case paper:
        c = 2;
        break;
}
like image 102
aah134 Avatar answered Oct 26 '22 00:10

aah134