Question
/*Below is a beginner C++ program assignment I wrote to calculate volume using functions. I cannot quite get it to run. Can you please help.
/*Below is a beginner C++ program assignment I wrote to calculate volume using functions. I cannot quite get it to run. Can you please help. I was having trouble with printing strings. Then I included
#include
#include
#include
#include
using namespace std;
const double PI = 3.14159;
// Prototyping all the functions
void enterValid(string promptString);
char printFigureMenu();
int areaMenu();
void areaSection();
void circleSection();
void rectangleSection();
void triangleSection();
double calculateArea(double radius);
double calculateArea(double length, double width);
double calculateArea(double side1, double side2, double side3);
void printArea(string shapeName, double area);
int volumeMenu();
void volumeSection();
void sphereSection();
void rectangularPrismSection();
void squarePyramidSection();
double calculateVolume(double radius);
double calculateVolume(double side1, double height);
double calculateVolume(double length, double width, double height);
void printVolume(string shapeName, double volume);
/*
main function calls the printFigureMenu function to get a choice from the user
until the user choses to exit.
*/
int main()
{
char choiceChar;
while (choiceChar != 'C' && choiceChar != 'c')
{
choiceChar = printFigureMenu();
switch (choiceChar)
{
case 'a':
case 'A':
areaSection();
break;
case 'b':
case 'B':
volumeSection();
break;
case 'c':
case 'C':
break;
default:
enterValid("choice");
break;
}
}
return 0;
}
char printFigureMenu()
{
char choiceChar;
cout << " Select the type of figure you want to work with ";
cout << "\tA. Area of Figures ";
cout << "\tB. Volume of Figures ";
cout << "\tC. Exit ";
cin >> choiceChar;
return choiceChar;
}
void enterValid(string promptString)
{
cout << "Enter valid " << promptString << endl;
}
int areaMenu()
{
int choiceInt;
cout << " Choose the shape to find area of ";
cout << "\t1. Circle ";
cout << "\t2. Rectangle ";
cout << "\t3. Triangle ";
cin >> choiceInt;
return choiceInt;
}
void areaSection()
{
int choiceInt = areaMenu();
switch (choiceInt)
{
case 1:
circleSection();
break;
case 2:
rectangleSection();
break;
case 3:
triangleSection();
break;
default:
enterValid("choice");
break;
}
}
void circleSection()
{
double radius;
cout << " Circle Area Section ";
cout << "Enter the radius: ";
cin >> radius;
while (radius <= 0) {
enterValid("radius");
cin >> radius;
}
double area = calculateArea(radius);
printArea("circle", area);
}
void rectangleSection()
{
double length, width;
cout << "Rectangle Area Section ";
cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin >> width;
double area = calculateArea(length, width);
printArea("rectangle", area);
}
void triangleSection()
{
double side1, side2, side3;
cout << "Enter the side1 length: ";
cin >> side1;
cout << "Enter the side2 length: ";
cin >> side2;
cout << "Enter the side3 length: ";
cin >> side3;
double area = calculateArea(side1, side2, side3);
printArea("traingle", area);
}
double calculateArea(double radius)
{
double area = PI * radius * radius;
return area;
}
double calculateArea(double length, double width)
{
double area = length * width;
return area;
}
double calculateArea(double side1, double side2, double side3)
{
double p = (side1 + side2 + side3) / 3;
double area = pow(p * (p - side1) * (p - side2) * (p - side3), 0.5);
return area;
}
void printArea(string shapeName, double area)
{
cout << "The area of this " << shapeName << " is " << area << endl;
}
int volumeMenu()
{
int choiceInt;
cout << "Choose the shape to find volume of ";
cout << "\t1. Sphere ";
cout << "\t2. Rectangular Prism ";
cout << "\t3. Square Pyramid ";
cin >> choiceInt;
return choiceInt;
}
void volumeSection()
{
int choiceInt = volumeMenu();
switch (choiceInt)
{
case 1:
sphereSection();
break;
case 2:
rectangularPrismSection();
break;
case 3:
squarePyramidSection();
break;
default:
enterValid("choice");
break;
}
}
void sphereSection()
{
double radius;
cout << "Sphere Volume Section ";
cout << "Enter the radius: ";
cin >> radius;
double volume = calculateVolume(radius);
printVolume("sphere", volume);
}
void rectangularPrismSection()
{
double length, width, height;
cout << "Rectangular Prism Volume Section ";
cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin >> width;
cout << "Enter the height: ";
cin >> height;
double volume = calculateVolume(length, width, height);
printVolume("rectangular prism", volume);
}
void squarePyramidSection()
{
double side1, height;
cout << "Square Pyramid Volume Section ";
cout << "Enter the side length: ";
cin >> side1;
cout << "Enter the height: ";
cin >> height;
double volume = calculateVolume(side1, height);
printVolume("square pyramid", volume);
}
double calculateVolume(double radius)
{
double volume = (4.0 / 3) * PI * pow(radius, 3);
return volume;
}
double calculateVolume(double length, double width, double height)
{
double volume = length * width * height;
return volume;
}
double calculateVolume(double side1, double height)
{
double volume = (1.0 / 3) * height * side1 * side1;
return volume;
}
void printVolume(string shapeName, double volume)
{
cout << "The volume of this " << shapeName << " is " << volume << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started