help with some math issues

This is a discussion on help with some math issues within the C++ Programming forums, part of the General Programming Boards category; this program is meant to let the user choose which shape they want to calculate the surface area of. so ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    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;
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    that makes perfect sense, and trying it out, it works! thank you so much.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by joeman View Post
    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
    Last edited by _Mike; 02-13-2010 at 10:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing aweful in math, but decent in CS?
    By psychopath in forum General Discussions
    Replies: 2
    Last Post: 11-04-2009, 09:49 AM
  2. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  3. Math Issues
    By Berserker in forum C Programming
    Replies: 9
    Last Post: 01-11-2005, 01:54 PM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 07:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21