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语句
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...
error: a label can only be part of a statement and a declaration is not a statement
switch(a){ swtch(a){
case 1: ca
case 1: cout << "平局" << endl; break;
case 2: cout << "你赢了" << endl; break;
case 3: cout << "电脑赢了" << endl; break;
break;
case 2:
switch(computer)
case 1: cout << "电脑赢了" << endl; break;
case 2: cout << "平局" << endl; break;
case 3: cout << "你赢了" << endl; break;
break;
case 3:
switch(computer)
case 1: cout << "你赢了" << endl; break;
case 2: cout << "电脑赢了" << endl; break;
case 3: cout << "平局" << endl; break;
break;
default:
cout << "输入错误!" << endl;
break;
return 0;
以上是一个简单的猜拳游戏实现,使用了嵌套的 switch 语句来处理不同的情况。
接下来,让我们看一下如何使用 C++ 的 switch 语句实现菜单程序。
```c++
#include <iostream>
using namespace std;
int main()
int choice;
cout << "请选择您要进行的操作:" << endl;
cout << "1.添加新的记录" << endl;
cout << "2.修改现有记录" << endl;
cout << "3.删除现有记录" << endl;
cout << "4.查看所有记录" << endl;
cout << "5.退出程序" << endl;
cin >> choice;
switch(choice)
case 1:
cout << "您选择了添加新的记录。" << endl;
break;
case 2:
cout << "您选择了修改现有记录。" << endl;
break;
case 3:
cout << "您选择了删除现有记录。" << endl;
break;
case 4:
cout << "您选择了查看所有记录。" << endl;
break;
case 5:
cout << "您选择了退出程序。" << endl;
break;
default:
cout << "输入错误,请重新选择。" << endl;
break;
return 0;
以上是一个简单的菜单程序实现,使用了 switch 语句来处理用户的不同选择。