Presentation is loading. Please wait.

Presentation is loading. Please wait.

10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10.

Similar presentations


Presentation on theme: "10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10."— Presentation transcript:

1 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

2 Chapter 102 Objectives After studying Chapter 10, you should be able to: Understand the need for interactive, menu-driven programs Create a program that uses a single-level menu Code modules as black boxes Improve menu programs

3 10 Chapter 103 Objectives After studying Chapter 10, you should be able to: Use a case structure to manage a menu Create a program that uses a multilevel menu Validate input Understand types of data validation

4 10 Chapter 104 Using Interactive Programs Programs for which all the data are gathered prior to running use batch processing Programs that depend on user input while they are running use interactive processing Interactive computer programs are often called real- time applications, because they run while a transaction is taking place, not at some later time You can refer to interactive processing as online processing, because the user’s data or requests are gathered during the execution of the program

5 10 Chapter 105 Using Interactive Programs A batch processing system can be off-line; that is, you can collect data such as time cards or purchase information well ahead of the actual computer processing of the paychecks or bills A menu program is a common type of interactive program in which the user sees a number of options on the screen and can select any one of them

6 10 Chapter 106 Using a Single-Level Menu The program in Figure 10-1 uses a single-level menu; that is, the user makes a selection from only one menu before using the program for its ultimate purpose— arithmetic practice

7 10 Chapter 107 Mainline Logic for Arithmetic Drill Menu Program

8 10 Chapter 108 Using a Single-Level Menu The startUp() module in the arithmetic drill program defines variables and opens files

9 10 Chapter 109 Using a Single-Level Menu When the looping() module ends, control passes to the main program

10 10 Chapter 1010 Coding Modules as Black Boxes Programmers often refer to modules such as addition() and subtraction() as existing within a black box meaning that the module statements are “invisible” to the rest of the program When programmers develop systems containing many modules, they often code “empty” black box procedures called stubs Most programming languages provide you with built-in modules or functions, which are subroutines that automatically provide a mathematical value such as a square root, absolute value, or random number

11 10 Chapter 1011 Coding Modules as Black Boxes You can make many additional improvements to any of the addition() modules shown in Figures 10-7, 10-9, and 10-10

12 10 Chapter 1012 Making Improvements to a Menu Program The programmer can assist the user by displaying a message when the selected response is not one of the allowable menu options, as shown in Figure 10-11

13 10 Chapter 1013 Using the Case Structure to Manage a Menu The arithmetic drill program contains just three valid user options: numeric entries that represent Addition, Subtraction, or Quit All menu-driven programs should be user- friendly, meaning that they should make it easy for the user to make desired choices Programmers often overlook the fact that computers recognize uppercase letters as being different from their lowercase counterparts

14 10 Chapter 1014 Menu Program Using the Case Structure

15 10 Chapter 1015 Menu Program Using the Case Structure with Multiple Allowed Responses

16 10 Chapter 1016 Using Multilevel Menus When you need to present the user with a large number of options, you invite several potential problems: –Not all the options will appear on the screen, and the user might not realize that additional options are available –The screen is too crowded to be visually pleasing when you try to force all the options to fit on the screen –Users become confused and frustrated when you present them with too many choices

17 10 Chapter 1017 Using Multilevel Menus When you have many menu options to present, using a multilevel menu might be more effective than using a single-level menu With a multilevel menu, the selection of a menu option leads to another menu where the user can make further, more refined selections You refer to a menu that controls whether or not the program will continue as the main menu of a program Alternatively, the user can choose to continue the program, selecting an Addition, Subtraction, Multiplication, or Division arithmetic drill

18 10 Chapter 1018 Using Multilevel Menus No matter which drill the user chooses you can display a second menu like the one shown in Figure 10-18 A second-level menu is a submenu

19 10 Chapter 1019 Third Menu for Arithmetic Drill Program

20 10 Chapter 1020 Using Multilevel Menus You would not need to learn any new techniques to create as many levels of menus as the application warrants The module that controls each new level can: 1.Display a menu 2.Accept a response 3.While the user does not select the quit option for the specific menu level, perform another module based on the selection (or inform the user of an error) 4.Display the menu and accept a response again

21 10 Chapter 1021 Validating Input The programs you write will be improved if you employ defensive programming, which means trying to prepare for all possible errors before they occur You can circumvent potential problems caused by a user’s invalid data entries by validating the user’s input Validating input involves checking the user’s responses to ensure they fall within acceptable bounds

22 10 Chapter 1022 Validating Input Alternatively, you can force the invalid data to a default value Forcing a field to a value means you override incorrect data by setting the field to a specific value New programmers often make the following two kinds of mistakes when validating data: –They use incorrect logic to check for valid responses when there is more than one possible correct entry –They fail to account for the user making multiple invalid entries

23 10 Chapter 1023 Validating Input

24 10 Chapter 1024 Best Method for Validating User Response

25 10 Chapter 1025 Understanding Types of Data Validation Some of the techniques you want to master include validating: –Data type –Range –Reasonableness and consistency of data –Presence of data

26 10 Chapter 1026 Validating a Data Type Some programming languages allow you to check data to make sure they are the correct type

27 10 Chapter 1027 Validating a Data Range Sometimes a user response or other data must fall within a range of values

28 10 Chapter 1028 Validating Reasonableness and Consistency of Data Data items can be the correct type and within range, but still be incorrect You have experienced this phenomenon yourself if anyone has ever misspelled your name or over billed you There are many data items that you cannot check for reasonableness; it is just as reasonable that your name is Catherine as it is that your name is Katherine or Kathryn

29 10 Chapter 1029 Validating Presence of Data Sometimes data are missing from a file, either on purpose or by accident A job applicant might fail to submit an entry for the salaryAtPreviousJob field or a client has no entry for the emailAddress field Good defensive programs try to foresee all possible inconsistencies and errors

30 10 Chapter 1030 Summary Programs for which all the data are gathered prior to running use batch processing When you create a single-level menu, the user makes a selection from only one menu before using the program for its ultimate purpose When you code a module as a black box, the module statements are invisible to the rest of the program A programmer can improve a menu program and assist the user by displaying a message when the selected response is not one of the allowable menu options

31 10 Chapter 1031 Summary You can use the case structure to make decisions when you need to test a single variable against several possible values When a program requires more options than can easily fit in one menu, you can use a multilevel menu You can circumvent potential problems caused by a user’s invalid data entries by validating the user’s input Some of the techniques you want to master include: validating data type, range, reasonableness and consistency of data, and presence of data


Download ppt "10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10."

Similar presentations


Ads by Google