Result not as expected-- classes, getters, setters and switch.

Hi,
I'm learning C++. I am trying to write a program to calculate the total surface area and volume of cuboids, spheres, cones and cylinders. The program is giving unexpected outputs mostly random numbers with exponents of minus a few hundreds. What am I doing wrong here? P.S: I will only be able to follow up 12 hours from posting. Don't expect a reply
before 7:30 GMT).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cmath>
using namespace std;
class calcformulae
{
    private:

        //vars--------------------------------------------------------------------

        double vc,vs,vco,vcy,sac,sas,saco,sacy,l,b,h,ll,bb,hh,r,rr,rrr,rd,hhh,hd,rco,rcc,hco,hcc;

        //eqns--------------------------------------------------------------------

        void cuboidv(){vc=l*b*h;}
        void cuboidsa(){sac=2*hh*(ll+bb);}

        void spherev(){vs=(4/3)*(22/7)*r*r*r;}
        void spheresa(){sas=4*(22/7)*rr*rr;}

        void cylinderv(){vcy=(22/7)*rrr*rrr*hhh;}
        void cylindersa(){sacy=2*(22/7)*rd*(rd+hd);}

        void conev(){vco=(1/3)*(22/7)*rco*rco*hco;}
        void conesa(){saco=(22/7)*rcc*(rcc+sqrt(rcc*rcc+hcc*hcc));}

    public:

        //setters-------------------------------------------------------------------------

        void setCuboidv(double x,double y,double z){l=x;b=y;h=z;}
        void setCuboidsa(double xx,double yy,double zz){ll=xx;bb=yy;hh=zz;}

        void setSpherev(double w){r=w;}
        void setSpheresa(double ww){rr=ww;}

        void setCylinderv(double www,double zzz){rrr=www;hhh=zzz;}
        void setCylindersa(double wd,double zd){rd=wd;hd=zd;}

        void setConev(double wco,double zco){rco=wco;hco=zco;}
        void setConesa(double wcc,double zcc){rcc=wcc;hcc=zcc;}

        //getters--------------------------------------------------------------------------

        double getCuboidv(){return vc;}
        double getCuboidsa(){return sac;}
        double getSpherev(){return vs;}
        double getSpheresa(){return sas;}
        double getCylinderv(){return vcy;}
        double getCylindersa(){return sacy;}
        double getConev(){return vco;}
        double getConesa(){return saco;}
};

//implementation code----------------------------------------------------------------------

int main()
{
    calcformulae cf;
    cout<<"\nVOLUME, SURFACE AREA CALCULATOR";
    cout<<"\n-------------------------------\n\n";
    cout<<"Calculate:\n\n1)Volume \nOR\n2)Surface area?\n\n";

    int selector,shapeselector;
    double i,o,p;
    cin>>selector;

    switch(selector)
    {   case 1: case 2:
        cout<<"\nOf:\n\n1)Cuboid\n2)Sphere\n3)Cylinder\nOR\n4)Cone?\n\n";
        cin>>shapeselector;

        switch(shapeselector)
        {
            case 1:

            cout<<"\nEnter length:\n\n";
            cin>>i;

            cout<<"\nEnter width:\n\n";
            cin>>o;

            cout<<"\nEnter height:\n\n";
            cin>>p;

            switch(selector)
            {
                case 1: cf.setCuboidv(i,o,p);  cout<<endl<<cf.getCuboidv();  break;
                case 2: cf.getCuboidsa(); cout<<endl<<cf.getCuboidsa(); break;
            }
            break;

            case 2:

            cout<<"\nEnter radius:\n\n";
            cin>>i;

            switch(selector)
            {
                case 1: cf.setSpherev(i); cout<<endl<<cf.getSpherev(); break;
                case 2: cf.setSpheresa(i); cout<<endl<<cf.getSpheresa(); break;
            }
            break;

            case 3: case 4:

            cout<<"\nEnter radius:\n\n:";
            cin>>i;

            cout<<"\nEnter height:\n\n:";
            cin>>o;

            switch(shapeselector)
            {
            case 3:

                switch(selector)
                {
                    case 1: cf.setCylinderv(i,o); cout<<endl<<cf.getCylinderv(); break;
                    case 2: cf.setCylindersa(i,o); cout<<endl<<cf.getCylindersa(); break;
                }
            break;

            case 4:

                switch(selector)
                {
                    case 1: cf.setConev(i,o); cout<<endl<<cf.getConev(); break;
                    case 2: cf.setConesa(i,o); cout<<endl<<cf.getConesa(); break;
                }
            break;
            }
            default: cout<<"\nERROR\n\n\n";
            main();
        }

        default: cout<<"\nERROR\n\n\n";main();
    }


return 0;
}



