i need to rewrite this program using functions, but i have no idea how. i've read up on it and the more i read the more confused i am. could someone give an example or show me how to use a function, say for the sphere, or any of the shapes? or give me some tips or advice. it'd be much appreciated, because i'm completely in over my head.
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
int choice, //declaring integer variables to be used
radius_sphere,
radius_cone,
length_cone,
length_rectprism,
width_rectprism,
height_rectprism,
length_pyramid,
slant_height_pyramid;
double surface_area_sphere, //declaring double variables to be used
surface_area_cone,
surface_area_rectprism,
surface_area_pyramid;
const double PI=3.14159; //setting PI to be a constant double with a value of 3.14159
cout<<"\nSurface Area Calculator" //displaying the initial menu to the user
<<"\n1) Sphere"
<<"\n2) Cone"
<<"\n3) Rectangular Prism"
<<"\n4) Pyramid"
<<"\n\n5) Quit"
<<"\n\nEnter Your Choice: "; //prompting user to select which option from the menu they would like
cin>>choice; //accepting user's choice from the menu
//setting while loop so that if a number larger than 5 or less than 1 is entered for choice, the user will receive an error message and be re-prompted to enter a new choice until he enters a valid one
while (choice>5||choice<1)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
//setting while loop so that the body will continue to run as long as choice is less than 5
while (choice<5)
{
if (choice==1) //setting if statement so that user enters 1 for choice, they will be able to calculate the surface area of a sphere
{
cout<<"\nPlease enter the radius of the sphere: ";
cin>>radius_sphere;
surface_area_sphere=4*PI*radius_sphere*radius_sphere; //setting equation to calculate the surface area of the sphere based on the user's input
cout<<"\nThe surface area of a sphere with radius "<<radius_sphere<<" is "<<fixed<<setprecision (4)<<surface_area_sphere; //displaying the calculated surface area of the sphere with 4 places after the decimal point
cout<<"\n\nSurface Area Calculator" //displaying the menu again for the user to choose another option
<<"\n1) Sphere"
<<"\n2) Cone"
<<"\n3) Rectangular Prism"
<<"\n4) Pyramid"
<<"\n\n5) Quit"
<<"\n\nEnter Your Choice: ";
cin>>choice;
while (choice>5||choice<1) //setting while loop so if choice is less than 1 or more than 5, the user will receive an error message and be prompted to try again
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==2) //setting if statement so that if the user enters 2 for choice, they will be able to calculate the surface area of a cone based on their inputs
{
cout<<"\nPlease enter the radius of the cone: ";
cin>>radius_cone;
cout<< "\nplease enter the length of the cone: ";
cin>>length_cone;
surface_area_cone=(PI * radius_cone * length_cone) + (PI * radius_cone * radius_cone); //setting equation to calculate the surface area of the cone based on the user inputs
cout<<"\nThe surface area of a cone with radius "<<radius_cone
<<" and length "<<length_cone<<" is "<< fixed<<setprecision (4)<<surface_area_cone; //displaying the surface area of the cone to 4 decimal places
cout<<"\n\nSurface Area Calculator"
<<"\n1) Sphere"
<<"\n2) Cone"
<<"\n3) Rectangular Prism"
<<"\n4) Pyramid"
<<"\n\n5) Quit"
<<"\n\nEnter Your Choice: ";
cin>>choice;
while (choice>5||choice<1) //setting while loop to display error message if user enters an invalid number, and prompting them to enter a new one.
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==3) //setting if statement so that if user enters 3 for choice, they will be able to calculate the area of a rectangular prism
{
cout<<"\nPlease enter the length of the prism: ";
cin>>length_rectprism;
cout<<"\nPlease enter the width of the prism: ";
cin>>width_rectprism;
cout<<"\nPlease enter the height of the prism: ";
cin>>height_rectprism;
surface_area_rectprism=(2*length_rectprism*width_rectprism)+(2*width_rectprism*height_rectprism)+(2*length_rectprism*height_rectprism); //setting the formula for the surface area of a rectangular prism
cout<<"\nThe surface area of a rectangular prism with length "<<length_rectprism<<" and width "<<width_rectprism<<"\nand height "<<height_rectprism<<" is: "<<fixed<<setprecision (4)<<surface_area_rectprism; //displaying the surface area based on user's inputs, and re-displaying the menu
cout<<"\n\nSurface Area Calculator"
<<"\n1) Sphere"
<<"\n2) Cone"
<<"\n3) Rectangular Prism"
<<"\n4) Pyramid"
<<"\n\n5) Quit"
<<"\n\nEnter Your Choice: ";
cin>>choice;
while (choice>5||choice<1) //setting while loop to display error message if user enters invalid input and prompting them to try again.
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==4) //setting if statement so that if user enters 4 for choice, they will be able to calculate the area of a pyramid
{
cout<<"\nPlease enter the length of the pyramid: ";
cin>>length_pyramid;
cout<<"\nPlease enter the slant height of the pyramid: ";
cin>>slant_height_pyramid;
surface_area_pyramid=(2*length_pyramid*slant_height_pyramid)+(length_pyramid*length_pyramid); //setting equation to calculate the surface area of a pyramid based on their inputs
cout<<"\nThe surface area a pyramid with length "<<length_pyramid<<" and height "<< slant_height_pyramid<<" is: "<<fixed<<setprecision (4)<<surface_area_pyramid; //displaying the surface area based on user's inputs and re-displaying menu
cout<<"\n\nSurface Area Calculator"
<<"\n1) Sphere"
<<"\n2) Cone"
<<"\n3) Rectangular Prism"
<<"\n4) Pyramid"
<<"\n\n5) Quit"
<<"\n\nEnter Your Choice: ";
cin>>choice;
while (choice>5||choice<1)//setting while loop to display error message if user enters and invalid number and prompting them to try again
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
}//closing while loop, loop will end when user enters 5 for choice, and the program will terminate.
return 0;
}