#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Little C++ homework


    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    using namespace std;
    
    class Sphere
    {
    	private:
    		double r, sArea;
    
    	public:
    		Sphere();
    		Sphere(double radius);
    		double surfArea();
    };
    
    Sphere::Sphere()
    {
    	r = 5;
    }
    
    Sphere::Sphere(double radius)
    {
    	r = radius;
    }
    
    double Sphere::surfArea()
    {
    	double const PI = 3.14159;
    	sArea = 4 * PI * r * r;
    	return sArea;
    }
    
    
    int main()
    {
    	double radius;
    	char continueValue ='y';
    	cout<<"Lab 10 - Surface area of a Sphere"<<endl;
    	
    	while(continueValue != 'n' && continueValue !='N')
    	{
    		cout<<endl;
    		cout<<"Please enter the radius: ";
    		cin>>radius;
    		while(radius < 0 || radius > 5000)
    		{
    			cout<<endl;
    			cout<<"Bad value! Please enter another 0 through 5000: ";
    			cin>>radius;
    		}
    
    		if(radius = 0)
    		{
    			Sphere mySphere;
    		}
    		else
    		{
    			Sphere mySphere(radius);
    			mySphere.surfArea();
    		}
    		cout<<endl;
    		cout<<"Would you like to enter another value(y/n):";
    		cin>>continueValue;
    	}
    	cout<<endl;
    	cout<<"Lab 10 has successfully terminated for Drew Faber";
    }
    Have a problem trying to display the object radius, not sure if I'm using the class functions properly.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    Code:
    		if(radius = 0)
    		{
    			Sphere mySphere;
    		}
    Well you want to say == 0 to begin with.

    Then you need to decide what to do when radius is 0.
    At the moment, your mySphere object has a very short life, and does nothing.

    > mySphere.surfArea();
    Perhaps print the result?

    cout << mySphere.surfArea();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Thanks Salem, 20/20

IMN logo majestic logo threadwatch logo seochat tools logo