< previous page page_372 next page >

Page 372
(This message is potentially confusing. It doesn't mean that studentCount is greater than zero. In fact, it's just the opposite. The message tells you that the assertion studentCount > 0 is FALSE.)
Executable assertions have a profound advantage over assertions expressed as comments: the effect of a false assertion is highly visible (the program terminates with an error message). The assert function is therefore valuable in software testing. A program under development might be filled with calls to the assert function to help identify where errors are occurring. If an assertion is false, the error message gives the precise line number of the failed assertion.
Additionally, there is a way to remove the assertions without really removing them. If you use the preprocessor directive #define NDEBUG before including the header file assert.h, like this:
#define NDEBUG
#include <assert.h>
.
.
.
then all calls to the assert function are ignored when you run the program. (NDEBUG stands for No debug, and a #define directive is a preprocessor feature that we don't discuss right now.) During program testing and debugging, programmers often like to turn off debugging statements yet leave them physically present in the source code in case they need the statements later. Inserting the line #defineNDEBUG turns off assertion checking without having to remove the assertions.
As useful as the assert function is, it has two limitations. First, the parameter to the function must be expressed as a C++ logical expression. We can turn the comment
// 0.0 <= deptSales <= 25000.0
into an executable assertion with the statement
assert(0.0 <= deptSales && deptSales <= 25000.0);
But there is no easy way to turn the comment
// For each department, the file contains a department ID,
// number of days, and one sales figure for each day

 
< previous page page_372 next page >