c++语句switch语句_错误:案例标签不在C中的switch语句内

c++语句switch语句

The error: case label not within a switch statement occurs in C language with switch case statement, when switch (variable/value) statement is terminated by the semicolon (;).

错误:当switch(变量/值)语句由分号( ; )终止时,C语言中的case标签使用switch case语句以C语言出现。

Example:

例:

#include <stdio.h>

int main(void) {
	
	int choice = 2;
	
	switch(choice);
	{
	    case 1:
	        printf("Case 1\n");
	        break;
	    case 2:
	        printf("Case 2\n");
	        break;	    
	    case 3:
	        printf("Case 3\n");
	        break;	    
	    case 4:
	        printf("Case 4\n");
	        break;	    
	    default:
	        printf("Case default\n");
	}
	
	return 0;
}

Output

输出量

prog.c: In function ‘main’:
prog.c:9:6: error: case label not within a switch statement
      case 1:
      ^~~~
prog.c:11:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:12:6: error: case label not within a switch statement
      case 2:
      ^~~~
prog.c:14:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:15:6: error: case label not within a switch statement
      case 3:
      ^~~~
prog.c:17:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:18:6: error: case label not within a switch statement
      case 4:
      ^~~~
prog.c:20:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:21:6: error: ‘default’ label not within a switch statement
      default:
      ^~~~~~~

How to fix?

怎么修?

See the statement switch(choice); it is terminated by semicolon (;) – it must not be terminated. To fix this error, remove semicolon after this statement.

参见语句switch(choice); 它以分号( ; )终止–一定不能终止。 要解决此错误,请在此语句后删除分号。

Correct code:

正确的代码:

#include <stdio.h>

int main(void) {
	
	int choice = 2;
	
	switch(choice)
	{
	    case 1:
	        printf("Case 1\n");
	        break;
	    case 2:
	        printf("Case 2\n");
	        break;	    
	    case 3:
	        printf("Case 3\n");
	        break;	    
	    case 4:
	        printf("Case 4\n");
	        break;	    
	    default:
	        printf("Case default\n");
	}
	
	return 0;
}

Output

输出量

Case 2


翻译自: https://www.includehelp.com/c-programs/case-label-not-within-a-switch-statement-error-in-c.aspx

c++语句switch语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值