Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— Presentation transcript:

1 This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

2 Menus Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 This lesson introduces a well-known algorithm for using a menu to interact with the user.

3 A menu program is one that presents the user with a list of choices. The user enters a value in order to select one of the choices. Processing continues based on the value selected. At the end of processing the user's selection, control returns to the menu. The program ends when the user selects an exiting option. Computer Learning Center Please select one of the following options: 1) Enter new employee profile 2) Edit employee profile 3) Enter work schedule 0) Quit Response:

4 The central part of a menu program is implemented via a switch statement. Computer Learning Center Please select one of the following options: 1) Enter new employee profile 2) Edit employee profile 3) Enter work schedule 0) Quit Response:

5 When using a switch, the user responses are limited to ints and chars. Why? Are there other advantages to limiting the choices to ints and chars? Computer Learning Center Please select one of the following options: 1) Enter new employee profile 2) Edit employee profile 3) Enter work schedule 0) Quit Response:

6 Computer Learning Center Please select one of the following options: 1) Enter new employee profile 2) Edit employee profile 3) Enter work schedule 0) Quit Response: 3 Computer Learning Center Enter work schedule: Employee ID: 123 Mon: 8 Tue: 8 Wed: 8 Thu: 8 Fri: 8 Sat: 8 Sun: 8 Typically, at the end of processing an option, control is returned to the menu. Computer Learning Center Please select one of the following options: 1) Enter new employee profile 2) Edit employee profile 3) Enter work schedule 0) Quit Response:

7 Computer Learning Center Edit employee profile: Employee ID: 123 First Name: Mike Last Name: Smith SSN: 000-00-0000 Date of Birth: 12 15 1978 Date of Hire: 02 01 2007 Typically, at the end of processing an option, control is returned to the menu and the process starts all over again. Computer Learning Center Please select one of the following options: 1) Enter new employee profile 2) Edit employee profile 3) Enter work schedule 0) Quit Response: Computer Learning Center Enter work schedule: Employee ID: 123 Mon: 8 Tue: 8 Wed: 8 Thu: 8 Fri: 8 Sat: 8 Sun: 8 Computer Learning Center Enter new employee profile: Employee ID: First Name: Last Name: SSN: Date of Birth: Date of Hire: Which logic structure causes the program to execute a set of code over and over again?

8 The menu processing switch is nested inside a loop and becomes the body of the loop. Initialize the test The test Affect the test

9 Arithmetic Menu +) Addition -) Subtraction *) Multiplication /) Division %) Modulo Q) Quit Enter the symbol for your selection or Q to quit Another Example, this one with code…

10 Operator = Menu(); while (Operator != 'q' && Operator != 'Q') { switch (Operator) { case '+':Result = Addition(); ++NumAttempt; NumCorrect = NumCorrect + Result; break; [NOTE: to save space, the other cases are not shown here.] default: printf("\nInvalid Entry."); fflush(stdin); getchar(); } Operator = Menu(); } Menus are implemented as a switch inside a loop.

11 /* FUNCTION PROTOTYPES */ char Menu(void); int Addition(void); int Subtraction(void); int Multiplication(void); int Division(void); int Modulo(void);

12 char Menu(void) { char Choice; /* MENU SELECTION MADE BY THE USER */ /* DISPLAY THE MENU */ system("cls"); printf("\n\n"); printf("\nArithmetic Menu\n"); printf("\n+) Addition"); printf("\n-) Subtraction"); printf("\n*) Multiplication"); printf("\n/) Division"); printf("\n%) Modulo"); printf("\nQ) Quit"); /* PROMPT FOR USER SELECTION */ printf("\n\nEnter the symbol for your selection or Q to quit "); scanf("\n%c", &Choice); return Choice; }

13 int Addition(void) { /* PERFORM ADDITION */ float Operand1; float Operand2; float PlayerAnswer; float CorrectAnswer; system("cls"); printf("\nEnter the first operand "); fflush(stdin); scanf("\n%f", &Operand1); printf("\nEnter the second operand "); fflush(stdin); scanf("\n%f", &Operand2); printf("\nEnter your answer to the problem: %f + %f = ", Operand1, Operand2); fflush(stdin); scanf("\n%f", &PlayerAnswer); continued on the next slide

14 CorrectAnswer = Operand1 + Operand2; if (PlayerAnswer == CorrectAnswer) { printf("\n\nCorrect!!!. Hit any key to return."); fflush(stdin); getchar(); return 1; } else { printf("\n\nSorry. The correct answer is %f", CorrectAnswer); fflush(stdin); getchar(); return 0; } } /* this curly brace closes the body of the function definition the opening brace is on the previous slide */


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google