Search code examples
c++switch-statementdev-c++

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?


Solution

  • 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;
    }