Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10: Using Menus and Validating Input Programming Logic and Design, Third Edition Comprehensive.

Similar presentations


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

1 Chapter 10: Using Menus and Validating Input Programming Logic and Design, Third Edition Comprehensive

2 2 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 Programming Logic and Design, Third Edition Comprehensive3 Objectives Use a case structure to manage a menu Create a program that uses a multilevel menu Validate input Understand types of data validation

4 Programming Logic and Design, Third Edition Comprehensive4 Using Interactive Programs Programs for which all the data items are gathered prior to running use batch processing Programs that depend on user input while the programs are running use interactive processing Many computer programs cannot be run in batches

5 Programming Logic and Design, Third Edition Comprehensive5 Using Interactive Programs ( continued ) Most programs must run interactively: –must interact with a user while they are running Example: Ticket reservation programs for airlines and theaters must select tickets while you are interacting with them, not at the end of the month Interactive computer programs are often called real-time applications: –run while a transaction is taking place, not at some later time

6 Programming Logic and Design, Third Edition Comprehensive6 Using Interactive Programs ( continued ) You can refer to interactive processing as online processing: –the user’s data or requests are gathered during the execution of the program while the computer is operating A batch processing system can be offline –you can collect data such as time cards or purchase information well ahead of the actual computer processing of the paychecks or bills

7 Programming Logic and Design, Third Edition Comprehensive7 Using Interactive Programs ( continued ) A menu program is a common type of interactive program –the user sees a number of options on the screen and can select any one of them –For example, an educational program that drills you on elementary arithmetic skills might display three options

8 Programming Logic and Design, Third Edition Comprehensive8 Using Interactive Programs ( continued ) The menu on the left is used in console applications: –require the user to enter a choice using the keyboard, The menu style on the right is used in graphical user interface applications: –allow the user to use a mouse or other pointing device

9 Programming Logic and Design, Third Edition Comprehensive9 Using a Single-Level Menu Suppose you want to write a program that displays a menu like the one shown previously in Figure 10-1 The program drills a student’s arithmetic skills –if the student chooses the first option, five addition problems are displayed –if the student chooses the second option, five subtraction problems are displayed Uses a single-level menu: –user makes a selection from only one menu before using the program for its ultimate purpose—arithmetic practice

10 Programming Logic and Design, Third Edition Comprehensive10 Using a Single-Level Menu ( continued ) Suppose you want to write a program that requires the user to enter a digit to make a menu choice The mainline logic for an interactive menu program is not substantially different from any of the other sequential file programs you’ve seen so far in this book You can create startUp(), looping(), and cleanUp() modules

11 Programming Logic and Design, Third Edition Comprehensive11 Using a Single-Level Menu ( continued ) The only difference between the mainline logic in Figure 10-2 and that of other programs you have worked with lies in the main loop control question When a program’s input data come from a data file, asking whether the input file is at the end-of- file ( eof ) condition is appropriate

12 Programming Logic and Design, Third Edition Comprehensive12 Using a Single-Level Menu ( continued )

13 Programming Logic and Design, Third Edition Comprehensive13 The displayMenu() Module of the Arithmetic Drill Program

14 Programming Logic and Design, Third Edition Comprehensive14 The looping() Module for the Arithmetic Drill Program

15 Programming Logic and Design, Third Edition Comprehensive15 The cleanUp() Module for the Arithmetic Drill Program

16 Programming Logic and Design, Third Edition Comprehensive16 Coding Modules as Black Boxes Any steps you want can occur within the addition() and subtraction() modules The contents of these modules do not affect the main structure of the program in any way Programmers often refer to the code in modules such as addition() and subtraction() as existing within a black box: –the module statements are “invisible” to the rest of the program

17 Programming Logic and Design, Third Edition Comprehensive17 Coding Modules as Black Boxes ( continued ) When programmers develop systems containing many modules, they often code “empty” black box procedures, called stubs Figure 10-7 shows a possible addition() module The module: –displays four addition problems one at a time – waits for the user’s response –displays a message indicating whether the user is correct

18 Programming Logic and Design, Third Edition Comprehensive18 The addition() Module, Version 1

