coding question

How would I make a program that asks the user to enter 3 dimensions of a box then it displays:
volume,surface area of each unique face, total surface area

Attempt:


#include <iostream>
using namespace std;
int main()
{
double, L,W,H,volume,total_surface_area,partial_surface_areas
cout << "Give me the dimensions of a box. \n";
cin >> L;
cin >> W;
cin >> H;
volume = L*W*H;
total surface area = (L*W)+(L*W)+(L*H)+(L*H)+(W*H)+(W*H);
cout << "Volume =" << volume << "Total surface area =" << total_surface_area << endl;
return 0;

I havnt even programmed the partial surface areas and what I have is pretty long and messy. How would I make a simple program that asks for the dimensions of a box and then it displays the volume, total surface area, and all the partial surface areas at the same time?
Last edited on
Hi there,

You seem to be on the right track here, some corrections to your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
double, L,W,H,volume,total_surface_area,partial_surface_areas;
cout << "Give me the dimensions of a box. \n";
cout << "Length: "; cin >> L;
cout << "Width: "; cin >> W;
cout << "Height: "; cin >> H;
volume = L*W*H;
total_surface_area = (L*W)+(L*W)+(L*H)+(L*H)+(W*H)+(W*H);
cout << "Volume =" << volume << "\nTotal surface area =" << total_surface_area << endl;
return 0;
}


You can continue along the same way, or you can put the calculations into different functions, which will make your code look a bit tidier.

Apart from that this seems OK to me, do you have any other questions?

All the best,
NwN
Thanks for the help. It is much appreciated. Could you show me the other functions that would make this code a little less long winded please?
Most welcome.

An example would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
double L,W,H,volume,total_surface_area,partial_surface_areas;
cout << "Give me the dimensions of a box. \n";
cout << "Length: "; cin >> L;
cout << "Width: "; cin >> W;
cout << "Height: "; cin >> H;
volume = L*W*H;
total_surface_area = calculate_total_surface(L, W, H);
cout << "Volume =" << volume << "\nTotal surface area =" << total_surface_area << endl;
return 0;
}

double calculate_total_surface(double L, double W, double H)
{
    return (L*W)+(L*W)+(L*H)+(L*H)+(W*H)+(W*H);
}


This takes the actual calculating logic out of the main() function and puts your code into smaller, better understandable blocks.

There is an excellent section on functions in this site's tutorial if you would like to know more.

All the best,
NwN
Topic archived. No new replies allowed.