Output Values are negative in Circle class

Hello,

I am working on a HW assignment and I have been stuck for a few days. I finally got it to compile but the values are all off and I cannot figure out where I am going wrong. I have reread the chapter and read all the tutorials. I am ripping my hair out here !!

Here is the prompt..
Write a program that reads the radius of a circle, as a double value, and computes and prints the diameter, circumference, and the area. The program should be able to compute this information for any number of radius values, until the radius value entered is < 0. The user should also be able to immediately exit the program if the first radius value entered is < 0. Use 3.14159 for the value of pi.
Your solution should contain a class called Circle, with methods GetDiameter, GetCircumference, and GetArea that compute the needed values. Your solution should separate interface from implementation, by having 3 files; Circle.h, Circle.cpp and Main.cpp.


Here is my Main.cpp

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
#include <iostream>
#include <cstdlib>
#include "simpleCircle.h"

using std::cout;
using std::cin;
using std::endl;

int main()
{
	SimpleCircle simpleCircle;

	double radiusVal;

	cout << "Enter the Radius of the Circle: ";
	cin >> radiusVal;

	
	if(radiusVal>0)
	{

	
		cout << "Radius = " << simpleCircle.getRadius() << endl;
		cout << "Diameter = " << simpleCircle.diameter() << endl;
		cout << "Circumference = " << simpleCircle.circumference() << endl;
		cout << "Area = " << simpleCircle.area() << endl;

	} 
	else
	{
		cout << "Radius cannot be 0!" << endl;
	}
    return 0;

}

Here is my Header File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SIMPLECIRCLE_H
#define SIMPLECIRCLE_H

class SimpleCircle
{
public:
	double getRadius();
	void setRadius(double rad);
	double diameter();
	double circumference();
	double area();

private:
	double radiusVal;
	

};

#endif 


Here is my Circle.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "simpleCircle.h"

double SimpleCircle::getRadius()
{
	return radiusVal;
}
void SimpleCircle::setRadius(double rad)
{
	radiusVal= rad;
}
double SimpleCircle::diameter()
{
	return 2 * (radiusVal);
}
double SimpleCircle::circumference()
{
	return 2 * 3.14159 * radiusVal;
}
double SimpleCircle::area()
{
	return 3.14159 * (radiusVal * radiusVal);
}


Here is my output for radius 2

Enter the Radius of the Circle: 2
Radius = -9.25596e+61
Diameter = -1.85119e+62
Circumference = -5.81569e+62
Area = 2.69149e+124
Last edited on
The radiusVal member of simpleCircle is uninitialised.
Call setRadius first, before performing other computations with the value.

Ideally write a constructor and use that for initialisation, and make the functions const-correct.
https://isocpp.org/wiki/faq/const-correctness#const-member-fns
Last edited on
Ahh ! it worked !! thank you !!!

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
#include <iostream>
#include <cstdlib>
#include "simpleCircle.h"

using std::cout;
using std::cin;
using std::endl;

int main()
{
	SimpleCircle simpleCircle;

	double radiusVal;

	cout << "Enter the Radius of the Circle: ";
	cin >> radiusVal;

	
	if(radiusVal>0)
	{
		simpleCircle.setRadius(radiusVal);
	
		cout << "Radius = " << simpleCircle.getRadius() << endl;
		cout << "Diameter = " << simpleCircle.diameter() << endl;
		cout << "Circumference = " << simpleCircle.circumference() << endl;
		cout << "Area = " << simpleCircle.area() << endl;

	} 
	else
	{
		cout << "Radius cannot be 0!" << endl;
	}
    return 0;

}



Enter the Radius of the Circle: 2
Radius = 2
Diameter = 4
Circumference = 12.5664
Area = 12.5664
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
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>

class SimpleCircle
{
public:
	SimpleCircle() {}
	SimpleCircle(double r) : radiusVal(r) {}
	double getRadius() const;
	void setRadius(double rad);
	double diameter() const ;
	double circumference() const;
	double area() const;

private:
	double radiusVal {};
};

double SimpleCircle::getRadius() const
{
	return radiusVal;
}

void SimpleCircle::setRadius(double rad)
{
	radiusVal = rad;
}

double SimpleCircle::diameter() const
{
	return 2 * radiusVal;
}

double SimpleCircle::circumference() const
{
	return 2 * M_PI * radiusVal;
}

double SimpleCircle::area() const
{
	return M_PI * (radiusVal * radiusVal);
}

int main()
{
	double radiusVal {};

	std::cout << "Enter the Radius of the Circle: ";
	std::cin >> radiusVal;

	if (radiusVal > 0) {
		const SimpleCircle simpleCircle(radiusVal);

		std::cout << "Radius = " << simpleCircle.getRadius() << '\n';
		std::cout << "Diameter = " << simpleCircle.diameter() << '\n';
		std::cout << "Circumference = " << simpleCircle.circumference() << '\n';
		std::cout << "Area = " << simpleCircle.area() << '\n';

	} else
		std::cout << "Radius cannot be 0 or negative!\n";
}

The manifest constant M_PI is not standard C or C++; it is a Posix extension.
(AFAIK, on Posix, _USE_MATH_DEFINES is not required unless the implementation is quite old).

C++20 has std::numbers::pi and a several other mathematical constants.
https://en.cppreference.com/w/cpp/header/numbers
Topic archived. No new replies allowed.