Last edited on
You never call cuboidv(), cuboidsa(), etc. functions, so vc, sac, etc. values never initialized and contain garbage.
Whoa, whoa WHOA! Easy there buddy!


Have you heard of a phrase called "Containment" or Encapsulation?

What it means is that an object has certain properties that only that object has which other objects don't need to see.

For example a Cuboid doesn't have a Radius but they are both a 3DShape. Does that make sense?

Your class contains several "Properties" or Traits which should be encapsulated into it's own objects.

For example

1
2
3
4
5
6
7
8
class Cuboid
{
private:
       double Length, Width, Height;
public:
        Cuboid(double l, double w, double h): Width(w), Length(l), Height(h) {} // Sets the variables
        double GetSurfaceArea() { return (Width*Length)*6; } // Area of a square multiplied by the amount of faces
}


All the data for a cuboid is contained within the Cuboid class. Keeping your objects simple and well defined, also allowing for better maintenance when wanting to add other features.

You have a lot of switch() statements in your code, the way you have it mapped out too is rather hard to follow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
    while(1) // Keep it running until close is selected
    {
        std::cout << "Choose which object: /* List objects */ \t Or press 0 to quit: ";
        int choice;
        std::cin >> choice;
        switch(choice)
        {
        case 0: return 0; // Exit out of main
        case 1: DoCuboidStuff(); break;
        /* Do other cases */
        default: std::cout << "No such choice!\n";
        }
    }
}

void DoCuboidStuff()
{
       std::cout << "What are the dimensions(Length, Width, Height)?\n";
       double l,w,h;
       std::cin >> l >> w >> h;
       Cuboid ACuboid(l,w,h); // create a cuboid shape
       std::cout << "What calculation would you like? 1) Surface Area\t2) Volume : ";
       int choice; std::cin >> choice;
      switch(choice)
      {
      case 1: std::cout << ACuboid.GetSurfaceArea() << "\n"; break;
      /* Other calculations */
      }
}


As you can see the contained variables and functions have improved the readability of the program a lot, allowing you to see problems more easily within your own code, and allowing other programmers to spot bugs. :-)
i have corrected for 1)volume 1)Cuboid. two more break were needed in switch-case. line 88 need argument. line 14 was not called - merge it with line 44.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <cmath>
using namespace std;
class calcformulae
{
    private:

        //vars--------------------------------------------------------------------

        double vc,vs,vco,vcy,sac,sas,saco,sacy,l,b,h,ll,bb,hh,r,rr,rrr,rd,hhh,hd,rco,rcc,hco,hcc;

        //eqns--------------------------------------------------------------------

        void cuboidv(){vc=l*b*h;}
        void cuboidsa(){sac=2*hh*(ll+bb);}

        void spherev(){vs=(4/3)*(22/7)*r*r*r;}
        void spheresa(){sas=4*(22/7)*rr*rr;}

        void cylinderv(){vcy=(22/7)*rrr*rrr*hhh;}
        void cylindersa(){sacy=2*(22/7)*rd*(rd+hd);}

