/* Test case 1: Input: 3, 1, 2 (train example) Expected Ouput: 8.283 Actual: Test case 2: Input: 2, 1, 2, 1 (ice cream cone) Expected Output: 2.5708 Actual Output: 2.5708 Test case 3: Input: 1, 1, 1, 2 (house) Expected: 2 Actual: */ #include <iostream> #include <string> using namespace std; const double PI = 3.14159265359; const double MAX_DIMENSION = 100000; // These must be declared before the function that calls them (e.g., main), but can be defined below void print_menu(); double get_user_input(double min, double max); double calculate_circle_area(double radius); double calculate_square_area(double side_length); double calculate_triangle_area(double width, double height); double calculate_house_area(double wall_height, double roof_height, double roof_width); double calculate_ice_cream_area(double cone_height, double cone_width, double ice_cream_height); double calculate_train_area(double car_height, double wheel_height); int main() { double area = 0.0; // prompt user for choice print_menu(); int choice = get_user_input(1, 3); // if choice is 1 if (choice == 1) { // prompt for house and roof dimensions double wall_height, roof_height, roof_width; cout << "You chose house. How tall's the wall? "; wall_height = get_user_input(0,MAX_DIMENSION); cout << "How tall's the roof? "; roof_height = get_user_input(0, MAX_DIMENSION); cout << "How wide's the roof? "; roof_width = get_user_input(0, MAX_DIMENSION); area += calculate_house_area(wall_height, roof_height, roof_width); } // if choice is 2 else if (choice == 2) { // prompt for ice cream cone dimensions double cone_height, cone_width, ice_cream_height; cout << "You chose ice cream cone. How tall's the cone? "; cone_height = get_user_input(0, MAX_DIMENSION); cout << "How wide's the cone? "; cone_width = get_user_input(0, MAX_DIMENSION); cout << "How tall's the ice cream? "; ice_cream_height = get_user_input(0, MAX_DIMENSION); area += calculate_ice_cream_area(cone_height,cone_width, ice_cream_height); } // if choice is 3 else if (choice == 3) { // prompt for train dimensions double car_height, wheel_height; cout << "You chose the train. How tall's the car? "; car_height = get_user_input(0, MAX_DIMENSION); cout << "How tall's the wheel? "; wheel_height = get_user_input(0, MAX_DIMENSION); area += calculate_train_area(car_height, wheel_height); } // Report total area cout << "Total area of wood needed is " << area << " square inches" << endl; system("pause"); return 0; } /* Just print the menu (doesn't read input) */ void print_menu() { cout << "Choose from the following\n" << "1. A 2d House\n" << "2. A 2d ice cream cone\n" << "3. A 2d train car\n" << "Choice: "; return; } /* Reads for integer input and reprompts until valid floating-point input is provided within range @param min minimum value for range of input @param max maximum value for range of input @return first valid input floating-point number */ double get_user_input(double min, double max) { int input; // read in from cin cin >> input; // while it's bad (non-int or not in range) while (cin.fail() || input < min || input > max) { // send message, reprompt cout << "That's bad input. Gimme a value between " << min << " and " << max << ":"; if (cin.fail()) { cin.clear(); string dummy; cin >> dummy; } // read again from cin cin >> input; } return input; } /* Calculate the area of a circle given the radius @param radius the radius of the circle @return the area of the circle given the radius */ double calculate_circle_area(double radius) { double area = 0.0; area = PI * pow(radius, 2); return area; } /* Calculate the area of a square given the side length @param side_length the length of the side of the square @return the area of the square */ double calculate_square_area(double side_length) { double area = 0.0; area = pow(side_length, 2); return area; } /* Calculate the area of a triangle given the width and height @param width length of the unequal edge of an isoseles triangle @param height length of the line perpendicular to the unequal edge of the isosoles triangle extending to the opposite corner @return the area of the triangle */ double calculate_triangle_area(double width, double height) { double area = 0.0; area = width * height / 2; return area; } /* Calculate the area of a house given the dimensions @param wall_height the height of a wall (the house is square) @param roof_height the height of the roof (see height of triangle in calculate_triangle_area function) @param roof_width the width of the roof (see width of triangle in calculate_triangle_area function) @return the area of the house as 2d object */ double calculate_house_area(double wall_height, double roof_height, double roof_width) { double area = 0.0; area += calculate_square_area(wall_height); area += calculate_triangle_area(roof_width, roof_height); return area; } /* Calculate the area of an ice cream cone given dimensions @param cone_height the height of the ice cream above the cone (i.e., radius of a half circle) @param cone_width the width of the cone (see width of triangle in calculate_triangle_area function) @param ice_cream_height the height of the cone (see width of triangle in calculate_triangle_area function) @return the area of the ice cream as 2d object */ double calculate_ice_cream_area(double cone_height, double cone_width, double ice_cream_height) { double area = 0.0; area += calculate_triangle_area(cone_width, cone_height); area += calculate_circle_area(ice_cream_height) / 2.0; return area; } /* Calculate the area of a train given dimensions @param car_height the height of the train car (which is also 2x the length) @param wheel_height the diameter of the train car wheel @return the area of the train as 2d object */ double calculate_train_area(double car_height, double wheel_height) { double area = 0.0; area += 2 * calculate_square_area(car_height); area += 2 * calculate_circle_area(wheel_height / 2.0); return area; }