C++11 如何使用开关盒c+退出while循环+;

C++11 如何使用开关盒c+退出while循环+;,c++11,C++11,激活案例“6”时,如何退出案例“5”?我似乎无法从循环中退出。我试着休息一下;在案例“6”中,但这似乎不起作用 case '5': //receive a '5' from serial com port and execute auto docking while(1) { north =0; south=0; east=0; west=0; scanmovement(); for(index=0;index<7;index++) { getdirection(

激活案例“6”时,如何退出案例“5”?我似乎无法从循环中退出。我试着休息一下;在案例“6”中,但这似乎不起作用

case '5': //receive a '5' from serial com port and execute auto docking
 while(1)
{
 north =0;
 south=0;
 east=0;
 west=0;
 scanmovement();
 for(index=0;index<7;index++)
  {
    getdirection(index);
  }  
  arlomove();
  obstacleavoidance();

 }
  break;
 case '6':   //receive '6' from serial com port and stop auto docking
 //somethingsomething
 //break out of while loop in case '5' when this is activated
 break;  
案例“5”://从串行com端口接收“5”并执行自动对接
而(1)
{
北=0;
南=0;
东=0;
西=0;
扫描运动();
for(index=0;index
switch(var)//您的变量
{
案例“5”:
而(1)
{
北=0;
南=0;
东=0;
西=0;
扫描运动();

对于(index=0;index您不能仅仅停止案例“5”,但您可以这样做

case '5': 
  while(!stop)
  {

  } 
  break;
case '6':
  //somethingsomething
  stop = true;
  break;

如果它在函数中,当您达到5时,您可以简单地
返回

比如说,

void function(int n) {
    while(1) {
        switch(n) {
            case 5:
                blahblah~~
                return;
        }
    }
}
或者,如果是最好的选择,您可以使用
goto

void main() {
    int n = 5;
    while(1) {
        switch(n) {
            case 5:
                blahblah~~
                goto theEnd;
        }
    }

    theEnd:
        blahblah~~
}

请编辑您的代码,以显示退出while循环的原因,最好是
if
语句。因此,要清楚,这是多线程的,或者以其他方式可重入(例如,通过信号处理程序,或者在
案例“5”中调用的方法之一:
循环)?否则,您将永远无法使用
案例“6”:
来终止循环。我已编辑了代码。这是函数的内部吗?是的,它在主函数的while循环中运行。您在回答中提出的建议不会阻止循环永远继续。
void main() {
    int n = 5;
    while(1) {
        switch(n) {
            case 5:
                blahblah~~
                goto theEnd;
        }
    }

    theEnd:
        blahblah~~
}