need help finding where the problem is for this code

This is a discussion on need help finding where the problem is for this code within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am very new to programming and I tried to write this code in c++ to calculate mass, ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Question need help finding where the problem is for this code

    Hi everyone,
    I am very new to programming and I tried to write this code in c++ to calculate mass, volume, and area for a sphere and a solid. It compiles but I get nonsense answers everytime I run the program..I know there is a error in my logic somewhere but I just can't see it.

    Here is the code
    Code:
    #include<iostream>
    using namespace std;
    
    class TSolid{
    public:
      TSolid(){};
      virtual ~TSolid(){};
    protected:
      double mass, volume, density, area;
    
    public:
      double getArea(){return area;}
      double getVolume(){return volume;}
      double getMass(){return mass;}
    
      void setDensity(double d){density=d;}
      virtual void ComputeMass(){;};
      virtual void ComputeVolume(){;};
      virtual void ComputeArea(){;};
    };
    
    class TSphere:public TSolid{
    public:
      TSphere (){};
      virtual ~TSphere (){};
      double radius,d;
     
      void ComputeArea (){area= 4*3.14*radius*radius;}
      void ComputeVolume (){volume=1.33*3.14*radius*radius*radius;}
      void ComputeMass () {mass=d*3.14*1.33*radius*radius*radius;} 
    };
    
    class TCube:public TSolid{
    public:
      TCube(){};
      virtual ~TCube(){};
      double side,d;
      void ComputeArea (){area=6*side*side;}
      void ComputeVolume (){volume=side*side*side;}
      void ComputeMass (){mass=d*side*side*side;}
    };
    
    int main ()
    {
      double side,d,radius;
      d=30;
      radius=15;
      side=12;
      TCube box;
      TSphere ball;
      cout<<"d="<<d<<endl;
      cout<<"radius="<<radius<<endl;
      cout<<"side="<<side<<endl;
      cout << "sphere info:"<<endl;
      cout << "area = " << ball.getArea()<<endl;
      cout <<"area=" <<4*3.14*radius*radius<<endl;
      ball.ComputeVolume();
      cout << "volume =" << ball.getVolume()<<endl;
      ball.ComputeMass();
      cout << "mass = " <<ball.getMass()<<endl;
      cout<< "cube info: "<<endl;
      box.ComputeArea();
      cout<< "area = " << box.getArea()<<endl;
      box.ComputeVolume();
      cout << "volume = " << box.getVolume()<<endl;
      box.ComputeMass();
      cout << "mass = " << box.getMass()<<endl;
      cout<<"side="<<side<<endl;
      return 0;
    }
    thanks for the help

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    side and radius seem to be unitialized in the classes.

    It doesn't help to use variables of the same name in main(). That doesn't magically set the data members of an object. That's what constructors are for.

    From a design point of view: why are the compute() and get() functions separate? What if I forget to call the compute() function first?

    Either a single function should calculate and return the result directly and the results are not stored in the class itself, or the compute() functions are private (or protected) and called automatically only by the class itself (in the constructor) before the user of the class gets a chance to call the get methods.
    Last edited by anon; 06-18-2007 at 09:37 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    It seems that you meant to pass 'radius' and 'side' as an argument to ComputerArea/Volume/Mass. That probably has something to do with it.

    this bit:
    Code:
    void ComputeArea (){area= 4*3.14*radius*radius;}
    might want to be this like so:
    Code:
    void ComputeArea (double radius){area= 4*3.14*radius*radius;}
    etc...

    (anon beat me to it *shakes fist*)
    Last edited by simpleid; 06-18-2007 at 09:58 AM.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    yep, that was definitely the issue. i finally get the right answers! this is exciting, thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

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