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?
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.
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With