help with some math issues
this program is meant to let the user choose which shape they want to calculate the surface area of. so if they select 1, it goes to a sphere, and they enter the radius, and then the program gives them back the surface area. and so on for the other 3 shapes. the problem i'm having is that the first time i try to do any of these shapes, i am getting ridiculous answers that are incorrect, but if i try a second time, without exiting the program, then they work correctly. for example, if i run the program and choose the sphere, and enter a radius of 1, the answer i get is 221589529710308. obviously incorrect, way too large. but if i just hit 1 again, and redo it, then i get 12.5664, which is the correct answer. any ideas as to what may be causing this?
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
int choice,
radius_sphere,
radius_cone,
length_cone,
length_rectprism,
width_rectprism,
height_rectprism,
length_pyramid,
height_pyramid;
double surface_area_sphere,
surface_area_cone,
surface_area_rectprism,
surface_area_pyramid;
const double PI=3.14159;
cout<<"\nSurface Area Calculator"
<<"\n1) Sphere"
<<"\n2) Cone"
<<"\n3) Rectangular Prism"
<<"\n4) Pyramid"
<<"\n\n5) Quit"
<<"\n\nEnter Your Choice: ";
cin>>choice;
while (choice>5)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
while (choice<5)
{
if (choice==1)
{
surface_area_sphere=4*PI*radius_sphere*radius_sphere;
cout<<"\nPlease enter the radius of the sphere: ";
cin>>radius_sphere;
cout<<"\nThe surface area of the sphere is "<<fixed<<setprecision (4)<<surface_area_sphere;
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)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==2)
{
surface_area_cone=(PI * radius_cone * length_cone) + (PI * radius_cone * radius_cone);
cout<<"\nPlease enter the radius of the cone: ";
cin>>radius_cone;
cout<< "\nplease enter the length of the cone: ";
cin>>length_cone;
cout<<"\nThe surface area of the cone is "<< fixed<<setprecision (4)<<surface_area_cone;
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)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==3)
{
surface_area_rectprism=(2*length_rectprism*width_rectprism)+(2*width_rectprism*height_rectprism)+(2*length_rectprism*height_rectprism);
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;
cout<<"\nThe surface area of the rectangular prism is: "<<fixed<<setprecision (4)<<surface_area_rectprism;
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)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==4)
{
surface_area_pyramid=(2*length_pyramid*height_pyramid)+(length_pyramid*length_pyramid);
cout<<"\nPlease enter the length of the pyramid: ";
cin>>length_pyramid;
cout<<"\nPlease enter the height of the pyramid: ";
cin>>height_pyramid;
cout<<"\nThe surface area of the pyramid is: "<<fixed<<setprecision (4)<<surface_area_pyramid;
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)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
}
return 0;
}