首页 > 解决方案 > 我可以从 C++ 中的某一行开始代码吗?

问题描述

基本上我希望代码在完成某些操作后返回并从某一行重新开始。例如,我有一个菜单,您可以在其中选择一个操作,如果该操作中的所有内容都已完成,我希望它再次显示该菜单。

这里有一些粗略的代码来说明。

    int option;

cout << "1";
cout << "2";
cout << "3";

cout << "What option do you choose?";
cin >> option;

if (option = 1) {
    //do something here 
    //if finished start again with the menu options
} else if (option = 2) {
    //do something here 
    //if finished start again with the menu options
} else {
    //do something here 
    //if finished start again with the menu options
}

标签: c++

解决方案


int option;
bool escape = true;

cout << "1\n2\n3\n";

     while(escape){

     cout << "What option do you choose?";
     cin >> option;

     if (option == 1) { // double == sign for checking. = is for assignment.
       //do something here 
       //if finished start again with the menu options
     } 
     else if (option == 2) {
       //do something here 
       //if finished start again with the menu options
     } 
     else if (option == 3) {
        //do something here 
        //if finished start again with the menu options
     }
     else {
        escape = false; // this is just to break the loop. 
     }
}

推荐阅读