Cicle Class Project

Hello, I am hoping someone can tell me why I am getting this error in my code.
Code is for an assignment Instructions Below:
"Construct a class named Circle that has a floating-point data member named radius. The class should have a zero-argument constructor that initializes this data member to 0. It should have member functions named calcCircumference() and calCarea() that calculate the circumference and area of a circle respectively, a member function setRadius()to set the radius of the circle, a member function getRadius() to return the radius, and a member function showData() that displays the circle’s radius, circumference, and area. The formula for the area of a circle is A=πr2. The formula for the circumference of a circle is C=2πr.

The class should use appropriate protection levels for the member data and functions. It should also follow “principles of minimalization”: that is, no member data should be part of a class unless it is needed by most member functions of the object. A general rule of thumb is that “if you can easily calculate it, don’t store it.”

Use your class in a program that creates an instance of a Circle (utilizing the zero-argument constructor), prompts a user for a radius, calls the setRadius() function to set the circle’s radius, and then calls showData() to display the circle’s radius, circumference, and area. Your program should allow the user to enter circle dimensions until the user enters -1. Be sure to include appropriate error checking. Does it make sense to enter “abc” as the radius of a circle? No. Therefore, you should ensure that the user enters numeric data for the radius. Negative numbers (other than the -1 to exit) should also be prevented."

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
#include <iostream>
#include <limits>

#include "Circle.h"
#include "Circle.cpp"


using namespace std;

int main()
{
	cout << "-Assignment 2" << endl;


	double InputRadius;

	while (true) {
		cout << "Enter the Radious";
		if (!(cin >> InputRadius));
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		continue;

		if (InputRadius == -1.0)
		{
			break;
		}
		
		circle::setRadius(InputRadius);

		circle::showData();
	
	}


	system("pause");
	return 0;
}

The issue is Line 36 and 38. Both read an error E0245. (This is compiler Visual Studio 2019) error reads nonstatic member reference must be relative to specific object.
I have a header file of:
#pragma once

class circle
{
private:
double radius;
public:

circle();

circle(double rad);

double getRadius() const;

double calCircumference() const;

double calcArea() const;

void setRadius(double rad);

void showData() const;

};

And a functions .cpp file:
#include <iostream>
#include "Circle.h"

#define PI 3.14

circle::circle() : radius(0.0) {}

circle::circle(double rad)
{
if (rad < 0)
radius = 0.0;
else
radius = rad;
}

void circle::setRadius(double rad)
{
if (rad < 0)
radius = 0.0;
else
radius = rad;

}

double circle::getRadius() const
{
return radius;

}

double circle::calCircumference() const {
double circum = 2 * PI * getRadius();
return circum;

}

double circle::calcArea() const {

return (PI * getRadius() * getRadius());
}

void circle::showData() const {
std::cout << "Radius is: " << getRadius() << "Circumference is: " << calCircumference() << "Area is: " << calcArea() << std::endl;
}
Hoping someone can help!
Thanks

Last edited on
1. It would have been helpful if you had used code tags on your custom class header and source files as you did with your driver source.

2. Don't include CPP files, just include header files.

3. Your errors are happening because you haven't create an instance of your class, an object. Create an instance, and then you can call the member functions on the created object:
29
30
31
32
33
   circle myCircle;

   myCircle.setRadius(InputRadius);

   myCircle.showData();

4. Once your errors are fixed your input loop is a continuous loop, it will never "break." Whatever you were trying to achieve isn't working. A simplified driver program might be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>
#include "Circle.hpp"  // I use .hpp, not .h, to designate this is a C++ header

int main()
{
   double InputRadius;

   std::cout << "Enter the Radius: ";
   std::cin >> InputRadius;

   circle myCircle;

   myCircle.setRadius(InputRadius);

   myCircle.showData();
}

5. Including personal information in a public forum, like your name, is not a good idea.
Last edited on
Hello heart1210,

It is best to post the complete error message that you received from the compiler. Your interpretation may not be correct yet.

You say the the error is with lines 36 and 38, but sometimes that is not where the error starts just where the compiler ends up.

I do not see anywhere in "main" that you defined an object of the class.

circle::setRadius(InputRadius); is trying to access the class definition not an object of the class.

If you name the class "Circle" then in "main" you could say Circle circle; and the function call would be circle.setRadius(InputRadius);

I think that should work for you.

Andy
Doing console input correctly when you want to flag non-numeric and certain numbers as invalid is not a simple task as my original example. Using std::string and std::istringstream is a modern C++ approach for that, your input stream will never go into an error state:
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
#include <iostream>
#include <sstream>
#include <string>
#include "Circle.hpp"

int main()
{
   std::string line;
   double      input { };

   // this loop runs until we break out of it
  // read the entire line of input into a std::string
   while ((std::cout << "Please enter a number: ") && std::getline(std::cin, line))
   {
      std::istringstream is { line };

      // re-using `line` to test for extra stuff after the number
      if ((is >> input) && !(is >> line))
      {
         // if the number retrieved is -1/-1.0 that is invalid
         if (input != -1.0) break; // done, we got what we wanted
      }
      std::cerr << "Invalid input, try again.\n";
   }

   circle myCircle;

   myCircle.setRadius(input);

   myCircle.showData();
}

Please enter a number: abc
Invalid input, try again.
Please enter a number: 123abc
Invalid input, try again.
Please enter a number: -1
Invalid input, try again.
Please enter a number: .75
Radius is: 0.75, Circumference is: 4.71, Area is: 1.76625

http://www.lb-stuff.com/user-input
Topic archived. No new replies allowed.