|
|
|
|
|
|
|
String9 strMonthAry[12] = // Parallel table in string form
{
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
int index; // Index of located month
Boolean found; // True if month is valid
do
{
cout << "Please enter month, capitalizing first letter."
<< endl;
cin.get(strMonth, 10); // Input at most 9 chars and
// leave room for '\0'
cin.ignore(100, '\n'); // Consume '\n' because the
// "get" routine does not
Search(strMonthAry, strMonth, 12, index, found);
if (found)
month = monthAry[index];
else
cout << "Month is misspelled." << endl << endl;
// Invariant:
// All values of strMonth prior to current
// value were invalid
// && found == TRUE if and only if current
// strMonth is valid
} while ( !found );
}
//******************************************************************
void Search(
/* in */ const String9 strMonthAry[], // List to be searched
/* in */ const String9 strMonth, // Search value
/* in */ int length, // Length of list
/* out */ int& index, // Location if found
/* out */ Boolean& found ) // True if value found
// Searches strMonthAry for strMonth, returning the index
// of strMonth if strMonth was found
// Precondition:
// length <= 12
// && strMonthAry[0..length-1] are assigned
// && strMonth is assigned |
|
|
|
|
|