返回A ++中的菜单选项

时间:2015-09-27 01:42:20

标签: c++ loops menu

这是一个很难回答的问题,而且我已经看到了这个问题的答案,但对于像我这样的菜鸟来说,这些答案并不是愚蠢的。所以,我的问题是,如果我要进入菜单,选择一个选项,完成该选项,我需要返回主菜单再做一个选项,任何人都可以用Noobie条款解释一下吗?感谢。

一个例子:

#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;
    }
}

1 个答案:

答案 0 :(得分: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;
        }
    }
}
相关问题