        void conev(){vco=(1/3)*(22/7)*rco*rco*hco;}
        void conesa(){saco=(22/7)*rcc*(rcc+sqrt(rcc*rcc+hcc*hcc));}

    public:

        //setters-------------------------------------------------------------------------

        void setCuboidv(double x,double y,double z){l=x;b=y;h=z;}
        void setCuboidsa(double xx,double yy,double zz){ll=xx;bb=yy;hh=zz;}

        void setSpherev(double w){r=w;}
        void setSpheresa(double ww){rr=ww;}

        void setCylinderv(double www,double zzz){rrr=www;hhh=zzz;}
        void setCylindersa(double wd,double zd){rd=wd;hd=zd;}

        void setConev(double wco,double zco){rco=wco;hco=zco;}
        void setConesa(double wcc,double zcc){rcc=wcc;hcc=zcc;}

        //getters--------------------------------------------------------------------------

        double getCuboidv(){vc=l*b*h; return vc;} //////////////////////////////
        double getCuboidsa(){return sac;}
        double getSpherev(){return vs;}
        double getSpheresa(){return sas;}
        double getCylinderv(){return vcy;}
        double getCylindersa(){return sacy;}
        double getConev(){return vco;}
        double getConesa(){return saco;}
};

//implementation code----------------------------------------------------------------------

int main()
{
    calcformulae cf;
    cout<<"\nVOLUME, SURFACE AREA CALCULATOR";
    cout<<"\n-------------------------------\n\n";
    cout<<"Calculate:\n\n1)Volume \nOR\n2)Surface area?\n\n";

    int selector,shapeselector;
    double i,o,p;
    cin>>selector;

    switch(selector)
    {   case 1: case 2:
        cout<<"\nOf:\n\n1)Cuboid\n2)Sphere\n3)Cylinder\nOR\n4)Cone?\n\n";
        cin>>shapeselector;

        switch(shapeselector)
        {
            case 1:

            cout<<"\nEnter length:\n\n";
            cin>>i;

            cout<<"\nEnter width:\n\n";
            cin>>o;

            cout<<"\nEnter height:\n\n";
            cin>>p;

            switch(selector)
            {
                case 1: cf.setCuboidv(i,o,p);  cout<<endl<<cf.getCuboidv();  break;
                case 2: cf.getCuboidsa(); cout<<endl<<cf.getCuboidsa(); break; /////////////////////
            }
            break;

            case 2:

            cout<<"\nEnter radius:\n\n";
            cin>>i;

            switch(selector)
            {
                case 1: cf.setSpherev(i); cout<<endl<<cf.getSpherev(); break;
                case 2: cf.setSpheresa(i); cout<<endl<<cf.getSpheresa(); break;
            }
            break;

            case 3: case 4:

            cout<<"\nEnter radius:\n\n:";
            cin>>i;

            cout<<"\nEnter height:\n\n:";
            cin>>o;

            switch(shapeselector)
            {
            case 3:

                switch(selector)
                {
                    case 1: cf.setCylinderv(i,o); cout<<endl<<cf.getCylinderv(); break;
                    case 2: cf.setCylindersa(i,o); cout<<endl<<cf.getCylindersa(); break;
                }
            break;

            case 4:

                switch(selector)
                {
                    case 1: cf.setConev(i,o); cout<<endl<<cf.getConev(); break;
                    case 2: cf.setConesa(i,o); cout<<endl<<cf.getConesa(); break;
                }
            break;
            } break; ////////////////////////////
            default: cout<<"\nERROR\n\n\n";
            main();//////////////////////// use goto
        }
        break; ///////////////////////////
        default: cout<<"\nERROR\n\n\n";main();/////////// use goto
    }


return 0;
}
Last edited on
Thanks for the advice everybody. Just to be clear, here is my interpretation of all your answers:

1)Too many nested switches making stuff complex-- simplify.
2)Encapsulate different shapes in different classes.
3)Fix goof-ups (like line 88).

Thanks for the help.
Topic archived. No new replies allowed.