如果选择了错误的选项,如何在不退出的情况下保持程序运行?

时间:2015-04-07 10:23:23

标签: c++ xcode menu

我正在创建一个图书馆管理系统。我有一些菜单,用户可以选择一个选项但是当选择了错误的选项时,它应该显示一条错误消息并要求用户选择另一个选项。目前,在用户选择了不可用的选项后,程序将退出。

这是我的一些代码:

while(1)
    {
        int mainSelect;
        int studentOption;
        int dvdOption;
        int bookOption;
        char name[30];


        // Ask the user to select an option
        cout << "****************************************************" << endl;
        cout << "*******************  Main Menu  ********************" << endl;
        cout << "****************************************************" << endl;
        cout << "*                                                  *" << endl;
        cout << "* PROGRAM          DESCRIPTION                     *" << endl;
        cout << "* ------------------------------------------------ *" << endl;
        cout << "*                                                  *" << endl;
        cout << "*   1              DVD                             *" << endl;
        cout << "*                                                  *" << endl;
        cout << "*   2              Books                           *" << endl;
        cout << "*                                                  *" << endl;
        cout << "*   3              Students                        *" << endl;
        cout << "*                                                  *" << endl;
        cout << "*   4              EXIT                            *" << endl;
        cout << "*                                                  *" << endl;
        cout << "* ------------------------------------------------ *" << endl;
        cout << "*                                                  *" << endl;
        cout << "****************************************************" << endl;

        // Read user selection
        cin.getline( name, 80);
        mainSelect = name[0];

        // Switch statement to select between the options
        switch (mainSelect)
        {
          case '1':
            break;
          case '2':
            break;
          case '3':
            break;
          case '4':
            exit(0);
            break;
          case '6':
            cout << "Invalid selection!" << endl;
            break;
          default:
            cout<<"Incorrect selection. Please select from the given options." <<endl;

        }

此外,我还有几个选项,例如添加新书,删除书籍。当用户选择添加新书选项时,在添加书籍后,程序退出。在用户添加书籍后系统返回菜单后如何制作。

这是我的一些代码,显示了Book菜单的开关案例:

else if (mainSelect == '2')
        {
            cout << "****************************************************" << endl;
            cout << "*******************  Book Menu  ********************" << endl;
            cout << "****************************************************" << endl;
            cout << "*                                                  *" << endl;
            cout << "* PROGRAM          DESCRIPTION                     *" << endl;
            cout << "* ------------------------------------------------ *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   1              Issue a book                    *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   2              Return a book                   *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   3              Add a new book                  *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   4              Update a book                   *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   5              Delete a book                   *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   6              Search for a book               *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   7              Show all books                  *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   8              Return to the previous Menu     *" << endl;
            cout << "*                                                  *" << endl;
            cout << "*   9              Exit                            *" << endl;
            cout << "*                                                  *" << endl;
            cout << "* ------------------------------------------------ *" << endl;
            cout << "*                                                  *" << endl;
            cout << "****************************************************" << endl;

            cin.getline(name, 80);
            bookOption = name[0];

            switch(bookOption)
            {
              case '1':
                book2.issueBook();
                break;
              case '2':
                book2.returnBook();
                break;
              case '3':
                book2.insertBook();
                break;
              case '4':
                book2.updateBook();
                break;
              case '5':
                book2.deleteBook();
                break;
              case '6':
                char barcode[6];
                cout<<"Enter The book barcode: " <<endl;
                cin>>barcode;
                book2.searchBook(barcode);
                break;
              case '7':
                book2.showallBooks();
                break;
              case '8':
                break;
              case '9':
                exit(0);
                break;
             }
          }

3 个答案:

答案 0 :(得分:1)

bool processed = false;
while(!processed)
{
    cin.getline( name, 80);
    mainSelect = name[0];
    switch(mainSelect)
    {
        case '1':
            processed = true;
            break;
            ...
        case '5':
        default:
            processed = false;
            break;
    }
}

答案 1 :(得分:0)

您可以在开关的正文中添加默认语句。

...
label:

cin.getline( name, 80);
mainSelect = name[0];

switch(){
    .....
    default: goto label;
    ......
}
......

答案 2 :(得分:0)

我要做的就是将主菜单放在do while循环中,只要用户没有选择exit(4),程序就应该按照用户的需要执行多次。

do {
//main menu 
//switch statement that lets them add books, etc.
} while (mainSelect != 4)

这就是我为我的类似项目所做的。