< previous page page_524 next page >

Page 524
{
    char    inChar;
    Boolean badData = FALSE;

    do
    {
        cout < Enter a number from 1 through 5 : ;
        cin >> inChar;
        if ( ! isdigit (inChar) )
            badData = TRUE;                   //It's not a digit
        else
        {
            response = int(inChar - 0);
            if (response < 1 || response >> 5)
               badData = TRUE;               // It's a digit, but
        }                                    // it's out of   range
        if (badData)
            cout < Please try again. < endl;

            // Invariant:
            //     All previous values of inChar were either
            //     nondigit chars or were out of range
            //  && IF current inChar is a digit char
            //         response == numeric equivalent of inChar

    }   while (badData);
}
Converting to Lowercase and Uppercase
When working with character data, you sometimes find that you need to convert a lowercase letter to uppercase, or vice versa. Fortunately, the programming technique required to do these conversions is easya simple call to a library function is all it takes. Through the header file ctype.h, the standard library provides not only the is functions we have discussed, but also two value-returning functions named toupper and tolower. Here are their descriptions:
Header FileFunctionFunction TypeFunction Value
<ctype.h>toupper(ch)char*Uppercase equivalent of ch, if ch is a lowercase letter; ch, otherwise
<ctype.h>tolower (ch)charLowercase equivalent of ch, if ch is an uppercase letter; ch, otherwise
* Technically, both the parameter and the return value are of type int. But conceptually, the functions operate on character data.

 
< previous page page_524 next page >