Home Segments Index Top Previous Next

266: Mainline

With trade_price redefined to operate on trade objects—instead of on a price and number combination—you can rewrite the program in Segment 263 as follows:

#include  
struct trade {double price; int number;}; 
double trade_price (struct trade t) {return t.price * t.number;} 
main ( ) { 
  double price; 
  int number; 
  struct trade t; 
  while (2 == scanf ("%lf%i", &price, &number)) { 
    t.price = price; 
    t.number = number; 
    printf ("The total value of the trade is %f.\n", 
            trade_price (t)); 
  } 
} 
--- Data ---
2.5 500 
--- Result --- 
The total value of the trade is 1250.000000.