简体   繁体   English

选择错误的选项时如何保持程序运行而不退出?

[英]How to keep program running without exiting when wrong option is chosen?

I am creating a library management system. 我正在创建一个图书馆管理系统。 I have a few menus for which the user can select an option but when the wrong option is chosen it should display an error message and ask the user to select another option. 我有一些菜单,用户可以选择一个选项,但是如果选择了错误的选项,它将显示一条错误消息,并要求用户选择另一个选项。 Currently after the user selects an option which is not available, the program exits. 当前,在用户选择一个不可用的选项之后,程序退出。

This is some of my code: 这是我的一些代码:

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;

        }

Also I have several options such as Add a new book, delete a book. 另外,我还有几个选项,例如添加新书,删除书。 When the user selects add a new book option and after adding a book the program exits. 当用户选择添加新书选项时,添加书后程序退出。 How I can make it after the user adds a book the system goes back to the menu. 用户添加一本书后,我该如何做,系统又回到菜单。

This is some of my code showing the switch case for the Book menu: 这是我的一些代码,显示了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;
             }
          }
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;
    }
}

You can add a default statement in you switch's body. 您可以在开关主体中添加默认语句。

...
label:

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

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

All I would do is put the main menu in a do while loop, as long as the user doesn't select exit (4) then the program should execute as many times as the user needs. 我要做的就是将主菜单放入do while循环中,只要用户未选择exit(4),那么程序就应根据用户需要执行多次。

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

This is how I did it for a similar project of mine. 这就是我为我的类似项目所做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM