starting out with programming logic & design - chapter10_menu driven programming
DESCRIPTION
Starting Out With Programming Logic & Design - Chapter10_Menu Driven ProgrammingTRANSCRIPT
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Starting Out with Programming Logic & Design
Second Edition
by Tony Gaddis
Chapter 11:
Menu-Driven Programs
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-2
Chapter Topics11.1 Introduction to Menu-Driven Programs11.2 Modularizing a Menu-Driven Program11.3 Using a Loop to Repeat the Menu11.4 Multiple-Level Menus
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-3
11.1 Introduction to Menu-Driven Programs
A menu is a list of operations that are displayed by a program, in which a user can select which operations to perform
Figure 11-1 A menu
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-4
11.1 Introduction to Menu-Driven Programs
A decision structure can be used to perform menu selections– This can be accomplished through a case structure,
series of nested if-then-else statements– A case structure is easier to follow the flow– A case structure will provide a case for each option
in the menu, in addition to a default case
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-5
11.1 Introduction to Menu-Driven Programs
Figure 11-2 Flowchart for Program 11-
1
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-6
11.1 Introduction to Menu-Driven ProgramsValidating the menu selection can be done before
the case is processed, allowing for no need for a default
Display “Enter your selection”Input menuSelection
//validationWhile menuSelection < 1 OR menuSelection >3
Display “That is an invalid selection. Enter 1, 2 or 3”Input menuSelection
End While
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-7
11.2 Modularizing a Menu-Driven Program
Since a menu-driven program is capable of performing many tasks, it should be put into modules– A module should be written for each case that
could be processed– The options would simply call modules– Allows for a clear flow of the program
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-8
11.2 Modularizing a Menu-Driven Program
Figure 11-5 Flowchart for the main
module in Program 11-3
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-9
11.3 Using a Loop to Repeat the MenuMost menu-driven programs use a loop to
repeatedly display the menu after a task is performed– This allows the user of the program to run another
option without restarting the program– The menu can also contain on option for ending
the programDisplay “1. Convert inches to centimeters.”Display “2. Convert feet to meters.”Display “3. Convert miles to kilometers.”Display “4. End the program.”
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-10
11.3 Using a Loop to Repeat the Menu
Figure 11-7 Flowchart for the
main module in Program 11-5
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-11
11.4 Multiple-Level MenusA multiple-level menu has a main menu and one
or more submenus– Some complex programs require more than one
menu– A programmer should consider breaking up long
menus into multiple menus– This is essentially nested case structures
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-12
11.4 Multiple-Level MenusInstead of this type of long menu
1. Process a sale2. Process a return3. Add a record to the inventory file4. Search for a record in the inventory file5. Modify a record in the inventory file6. Delete a record in the inventory file7. Print an inventory list report8. Print a list of inventory items by cost9. Print a list of inventory items by age10. Print a list of inventory items by retail value
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-13
11.4 Multiple-Level MenusConvert to multiple menus such as
Main Menu1. Process a Sale or a Return2. Update the Inventory File3. Print an Inventory Report4. Exit the Program
Sales and Returns Menu5. Process a Sale6. Process a Return7. Go Back to the Main Menu
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-14
11.4 Multiple-Level MenusMore menus
Update Inventory File Menu1. Add a Record2. Search for a Record3. Modify a Record4. Delete a Record5. Go Back to the Main Menu
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-15
11.4 Multiple-Level MenusMore menus
Inventory Report Menu1. Print an inventory list report2. Print a list of inventory items by cost3. Print a list of inventory items by age4. Print a list of inventory items by retail value5. Go Back to the Main Menu