# Thread: help with some math issues

1. ## 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,
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"
cin>>choice;
while (choice>5)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
while (choice<5)
{
if (choice==1)
{

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"
cin>>choice;
while (choice>5)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}
if (choice==2)
{

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"
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"
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"
cin>>choice;
while (choice>5)
{
cout<<"\nInvalid number, please try again: ";
cin>>choice;
}
}

}

return 0;
}```

2. Code:
`surface_area_pyramid=(2*length_pyramid*height_pyramid)+(length_pyramid*length_pyramid);`
(edited for clarity)

I think you might be looking at this too much like mathematics. Basically you're writing a series of instructions that will be executed as a procedure. So when you write a statement like the above, the current values of length_pyramid, height_pyramid, etc... are used to calculate the result, and that result is assigned to surface_area_pyramid. If any of those 'input' values change in the future - C doesn't automatically update surface_area_pyramid (although some language have a feature called 'binding' that does stuff like this for you). It will keep the same value until you explicitly assign a new value to it. So what you should do is get the correct values for length, height, etc. from the user, and THEN make the assignment. Otherwise the equation will be executed with whatever random values happened to be in memory at the location the length_pyramid refers to. I hope the makes sense - does it?

3. that makes perfect sense, and trying it out, it works! thank you so much.

4. Originally Posted by joeman
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?
You are calculating the surface area using uninitialized variables before the user has inputted the values for them.

Edit:
Guess I'm to tired to read properly.. Seems like sean already said this