Sum and Average

I'm a beginner, how would I make it so that I can have someone input the length, width and height for all 3 boxes and then have it output the sum and average volume? Here's an example of what I would like:
INPUT - Enter Box 1 (Length, Width, Height): 10.1 11.2 3.3
INPUT – Enter Box 2 (Length, Width, Height): 5.5 6.6 7.7
INPUT – Enter Box 3 (Length, Width, Height): 4.0 5.0 8.0
OUTPUT – The sum of the volume is 812.806
OUTPUT – The average volume is 270.935

Here's my original code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  
#include <iostream>  
using namespace std;  
  
double box(double length, double width, double height); // use double data  
  
int main()  
{  
  double sum;  
  
  sum = box(10.1, 11.2, 3.3) + box(5.5, 6.6, 7.7) + box(4.0, 5.0, 8.0);  
  
  cout << "The sum of the volumes is " <<  sum << "\n";  
  cout << "The average volume is " << sum / 3.0 << "\n";  
  
  return 0;  
}  
  
// This version of box uses double data.  
double box(double length, double width, double height)  
{  
  return length * width * height ;  
}
The simplest way would be to use simple cin commands.

1
2
cout << "Enter Box 1 (Length Width Height): ";
cin >> length1 >> width1 > height1;


However, using cin would require you to press enter after each value and therefore it will use three lines to get the input. It would look at follows:

Enter Box 1 (Length Width Height): 10.1
11.2
3.3


If you want your input to be on one line like you showed at the top of your post, you'll have to make it much more complicated by taking the input as a string and then using string member functions to split the line up into three numbers.
Topic archived. No new replies allowed.