CTCH 233 mini Menus

advertisement
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.
Menus
This lesson introduces a well-known algorithm for using a menu to
interact with the user.
Christine S. Wolfe
Ohio University Lancaster
2008-Aug-01
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)
2)
3)
0)
Enter new employee profile
Edit employee profile
Enter work schedule
Quit
Response:
The central part of a menu program
is implemented via a switch
statement.
Computer Learning Center
Please select one of the following options:
1)
2)
3)
0)
Enter new employee profile
Edit employee profile
Enter work schedule
Quit
Response:
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)
2)
3)
0)
Enter new employee profile
Edit employee profile
Enter work schedule
Quit
Response:
Typically, at the end of processing an
option, control is returned to the
menu.
Computer Learning Center
Pleasework
Enter
select
schedule:
one of the
Employee ID:
following
options:
123
Mon: 8
Tue:Enter
1)
8
new employee profile
Wed:Edit
2)
8
employee profile
Thu:Enter
3)
8
work schedule
Fri:Quit
0)
8
Sat: 8
Sun: 8
Response:
3
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)
2)
3)
0)
Enter new employee profile
Edit employee profile
Enter work schedule
Quit
Computer Learning Center
Enter new employee profile:
Employee ID:
First Name:
Last Name:
SSN:
Date of Birth:
Date of Hire:
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
Response:
Computer Learning Center
Which logic structure causes
the program to execute a set
of code over and over again?
Enter work schedule:
Employee ID:
123
Mon: 8
Tue: 8
Wed: 8
Thu: 8
Fri: 8
Sat: 8
Sun: 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
Another Example, this one with code…
Arithmetic Menu
+) Addition
-) Subtraction
*) Multiplication
/) Division
%) Modulo
Q) Quit
Enter the symbol for your selection or Q to quit
Menus are implemented as a switch inside a loop.
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();
}
/* FUNCTION PROTOTYPES */
char Menu(void);
int Addition(void);
int Subtraction(void);
int Multiplication(void);
int Division(void);
int Modulo(void);
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;
}
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
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