Image

Imagenallix wrote in Imagecpp

I'm taking a introductory C++ programming class this semester and I'm looking for some critique on my code for my assignments.


-----------------------------------------------
Phillip de Holes is the chief engineer for street repairs for New Orleans. He just hired you to help him in his cost determination of street repairs. He wants you to compose a program to calculate the amount of asphalt needed to repair potholes.

Input for your program consists of three, four or five numeric values, separated by a space, all on one line. The first number is an integer indicating the general shape of the pothole. The other values are floating-point values that serve as the dimensions of the hole. Note that the number of dimensions needed varies with the shape of the hole. All dimensions are measured in inches. The cost of asphalt is $0.1234 per cubic inch. The different types of holes include:

1 Circle
the second value is the diameter, the third is the depth of the hole

2 Rectangle
the second, third, and fourth values are the length, width, and depth of the hole

3 Trapezoid
the second and third values are the lengths of the two parallel bases, the fourth is the distance between the two parallel bases (i.e., the height of the trapezoid), and the fifth is the depth of the hole

The program produces three lines of output in the general form:

type pothole: # # # #
Volume: #.#
Cost: $ #.#

where type is the word Circle, Rectangle, or Trapezoid as appropriate; # is one of the dimension of the pothole (e.g., diameter, length, width, depth); #.# is the volume of the pothole in cubic inched; and $#.# is the cost of fill in the pothole.
-----------------------------------------------

And here's the code I came up with:


-------------------------------------------------
/*Homework 3
*Author: Dennis Groome
*
*Purpose: Calculate volume of three different shapes by using conditionals.
*
*Input consists of one integer followed by two to four floating-point variables
*on a single line.
*
*Output consists of the type of pot hole, it's dimensions, it's volume, and the
*cost of the asphalt required to fill it in the form:
*
* type pothole: # # # #
* Volume: #.#
* Cost: $ #.#
* where Type is the shape of the pothole, # represents a floating point value,
* #.# is the calculated volume and $#.# is the cost.
*/


#include

int main()
{ // program Potholes

int intType; // Shape of pothole
double dblRadius; // Radius of circular pothole in inches
double dblDiameter; // Diameter of circular pothole in inches
double dblLengthOne; // Length of a rectangular pothole or one side of
// trapezoidal pothole
double dblLengthTwo; // Length of second side of trapezoidal pothole
double dblWidth; // Width of a pothole
double dblDepth; // Depth of pothole in inches
double dblVolume; // Volume of pothole in cubic inches
double dblCost; // Cost of filling the pothole

// get the shape of the pothole
cin >> intType; // gets the type of pothole

// select type and get appropriate dimensions
if (intType == 1) { // Pothole is Circle type

// gets diameter and depth
cin >> dblDiameter >> dblDepth;

// calculates radius, volume, and cost of asphalt
dblRadius = dblDiameter / 2;
dblVolume = dblRadius * dblRadius * dblDepth * 3.1415927;
dblCost = dblVolume * 0.1234;

// Outputs dimensions, volume, and cost
cout << "Circular Pothole: " << dblDiameter << " inches in diameter and ";
cout << dblDepth << " inches deep." << endl;
cout << endl << "Volume: " << dblVolume << " cubic inches.";
cout << endl << "Cost: $" << dblCost << endl;

} // Pothole is Circle type

if (intType == 2) { // Pothole is Rectangle Type

cin >> dblLengthOne >> dblWidth >> dblDepth; // gets the dimensions of pothole

// Calculates volume and cost of asphalt
dblVolume = dblLengthOne * dblWidth * dblDepth;
dblCost = dblVolume * 0.1234;

// outputs dimensions, volume and cost
cout << "Rectangular Type: " << dblLengthOne << " inches by " << dblWidth;
cout << " inches wide and " << dblDepth << " inches deep" << endl;
cout << endl << "Volume: " << dblVolume << " cubic inches";
cout << endl << "Cost: $" << dblCost << endl;

} // Pothole is Rectangle Type

if (intType == 3) { // Pothole is Trapezoidal Type

// gets dimensions of pothole
cin >> dblLengthOne >> dblLengthTwo >> dblWidth >> dblDepth;

// calculates volume and cost
dblVolume = ((dblLengthOne + dblLengthTwo)/2) * dblWidth * dblDepth;
dblCost = dblVolume * 0.1234;

// Output dimensions, volume, and cost
cout << "Trapezoidal Pothole: " << dblLengthOne << " inches by ";
cout << dblLengthTwo << " inches by " << dblWidth << " inches by ";
cout << dblDepth << " inches deep" << endl;
cout << endl << "Volume: " << dblVolume << " cubic inches";
cout << endl << "Cost: $" << dblCost << endl;

} // Pothole is Trapezoidal Type

return 0;
}
------------------------------------------------

Any suggestions or comments on the code? The homework assignment required the use of if-then conditionals, and we haven't covered much since we're only 4 weeks into the class, so the commands are still rudimentary.