在a++中返回菜单选择

Return to a menu choice in A++

本文关键字:菜单 选择 返回 a++      更新时间:2023-10-16

这是一个很难的问题,我已经看到了这个问题的答案,但这些答案对于像我这样的新手来说还不够愚蠢。所以,我的问题是,如果我进入一个菜单,选择一个选项,完成那个选项,然后我需要回到主菜单去做另一个选项,有人能用菜鸟术语解释吗?谢谢。

一个例子:

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    cout << "Main Menu:" << endl;
    cout << "01. This Option" << endl;
    cout << "02. Another Option" << endl;
    int option;
    cin >> option;
    if(option==1)
    {
        //do stuff and then go back somehow
    }
    if(option==2)
    {
        //do other stuff and come back somehow
    }
    else
    {
        cout << "INVALID!" << endl;
        system("Pause");
        return 0;
    }
}

引入一个循环。

#include <iostream>
#include <cstdlib> //this is needed to use the function system
//#include <windows.h> //not used here
using namespace std;
int main()
{
    for(;;) //infinite loop
    {
        cout << "Main Menu:" << endl;
        cout << "01. This Option" << endl;
        cout << "02. Another Option" << endl;
        int option;
        cin >> option;
        if(option==1)
        {
            //do stuff and then go back somehow
        }
        else if(option==2) // else should be here, or the program will end when option==1
        {
            //do other stuff and come back somehow
        }
        else
        {
            cout << "INVALID!" << endl;
            system("Pause");
            return 0;
        }
    }
}