// PROGRAM: dcheung_box.cpp // PROGRAMMER: David Cheung // DATE: 09/19/06 // PURPOSE: Calculate the surface area and the volume of a box. // INPUT: Length, height, and width - from keyboard // OUTPUT: Surface area and volume of the box - to the screen // DESCRIPTION: /* Algorithm: Request the dimensions of the box from user Length (varLength) Width (varWidth) Height (varHeight) Multiply 3 dimensions for volume Length * Width * Height Calculate the area Multiply L * W and double the result Multiply W * H and double the result Multiply H * L and double the result Add the three results together Show calculated volume and area Assuming the user knows not to mix different units, and not to include the units when inputting the variables. */ #include #include using namespace std; int main() { // Declare variables float varLength, // User input length varWidth, // User input width varHeight, // User input height totalVolume, // Calculated total volume totalArea, // Calculated total area tmpArea1, // Variables tmpArea2, // to be used tmpArea3; // to calculate the area // Use a fixed number notation, and show the decimal point cout << fixed << showpoint; // Start the program cout << "This program computes the surface area and the volume of a box."; cout << endl << endl; // Request the dimensions of the box from the user // Hopefully the user will only use one system of measurement cout << "Please enter the length of the box: "; cin >> varLength; cout << " width of the box: "; cin >> varWidth; cout << " height of the box: "; cin >> varHeight; // Confirm the inputs // Places 3 decimal places behind the decimal point cout << endl << "Dimensions of the box" << endl; cout << setprecision(3) << " length: " << setw(6) << varLength << endl; cout << " width: " << setw(7) << varWidth << endl; cout << " height: " << setw(6) << varHeight << endl << endl; // Volume totalVolume = varLength * varWidth * varHeight; // Area tmpArea1 = 2 * (varLength * varWidth); tmpArea2 = 2 * (varWidth * varHeight); tmpArea3 = 2 * (varHeight * varLength); totalArea = tmpArea1 + tmpArea2 + tmpArea3; // Results cout << "This box has a surface area of " << setw(7) << totalArea << endl; cout << " and a volume of " << setw(7) << totalVolume << endl << endl; // End cout << "Goodbye!" << endl; return 0; }