Javatpoint Logo
Javatpoint Logo

Menu-driven program in C++

A menu-driven program in the C++ programming language is a kind of interactive software application that gives the user a menu of options and lets them select from a list of actions or functionalities. These apps are frequently used in a variety of fields, including software applications, gaming, utilities, and more. Here is a theoretical breakdown of the fundamental ideas involved with C++ menu-driven programs:

  1. Interface for Users (UI)
    A text-based or graphical menu listing the available options or actions often makes up the user interface of a menu-driven program. Users use the keyboard, mouse, or touchscreen to enter their selection of an option.
  2. Looping Structure
    The menu-driven programs frequently use a looping structure, such as a do-while loop or a while loop, to repeatedly display the menu and wait for user input until a certain exit choice is selected. By doing this, it is made sure that the program is interactive until the user chooses to close it.
  3. User input
    Menu-driven programs depend heavily on user interaction. Typical input techniques, such cin for keyboard input or GUI elements like buttons for mouse interaction, are frequently used to collect inputs.
  4. Decision Structure
    A decision structure is employed, such as a switch statement or a string of if-else statements, to decide which action or code block to perform based on the user's selection. Each selection on the menu is matched to a certain circumstance.
  5. Designing in Modules
    Each menu item is typically implemented as a separate function or method to make the program structured and maintainable. Testing and debugging are made easier, and code modularity is encouraged.
  6. Handling errors
    Error management is frequently present in menu-driven programs to address improper user input. Users are given the option to fix incorrect input or select a viable choice using error messages that can be displayed.
  7. Assistance and documentation
    Complex menu-driven programs may have documentation or help options that give users advice on how to use certain features, how to use them correctly, or how to fix issues.
  8. Choice of Exit
    The capacity to gracefully exit a program is a crucial feature of menu-driven programs. In most cases, it entails giving consumers a clear "Exit" or "Quit" option that enables them to end the program and return to the operating system or the preceding menu.
  9. Evaluation and Validation
    The program must undergo extensive testing and validation to ensure that it functions as intended. It entails evaluating each menu item and dealing with unique situations or unexpected inputs.
  10. The user's experience (UX)
    In programs that rely on menus, the user experience is quite important. Design factors, including responsiveness, straightforward navigation, and clear and concise menu labelling, all contribute to a great user experience.
  11. Use cases
    Menu-driven programs are adaptable and can be used for a variety of tasks, including setting up settings, carrying out computations, managing data, managing data, playing games, and communicating with databases.
  12. Scalability and Extensibility
    The flexibility to introduce additional menu options or features in the future without drastically altering the program's code is something that developers frequently keep in mind while creating menu-driven programs.

Program:

Let's take an example to demonstrate a menu-driven program in C++:

Output:

MENU
1: Addition 
2: Subtraction
3: Multiply 
4: Divide 
5: Exit 
Enter your choice :1
Enter the two numbers: 10 20
The Sum is 30
MENU
1: Addition 
2: Subtraction
3: Multiply 
4: Divide 
5: Exit 
Enter your choice :10 20
Invalid Input
MENU
1: Addition 
2: Subtraction
3: Multiply 
4: Divide 
5: Exit 
Enter your choice : Invalid Input
MENU
1: Addition 
2: Subtraction
3: Multiply 
4: Divide 
5: Exit 
Enter your choice :3
Enter the two numbers: 10 20
The Product is 200
MENU
1: Addition 
2: Subtraction
3: Multiply 
4: Divide 
5: Exit 
Enter your choice :4
Enter the two numbers: 10 20
The Quotient is 0.5
MENU
1: Addition 
2: Subtraction
3: Multiply 
4: Divide 
5: Exit 
Enter your choice :6
Invalid Input
Enter your choice :5

Explanation:

  • In this example, the main() function is where the program begins. It declares the user's input variables (x, y, and choice).
  • The program uses a do-while loop to display a menu of choices for the user by calling the show_Choices() function.
  • The option (an integer) made by the user is entered and stored in the choice variable using cin.
  • The program to carry out the chosen action or quit depending on the user's selection, uses a switch statement.
  • The program calls the appropriate function (addition(), subtraction(), multiply(), divide()) for each arithmetic operation (addition, subtraction, multiplication, division), passing x and y as parameters and showing the outcome.
  • The loop ends, and the program is finished if the user chooses "Exit" (option 5).
  • An error message is shown if the user selects the incorrect option.
  • Each arithmetic operation (addition(), subtraction(), multiply(), and divide()) is implemented as a separate function, and division by zero is handled with an error message.

Conclusion:

C++ menu-driven programs give users an organized and engaging way to interact with software, making them appropriate for a variety of applications and user scenarios. They are a useful method for arranging and presenting a group of features to users in a user-friendly fashion.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA