Skip to content

Commit abfac31

Browse files
committed
added files
1 parent 6b95628 commit abfac31

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

trapezoidalPrismVolume

77.2 KB
Binary file not shown.

trapezoidalPrismVolume.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) 2025 Luke Di Bert All rights reserved.
2+
//
3+
// Created by: Luke Di Bert
4+
// Date: May 21, 2025
5+
6+
// adds iostream library
7+
#include <iostream>
8+
9+
// adds string library
10+
#include <array>
11+
12+
// adds rounding
13+
#include <iomanip>
14+
15+
// adds strings
16+
#include <string>
17+
18+
// function to calculate area
19+
float areaCalc(std::array<float, 4> array) {
20+
float area = 0.5 * (array[0] + array[1]) * array[2];
21+
return area;
22+
}
23+
24+
// main function calculating volume
25+
int main() {
26+
while (true) {
27+
// variables to store data for transfer
28+
std::string base1;
29+
std::string base2;
30+
std::string height;
31+
std::string length;
32+
33+
std::cout <<
34+
"Welcome to Luke's volume of a trapezoidal prism calculator!"
35+
<< std::endl;
36+
37+
// array for shape dimensions
38+
std::array<float, 4> dimension;
39+
40+
// user input for dimensions
41+
std::cout << "Enter base 1 of the trapezoidal prism (cm): ";
42+
std::cin >> base1;
43+
std::cout << "Enter base 2 of the trapezoidal prism (cm): ";
44+
std::cin >> base2;
45+
std::cout << "Enter height of the trapezoidal prism (cm): ";
46+
std::cin >> height;
47+
std::cout << "Enter length of the trapezoidal prism (cm): ";
48+
std::cin >> length;
49+
50+
try {
51+
// transfers data from variables into array
52+
dimension[0] = std::stof(base1);
53+
dimension[1] = std::stof(base2);
54+
dimension[2] = std::stof(height);
55+
dimension[3] = std::stof(length);
56+
57+
// compound boolean expression if statement
58+
if (
59+
dimension[0] <= 0 ||
60+
dimension[1] <= 0 ||
61+
dimension[2] <= 0 ||
62+
dimension[3] <= 0) {
63+
std::cout << "Dimensions cannot be zero!" << std::endl;
64+
break;
65+
}
66+
67+
// areaCalc call with dimension array in parameter
68+
float baseArea = areaCalc(dimension);
69+
70+
// volume calculation using returned value
71+
float volume = baseArea * dimension[3];
72+
73+
// volume displayed rounded to two decimal places
74+
std::cout << std::fixed << std::setprecision(2);
75+
std::cout << "The volume is: " << (volume) << "cm^3" << std::endl;
76+
} catch (const std::invalid_argument&) {
77+
std::cout <<
78+
"Oops you entered invalid input, use numbers only!"
79+
<< std::endl;
80+
break;
81+
}
82+
// asks the user if they want to quit
83+
std::string userQuit;
84+
std::cout << "Would you like to play again? yes(0), no(1): ";
85+
std::cin >> userQuit;
86+
try {
87+
// converts user input into usable value
88+
int userQuitInt = std::stoi(userQuit);
89+
90+
// quits program if user put 1
91+
if (userQuitInt == 1) {
92+
std::cout << "Have a good day" << std::endl;
93+
break;
94+
}
95+
} catch (const std::invalid_argument&) {
96+
std::cout << userQuit <<
97+
" is invalid input! please use an integer"
98+
<< std::endl;
99+
break;
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)