Can't seem to cout.

I'm trying to have it cout the area, perimeter, and circumference, but I'm not sure where I'm going wrong for it.

I'm trying to cout the perimeter in this set, but it prints 2 above everything, asks for radius, but does nothing with it?

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
#include <iostream>
#include "circle.h"
using namespace std;

//PI (type double and also should be a static member)
double Circle::PI = 3.14;


//Question 1a) Default constructor setting the radius to 1 if no value passed otherwise set radius to value passed.
Circle::Circle(double r)
{
  radius = r;
}

//print for my purpose to check my work
void Circle::print()const
{
  cout << "\nRadius \t= " << radius;
  cout << "\nPI \t= " << PI;
  
//Then write program to test Circle class by asking user for the radius of circle then create a circle object and report the circle’s area, diameter and circumference.
  double potato;
  cout << "\nWhat is the radius?" << endl;
  cin >> potato;
}

//Question 1b) setRadius() mutator.
void Circle::setRadius(double r)
{
  radius = r;
}

//Question 1c) getRadius() accessor.
double Circle::getRadius()
{
  return radius;
}

//Question 1d) getArea() accessor 
//which return the area after using this formula:
//area = PI * radius (squared)
double Circle::getArea()
{
  double area = PI * radius*radius;
  return area;
}


//Question 1e) getPerimeter() accessor which return the perimeter
//after using this formula:
//perimeter = 2 * radius
double Circle::getPerimeter()
{
  double potato = (2 * radius);
  cout<< potato;
  return perimeter;
}

//Question 1f) getCircumference() accessor which return the
//circumference after using this formula:
//circumference = 2 * PI * radius
double Circle::getCircumference()
{
  double circumference = 2 * PI * radius;
  return circumference;
}


//main function
int main()
{
  Circle c1;
  Circle c2(5.4);
  c1.getPerimeter();
  
  c1.print();
  cout << endl;
  
  c2.print();
  cout << endl;


  
  return 0;
}
Last edited on
Hello Garribean,

You include the class function definitions and main, but not the header file with the class definition. Just saying. It may not make any difference, but helps to know what is going on. It also helps so I can load and compile the program for testing.

I will look over what you have and let you know what I find.

Andy
Hello Garribean,

Line 2. I do not know what is in "circle.h", so it makes it hard to know what is wrong.

Line 3. Best not to use this. You should start learning what is in the "std::" name space and how to qualify those variables and functions.

Line 6. Looks like you are trying to define a function, but you are setting a variable. I do not think this will work. For now I changed it to: constexpr double PI = 3.14159;. If you are going to define PI yourself try to use as many decimal places as you can for a more accurate result.

Lines 9 and 10:
1
2
//Question 1a) Default constructor setting the radius to 1 if no value passed otherwise set radius to value passed.
Circle::Circle(double r)

This in not the default ctor it is an overloaded ctor and will only set the value of "radius" to the value of "r" unless the declaration in the class has a default value if no variable is passed.

This would be more inline with a default ctor:
1
2
3
4
5
Circle::Circle()
{
    radius = 0;
    // <--- Any other variables that would need to be set.
}


And it would be a good idea to define a default dtor:
1
2
3
Circle::~Circle()
{
}


Line 16. The print function. Lines 18 and 19 make some sense, but after that this makes no sense:
//Then write program to test Circle class by asking user for the radius of circle then create a circle object and report the circle’s area, diameter and circumference.

The print function should only print and nothing more. Lines 21 - 24 should be a separate function. Then there is the point that you define "potato", which means what exactly, and then never use this variable.

Line 28. A function that is never used.

Line 34. Will work.

Line 42. Function works, but you never use the return value. Depending on what is in the class this function may not be written correctly.

Line 52. This function is about half right. Again you define "potato" and give it a value. Print it out, that is OK. And then return "perimeter". Where is "perimeter" defined and where does it get its value?

Line 62. Looks OK, but never used. Again depending on how you set up the class this may be wrong too.

Line 72. You create an object of "Circle", but with no default ctor it contains garbage for the variable value(s).

Line 73. Creates another object, but this time the overloaded ctor is used to initialize the variable "radius".

Line 74. calls the "getPerimeter()" function of the class, but you do not collect or use the return value of the function.

Line 76. Will work, but you will be printing a garbage value.

Line 79. Is a little better. At least the value of "radius" will not be garbage.

When I refer to a variable as containing garbage this means that since the variable was not given an initial value or otherwise initialized it contains whatever is in the memory location when space for that variable was set aside.

Hope that helps,

Andy
Topic archived. No new replies allowed.