< previous page page_413 next page >

Page 413
    return (fabs(angle1 + angle2 + angle3 - 180.0) < 0.00000001);
}
The following program fragment shows how function IsTriangle might be called:
cin >> angleA >> angleB >> angleC;
if (IsTriangle(angleA, angleB, angleC))             // Function call
    cout << The three angles form a valid triangle.;
else
    cout << Those angles do not form a triangle.;
The If statement is much easier to understand with the function than it would be if the entire condition were coded directly. When a conditional test is at all complicated, a Boolean function is in order.
The C++ standard library provides a number of helpful Boolean functions that let you test the contents of char variables. To use them, you #include the header file ctype.h. Here are some of the available functions; Appendix C contains a more complete list.
Header FileFunctionFunction TypeFunction Value
<ctype.h>isalpha(ch)
int
Nonzero, if ch is a letter (A,Z, az);
0, otherwise
<ctype.h>isalnum(ch)
int
Nonzero, if ch is a letter or a digit (AZ, az, 09);
0, otherwise
<ctype.h>isdigit(ch)
int
Nonzero, if ch is a digit (09);
0, otherwise
<cytpe.h>islower(ch)
int
Nonzero, if ch is a lowercase letter (az);
0, otherwise
<ctype.h>isspace(ch)
int
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Nonzero, if ch is a whitespace character (blank, newline, tab, carriage return, form feed);
0, otherwise
<ctype.h>isupper(ch)
int
Nonzero, if ch is an uppercase letter (AZ);
0, otherwise

Although Boolean is not a built-in data type in C++, the is functions behave like Boolean functions. They return an int value that is nonzero (true) or zero (false). These functions are convenient to use and make programs more readable. For example, the test
if (isalnum(inputChar))

 
< previous page page_413 next page >