简体   繁体   中英

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:

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 {
//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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