19 Programming Logic and Design, Third Edition Comprehensive19 Coding Modules as Black Boxes ( continued ) The addition() module shown in Figure 10-7 works, but it is repetitious –a basic set of statements repeats four times, changing only the actual problem values that the user should add, and the correct answer to which the user’s response is compared A more elegant solution: –store the problem values in arrays –use a loop

20 Programming Logic and Design, Third Edition Comprehensive20 Coding Modules as Black Boxes ( continued )

21 Programming Logic and Design, Third Edition Comprehensive21 Coding Modules as Black Boxes ( continued ) The addition() module in Figure 10-9 is more compact and efficient than the module shown in Figure 10-7 However, it still contains flaws A student will not want to use the addition() module more than two or three times Every time a user executes the program, the same four addition problems are displayed

22 Programming Logic and Design, Third Edition Comprehensive22 The addition() Module, Version 2, Using Arrays

23 Programming Logic and Design, Third Edition Comprehensive23 Coding Modules as Black Boxes ( continued ) Once students have solved all the addition problems, they probably will be able to provide memorized answers without practicing arithmetic skills at all Fortunately, most programming languages provide you with built-in modules, or functions: –subroutines that automatically provide a mathematical value such as a square root, absolute value, or random number

24 Programming Logic and Design, Third Edition Comprehensive24 Coding Modules as Black Boxes ( continued ) Figure 10-10 shows an addition() module in which two random numbers, each 10 or less, are generated for each of four arithmetic problems Using this technique, –you do not have to store values in an array –users encounter different addition problems every time they use the program

25 Programming Logic and Design, Third Edition Comprehensive25 The addition() Module, Version 3, Using Random Values

26 Programming Logic and Design, Third Edition Comprehensive26 Making Improvements to a Menu Program In programming, there is a saying that no program is every really completed - you always can continue to make improvements For example, the looping() module in Figure 10- 11 shows that a helpful message (“You must select 1, 2, or 3”) appears when the user selects an inappropriate option

27 Programming Logic and Design, Third Edition Comprehensive27 Making Improvements to a Menu Program ( continued ) However, if users do not understand the message, or simply do not stop to read the message, they might keep entering invalid data As a user-friendly improvement to your program, you can add a counter that keeps track of a user’s invalid responses

28 Programming Logic and Design, Third Edition Comprehensive28 Adding an Error Message to the looping() Module for the Arithmetic Drill Program

29 Programming Logic and Design, Third Edition Comprehensive29 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 Many menus include more than three options, but the main logic of such programs is not substantially different from that in programs with only three You just include more decisions that lead to additional subroutines

30 Programming Logic and Design, Third Edition Comprehensive30 Using the Case Structure to Manage a Menu ( continued ) All menu-driven programs should be user- friendly: –they should make it easy for the user to make desired choices Instead of requiring a user to type numbers to select an arithmetic drill – you can improve the menu program by allowing the user the additional option of typing the first letter of the desired option, for example A for addition

31 Programming Logic and Design, Third Edition Comprehensive31 Using the Case Structure to Manage a Menu ( continued ) To enable the menu program to accept alphabetic characters as a variable named response, you must make sure you declare response as a character variable in the startUp() module

32 Programming Logic and Design, Third Edition Comprehensive32 Main Logic of Program Containing Four Optional Arithmetic Drills

33 Programming Logic and Design, Third Edition Comprehensive33 Using the Case Structure to Manage a Menu ( continued ) Programmers often overlook the fact that computers recognize uppercase letters as being different from their lowercase counterparts Figure 10-16 shows the case structure that performs the menu option selection when the user can enter a variety of responses for each menu choice

34 Programming Logic and Design, Third Edition Comprehensive34 Using the Case Structure to Manage a Menu ( continued )

35 Programming Logic and Design, Third Edition Comprehensive35 Using Multilevel Menus Sometimes, a program requires more options than can easily fit in one menu When you need to present the user with a large number of options, several potential problems arise: –If there are too many options to fit on the display at one time, the user might not know additional options are available –Overcrowded screen is not visually pleasing –Users become confused and frustrated when presented with too many choices

36 Programming Logic and Design, Third Edition Comprehensive36 Using Multilevel Menus ( continued ) 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 from which the user can make further, more refined selections

