Home Segments Index Top Previous Next

389: Mainline

Alternatively, you can have the input expression place numbers directly in the cylinder member variables, rather than indirectly through radius and length variables:

#include 
const double pi = 3.14159;
// Define the cylinder class:
class cylinder {
  public: double radius, length;
          double volume ( ) {return pi * radius * radius * length;}
};
// Define cylinder array:
cylinder oil_tanks[100];
main ( ) {
  // Declare various variables:
  int limit, counter;
  double sum = 0.0;
  // Read numbers and write them into array:
  for (limit = 0;                       
       cin >> oil_tanks[limit].radius   
           >> oil_tanks[limit].length;  
       ++limit)                         
    ;                                   
  // Compute volumes: 
  for (counter = 0; counter < limit; ++counter) 
    sum = sum + oil_tanks[counter].volume ( ); 
  // Display sum: 
  cout << "The total volume in the " 
       << limit << " storage tanks is " 
       << sum << endl; 
} 
--- Data ---
4.7       40.0 
3.5       35.5 
3.5       45.0       
--- Result --- 
The total volume in the 3 storage tanks is 5873.91