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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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':
    		    break;
    		  case '5':
    		    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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
    			 }
    		  }

1
2
3
 case '5':
    		    exit(0);
    		    break;


Here you are telling the program to exit if the user write 5, that is an option not available. Is that what you want to do?
I just noticed that and I have changed the code to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (mainSelect)
		{
          case '1':
		    break;
		  case '2':
		    break;
		  case '3':
		    break;
		  case '4':
		    exit(0);
		    break;
		  case '5':
		    cout << "Invalid selection!" << endl;
		    break;
          default:
            cout<<"Incorrect selection. Please select from the given options." <<endl;
               
	    }

But still the program closes when the user enters an incorrect option such as 6.
First of all.

mainSelect = name[0];

name is a character, while mainSelect is a integer. You cant assign a character to an integer.

Change to char mainSelect;

Then you'd have to use a loop so the program wont exist. Put the mainSelect switch statement inside this loop

1
2
3
4
do
{

}while(mainSelect != '4');
Last edited on
Topic archived. No new replies allowed.