4

When I try to compile my code, I keep getting this error message:

badges.c: In function ‘badgeAnyColor’:
badges.c:335: error: label at end of compound statement

To help you see where it is complaining about, the complaints are on the last line of the switch statement, it says: "case 5: ". And one more concern I have is whether or not I'm doing the switch statement correctly syntax wise (I'm completely new to using switch statements with if/else statements) Help would be appreciated!

Here's my code:

int badgeAnyColor(int x, int y) {
       int bronzebadges, northbadges, northeastbadges, northwestbadges, eastbadges,
          westbadges, southbadges, southeastbadges, southwestbadges, polybadge;
    
       double fs, ht, fp, sunexp, irrexp;
    
       if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
          fs = fieldScore(x, y);
          ht = harvestTime(x, y);
          fp = fieldProfit(x, y);
          sunexp = sunExposure(x, y);
          irrexp = irrigationExposure(x, y);
          bronzebadges = countBadges(x, y);
          northbadges = countBadges(x, y + 1);
          northeastbadges = countBadges(x + 1, y + 1);
          northwestbadges = countBadges(x - 1, y + 1);
          eastbadges = countBadges(x + 1, y);
          westbadges = countBadges(x - 1, y);
          southbadges = countBadges(x - 1, y - 1);
          southeastbadges = countBadges(x + 1, y - 1);
          southwestbadges = countBadges(x - 1, y - 1);
    
          switch (bronzebadges) {
             case 0: {
                if (x == 1 && y == 1) {
                   if (northbadges == 0 && northeastbadges == 0 &&
                      eastbadges == 0) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 20 && y == 1) {
                   if (northbadges == 0 && northwestbadges == 0 &&
                      westbadges == 0) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 1 && y == 20) {
                   if (eastbadges == 0 && southeastbadges == 0 &&
                      southbadges == 0) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 20 && y == 20) {
                   if (westbadges == 0 && southwestbadges == 0 &&
                      southbadges == 0) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && y == 1) {
                   if (westbadges == 0 && northwestbadges == 0 && northbadges == 0
                      && northeastbadges == 0 && eastbadges == 0) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                return polybadge;
             }
             case 1: {
                if (fs >= 20) {
                   polybadge = 1;
                }
                else {
                   polybadge = 0;
                }
                return polybadge;
             }
             case 2: {
                if (fp <= 0 || ht < 80 || sunexp > irrexp) {
                   polybadge = 1;
                }
                else {
                   polybadge = 0;
                }
                return polybadge;
             }
             case 3: {
                if ((x >= 2 && x <= 19) && y == 1) {
                   if (((westbadges + northbadges) >= 2) || ((northbadges +
                      eastbadges) >= 2) || ((westbadges + eastbadges) >= 2)) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 1 && y == 1) {
                   if ((northbadges + eastbadges) >= 2) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 20 && y == 1) {
                   if ((northbadges + westbadges) >= 2) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 1 && y == 20) {
                   if ((southbadges + eastbadges) >= 2) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if (x == 20 && y == 20) {
                   if ((southbadges + westbadges) >= 2) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                if ((x >= 2 && x <= 19) && y == 20) {
                   if (((westbadges + southbadges) >= 2) || ((southbadges +
                      eastbadges) >= 2) || ((westbadges + eastbadges) >= 2)) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                else {
                   if (((northbadges + westbadges) >= 2) || ((westbadges +
                      southbadges) >= 2) || ((southbadges + eastbadges) >= 2) ||
                         ((northbadges + southbadges) >= 2) || ((westbadges +
                            eastbadges) >= 2)) {
                      polybadge = 1;
                   }
                   else {
                      polybadge = 0;
                   }
                }
                return polybadge;
             }
             case 4:
             case 5:
          }
       }
       else {
          polybadge = -1;
       }
       return polybadge;
    }
4

4 回答 4

4

错误:复合语句末尾的标签

上面的错误是因为这两种情况

case 4:
case 5: 
    // here you need to add statement 
    //if you don't want to do anything simple break statement will work for you
    break;    

现在你没有使用这两种情况你也可以删除它们。

如果我希望案例 3 中发生的相同事情也发生在案例 4 和 5 中怎么办?并感谢您的意见

您需要移动案例 4 和案例 5

case 3:
case 4:
case 5: 
//statements you written for case 3 

例如

switch(n)
{
    case 1:  
        printf("case 1");
        break;
    case 2:  
        printf("case 2");
        break;
    case 3:
    case 4:
    case 5: 
        printf("case 3 or 4 or 5");
        break;
    default: 
        printf("Wrong choice");
        break;

}
于 2013-10-24T09:14:26.933 回答
4

C 中的Aswitch由一组必须包含语句的案例标签组成。粗略地说,语句是以 . 结尾的东西;。像这样的代码

switch (n){
    case 4:
    case 5:
}

无效,因为没有要执行的语句。这就是给你一个编译错误的原因。一种解决方法是写

switch (n){
    case 4:
    case 5:
        ;
}

其中;is 表示一个空语句。请注意,switch inC具有后续功能含义,case4case5. 这就是为什么在 . 之后不需要空语句的原因case4

更好的是,只需删除这些案例。

于 2013-10-24T09:23:49.703 回答
2

您必须指定案例 5(和案例 4)应该发生的情况;现在什么都没有,这就是它所抱怨的——你给了它一个案例标签,但没有告诉它如何处理那个案例。

如果您不希望发生任何事情,您可以将这些案例留在您的交换机之外。

于 2013-10-24T09:14:09.070 回答
1

复合语句末尾的标签是case 5:没有任何要执行的。你至少需要的是break

case 4:
case 5:
    // do nothing
    break;

现在关于语法 - 它似乎确实是错误的。首先 - 您不需要额外的代码括号以防万一。其次 - 你确实需要break在每个块的末尾,除非你想“通过”到下一个案例分支。如果您确实需要,请添加评论说明:

case 1:
    do_something();
    break;
case 2:
    do_something_else();
    /* fall-through */
case 3:
    and_something_else_more();
    break;
case 42:
    if (allow_easter_eggs) // like this
        printf("DON'T PANIC\n");
    break;

这将do_something()在 1、2do_something_else()和3and_something_else_more()的情况下执行and_something_else_more()

default最后,添加标签总是一个好主意。

于 2013-10-24T09:31:09.103 回答