Can someone take a look at my code.. I am getting some crazy numbers

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
#include "pointType.h"
#include "circleType.h"
#include "cylinderType.h"
#include <iostream>

using namespace std;

int main()
{

        pointType point;

        double x, y;
	cout<<" Access ID: eq8708"<<endl;
	cout<<" Program 1"<<endl;

	cout << "Enter the x-coordinate: ";
	cin >> x;

	cout << "Enter the y-coordinate: ";
	cin >> y;

	point.setPoints(x, y);
	point.print();
	cout <<" Program 2"<<endl;
	circleType circle;

        double r;

	cout << "Enter the radius of the circle: ";
	cin >> r;

        circle.setRadius(r);
	circle.print();

	cylinderType cylinder;
 
    double h;
 
    cout << "Enter the height of the cylinder: ";
    cin >> h;
 
    cylinder.setHeight(h);
    cylinder.print();




[code]#ifndef H_CylinderType
#define H_CylinderType
 
#include "circleType.h"
 
using namespace std;
 
class cylinderType: public circleType
{
 
public:
 
    void setHeight(double cHeight);
     
    void print() const;
 
    double getHeight() const;
 
    double calcVolume()const;
 
    double calcSurface() const;
 
    cylinderType();
 
    cylinderType(double cHeight);
 
private:
 
    double height;
 
};
 
#endif 


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
#include "cylinderType.h"
#include <iostream>
#include <cmath>
#include <iomanip>
 
using namespace std;
 
void cylinderType::setHeight(double cHeight)
{
    height = cHeight;
}
 
void cylinderType::print() const
{
    cout << "The height of the cylinder is: " << getHeight() << endl;
    cout << "The radius of the cylinder base is: " << getRadius() << endl;
    cout << "The volume of the cylinder is: " << calcVolume() << endl;
    cout << "The surface area of the cylinder is: " << calcSurface() << endl;
    cout << fixed << setprecision(2) << "The x and y coordinates are: ";
    cout << getX() << "," << getY() << "." << endl;
}
 
double cylinderType::getHeight() const
{
    return height;
}
 
double cylinderType::calcVolume() const
{
    double pi = 3.14159;
    return (height * pi) * (getRadius() * getRadius());
}
 
double cylinderType::calcSurface() const
{
    double pi = 3.14159;
    return (2 * calcArea()) + (calcCir() * height);
}
 
cylinderType::cylinderType()
{
    height;
}
 
cylinderType::cylinderType(double cHeight)
{
    height = cHeight;
}
Last edited on
If you are getting "Crazy" numbers it is probably due to an undefined variable somewhere. Anyways what is up with this your default constructors. You just evaluate a variable and no nothing with it which is probably where the undefined is.
I cant find whats wrong..hence why i need a second person to take a look
If you were a little more specific on which parts you are getting crazy numbers it would help. Why are you calling getx and gety inside the print function in your point class? Why not simply access the x and y variables.


my radius and surface for cylinder is like 980000000000099980000
so something really really huge when my height is like 2
closed account (D80DSL3A)
I think that cylinder.r is never assigned a value. There are problems with the constructors, etc. as giblet says, but a quick fix may be to call cylinder.setRadius(r); at about line 42 in your main() function.
Thanks fun2code!!!!
Its doing the same issue for x and y cordinates I guess i do the same thing right?
closed account (D80DSL3A)
Yes. I didn't notice that. Call setPoints(double, double) on both circle and cylinder.

Or better yet! Write constructors for circleType and cylinderType which take all the relevant parameters for the object. ie x,y,r for a circle and x,y,r,h for a cylinder. Collect the values from the user then use them when constructing (declaring) the object. This would be cleaner.
Last edited on
Can you explain the second option again?
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
#include <iostream>

class Circle
{
    public:
        Circle(double const radius); //all variables required on creation
        //They should be private/protected variables
        double area(void) const;
    private:
        double radius;
        static double constexpr pi = 3.14159;
};

Circle::Circle(double const radius) : radius(radius){}

double Circle::area(void) const
{
    return pi * radius * radius;
}

int main()
{
    Circle c(5.27);
    std::cout << c.area() << std::endl;
    return 0;
}
87.2511
closed account (D80DSL3A)
OK. Suppose you add a constructor to the pointType class taking 2 doubles.
pointType::pointType(double X, double Y): x(), y(Y) {}// intializer list method
Or
pointType::pointType(double X, double Y): {x=X; y=Y;}

With this ctor (constructor) you can do this in main:
1
2
3
4
5
6
cout << "Enter the x-coordinate: ";
	cin >> x;
	cout << "Enter the y-coordinate: ";
	cin >> y;
	pointType point(x, y);
	point.print();

Then for circleType you can have:
circleType::circleType(double X, double Y, double R): {x=X; y=Y; r=R;} and in main:
1
2
3
4
5
6
7
8
"Enter the x-coordinate: ";
	cin >> x;
	cout << "Enter the y-coordinate: ";
	cin >> y;
        cout << "Enter the radius: ";
	cin >> ;
	circleType circle(x, y, r);
        circle.print();

Extend this to cylinderType.
Last edited on
thanks but could you help me with the x and y cordination part? I am getting like really really big numbers
i am getting this error
circleType::circleType' : no overloaded function takes 3 arguments
You have to create the prototype not just copy his definition. Look at my example is has the prototypes and definitions. Then you can use that to modify yours like fun2code mentioned.

By the way why did you remove about half of the original post?
Last edited on
yeah i didnt want people to just copy it
Topic archived. No new replies allowed.