Home Segments Index Top Previous Next

479: Mainline

Once you know how to test the bits in the status variable, you can incorporate such tests in your programs:

#include   
/* Define the trade apparatus */ 
struct trade {double price; int number;}; 
struct trade *trade_pointers[100]; 
enum {bad_size_bit = 1, bad_price_bit  = 2, 
      too_much_bit = 4, too_little_bit = 8}; 
main ( ) { 
  /* Declare various variables */ 
  int limit, counter, number; 
  double price, sum = 0.0; 
  /* Declare status byte */ 
  unsigned char status = 0; 
  /* Read trade information */ 
  for (limit = 0; 
       2 == scanf ("%lf%i", &price, &number) && limit < 100; 
       ++limit) 
    if (price > 0.0 && number > 0) { 
      trade_pointers[limit]  
        = (struct trade*) malloc (sizeof (struct trade)); 
      trade_pointers[limit] -> price = price; 
      trade_pointers[limit] -> number = number; 
    } 
    else { 
      if (price <= 0.0) {status = status | bad_price_bit;} 
      if (number <= 0) {status = status | bad_size_bit;} 
    } 
  if (limit < 10) status = status | too_little_bit; 
  else if (limit == 100) status = status | too_much_bit; 
  /* Check status variable */ 
  if (status & (bad_price_bit | bad_size_bit)) 
    printf ("Bad data were encountered and ignored.\n", status); 
  if (status & (too_little_bit | too_much_bit)) 
    printf ("Wrong number of data were encountered.\n", status); 
  // Perform other computations 
} 
--- Data ---
 3.3  300 
 2.5  400 
 9.9  100 
10.1  -100 
-5.0  200 
--- Result --- 
Bad data were encountered and ignored. 
Wrong number of data were encountered.