37 Programming Logic and Design, Third Edition Comprehensive37 Using Multilevel Menus ( continued )

38 Programming Logic and Design, Third Edition Comprehensive38 Flowchart and Pseudocode for startUp() Module for Multilevel Menu Program

39 Programming Logic and Design, Third Edition Comprehensive39 Flowchart and Pseudocode for displayMenu() Module for Multilevel Menu Program

40 Programming Logic and Design, Third Edition Comprehensive40 Flowchart and Pseudocode for looping() Module for Multilevel Menu Program

41 Programming Logic and Design, Third Edition Comprehensive41 Validating Input Menu programs rely on user input to select one of several actions Other types of programs also require a user to enter data Users will make incorrect choices because: – they don’t understand the valid choices – they make typographical errors To improve programs, employ defensive programming: –prepare for all possible errors before they occur

42 Programming Logic and Design, Third Edition Comprehensive42 Validating Input ( continued ) Incorrect user entries are by far the most common source of computer errors Validating input: checking the user’s responses to ensure they fall within acceptable bounds –Does not eliminate all program errors The correct action to take when you find invalid data depends on the application

43 Programming Logic and Design, Third Edition Comprehensive43 Validating Input ( continued ) If program uses data file – print a message so someone can correct the invalid data, OR –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

44 Programming Logic and Design, Third Edition Comprehensive44 Validating Input ( continued ) 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

45 Programming Logic and Design, Third Edition Comprehensive45 Validating Input ( continued ) The logic shown in Figure 10-26 intends to make sure that the user enters a Y or N However, if you use the logic shown in Figure 10- 26, all users will see the “invalid response” error message, no matter what they type

46 Programming Logic and Design, Third Edition Comprehensive46 Understanding Types of Data Validation The data used within computer programs are varied Thus, validating data requires a variety of methods

47 Programming Logic and Design, Third Edition Comprehensive47 Validating a Data Type Some programming languages allow you to check data items to make sure they are the correct data type Although this technique varies from language to language, you can often make a statement like the one shown in Figure 10-29 In this program segment, isNumeric represents a method call; it is used to check whether the entered employeeSalary falls within the category of numeric data

48 Programming Logic and Design, Third Edition Comprehensive48 Validating a Data Type ( continued )

49 Programming Logic and Design, Third Edition Comprehensive49 Validating a Data Range Sometimes, a user response or other data must fall within a range of values –For example, when the user enters a month, you typically require it to fall between 1 and 12, inclusive The method you use to check for a valid range is similar to one you use to check for an exact match –you continue to prompt for and receive responses while the user’s response is out of range, as shown in Figure 10-30

50 Programming Logic and Design, Third Edition Comprehensive50 Validating a Data Range ( continued )

51 Programming Logic and Design, Third Edition Comprehensive51 Validating Reasonableness and Consistency of Data Data items can be the correct type and within range, but still be incorrect However, there are many data items that you can check for reasonableness –If you make a purchase on May 3, 2004, then the payment cannot possibly be due prior to that date –If your ZIP code is 90201, your state of residence cannot be New York

52 Programming Logic and Design, Third Edition Comprehensive52 Validating Reasonableness and Consistency of Data ( continued ) Each of the previous examples involves comparing two data fields for reasonableness and consistency You should consider making as many such comparisons as possible when writing your own programs

53 Programming Logic and Design, Third Edition Comprehensive53 Validating Presence of Data Sometimes, data are missing from a file, either for a reason or by accident Many programming languages allow you to check for missing data with a statement similar to if emailAddress is blank perform noEmailModule() You can place any instructions you like within the noEmailModule(), including –forcing the field to a default value or –issuing an error message

54 Programming Logic and Design, Third Edition Comprehensive54 Summary 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, real-time, online processing When you code a module as a black box, the module statements are invisible to the rest of the program

55 Programming Logic and Design, Third Edition Comprehensive55 Summary ( continued ) 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 You can use the case structure to make decisions when you need to test a single variable against several possible values

56 Programming Logic and Design, Third Edition Comprehensive56 Summary ( continued ) 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 –presence of data


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

Similar presentations


Ads by Google