|
|
|
|
|
|
|
character of the month is invalid) or erroneous output (if the second or third characters are invalid). Let's rewrite the GetMonth, J_Month, A_Month, and M_Month functions to incorporate active error detection. The first character must be checked to see whether it is one of the valid first letters. If not, the error must be reported. If the first character is an A, a J, or an M, the second or third character (or both) must be checked. |
|
|
|
|
|
|
|
|
The first character should be checked where it is read, in the body of Get_Month. To handle invalid characters, we add a default label to the Switch statement. The second and/or third character should be checked in the function that uses it. Functions J_Month, A_Month, and M_Month need an extra parameter to let GetMonth know whether an error occurred. If an error has occurred, GetMonth must notify the user and request another input. This implies that the Switch statement must be in a loop that continues until a month has been recognized. |
|
|
|
|
|
|
|
|
This scheme does not check the entire spelling of each month. It checks to see only that there are enough letters to recognize a month. |
|
|
|
|
|
|
|
|
We said that J_Month, A_Month, and M_Month need an extra parameter to report to GetMonth whether an error occurred. A Boolean flag is appropriate for reporting an error, but now we have a problem. These three functions are value-returning functions and return only one value. We need to return two valuesthe appropriate month and a Boolean flag. Let's change these valuereturning functions into void functions, returning two results through the parameter list. Because void functions should be named using imperative verbs, we rename the three functions as CheckJ$ecs;, CheckA, and CheckM. |
|
|
|
|
|
|
|
|
The following code assumes that we have defined our own Boolean type. |
|
|
|
|
|
|
|
|
void CheckA( char, Months&, Boolean& );
void CheckJ( char, char, Months&, Boolean& );
void CheckM( char, Months&, Boolean& );
void GetMonth( Months& );
.
.
.
//******************************************************************
void GetMonth( /* out */ Months& month ) // User's desired month
// Inputs a month after prompting the user
// Postcondition:
// User has been prompted to enter a month
// && Only the characters needed to determine the month are read
// (the remaining characters on the input line are read and
// discarded)
// && On invalid input, the user has been repeatedly prompted to
// type a correct month
// && month == value of type Months corresponding to user's input
|
|
|
|
|
|