// IfFilterGuard.C // (c) Burt Rosenberg 1997 // This program illustrates use of the if/else statement // as a FILTER to coerce the world into a comfortable // state, defined as one in which the ASSERTION is satisfied, // and as a GUARD to protect code from being used under // conditions adverse to the code's health. #include void main() { // Create two integers int numerator ; int denominator ; // input values cout << "Enter numerator: " ; cin >> numerator ; cout << "Enter denominator: " ; cin >> denominator ; // FILTER the input, boy we wish those // values were positive ... if ( numerator<0 ) { numerator = -numerator ; } if ( denominator<0 ) { denominator = -denominator ; } // ASSERT, numerator >= 0 AND denominator >=0 // GUARD against division by zero if ( denominator != 0 ) { // ASSERT, denominator > 0 cout << "numerator/denominator = " << (numerator/denominator) ; } else { cout << "Illegal division by zero." ; } cout << "\n" ; // I need a newline. cout << "Good bye." << endl ; }