Hello, I started C++ and I have a trouble with this thing. I can't make it work. What I am trying to do is to get the value and calculate the area of a circle using a class type of coding style.
Note that this does not even compile.
"Where should i put the cin"

#include <iostream>

using namespace std;

const double Pi = 3.1415;

class master
{
	private:
		double radius;
	public:
		void setvalue(double);
		double getvalue();
		double area(double);

};
void master :: setvalue(double a)
{
	radius = a;
};
double master :: getvalue()
{
	return radius;
};

double master :: area(radius)
{
	return Pi * radius * radius;
}
int main()
{
	master z01;
	cout << "Please Enter A Radius \n";
	cout << "Press <ENTER> to end... " << endl;
	fflush(stdin);
	cin.get();
}

Dani AI

Generated

The simplest fix is: read the radius in main, validate it, store it in the object (via a setter or constructor) and call a parameterless area() that uses the stored radius. had the right idea but the member area should not require an external parameter when the object already owns radius. 's remarks about avoiding using namespace std and improving Pi precision are also sound.

A few practical rules and caveats:

  • Give the setter a clear name (for example setRadius) and make area() a const member that takes no arguments. This keeps responsibility clear: the object stores state, and area() computes from that state.
  • Avoid fflush(stdin) — it is undefined behavior on many platforms. To discard bad input or pause for Enter, use std::cin.clear() and std::cin.ignore(...) instead.
  • Validate input (check std::cin.fail() and non-negative radius) so the program does not compute nonsense.
  • Prefer a more accurate Pi (or std::numbers::pi in C++20). Keep global state minimal.

Example showing these points (compact, beginner-friendly):

#include <iostream>
#include <limits>
#include <iomanip>

constexpr double PI = 3.14159265358979323846;

class Circle {
    double radius_;
public:
    explicit Circle(double r = 0.0) : radius_(r) {}
    void setRadius(double r) { radius_ = r; }
    double getRadius() const { return radius_; }
    double area() const { return PI * radius_ * radius_; }
};

int main() {
    Circle c;
    double r = 0.0;
    std::cout << "Enter radius: ";
    while (!(std::cin >> r) || r < 0.0) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Invalid radius. Enter a non-negative number: ";
    }
    c.setRadius(r);
    std::cout << "Area = " << std::fixed << std::setprecision(4) << c.area() << '\n';
    return 0;
}

Additional improvements: use a constructor to initialize radius, add tests for border cases, and consider std::numbers::pi when targeting modern compilers.

Recommended Answers

All 2 Replies

Sorry, now it compiles, but I need to add a cin somewhere.

#include <iostream>

using namespace std;

const double Pi = 3.1415;

class master
{
	private:
		double radius;
	public:
		void setvalue(double);
		double getvalue();
		double area(double);

};
void master :: setvalue(double a)
{
	radius = a;
};
double master :: getvalue()
{
	return radius;
};

double master :: area(double radius)
{
	return Pi * radius * radius;
}
int main()
{
	master z01;
	cout << "Please Enter A Radius \n";
	cout << "Press <ENTER> to end... " << endl;
	fflush(stdin);
	cin.get();
}
using namespace std; // don't
 
const double Pi = 3.1415; // why not 3.1416 (3.14159)
int main() {
  master z01;
  double r;
  std::cout << "Please Enter A Radius \n";
  std::cin >> r;
  z01.setvalue(r); // setvalue is really ambiguous, consider changing function name
  std::cout << "The area is: " << z01.area() << std::endl;
  std::cout << "Press <ENTER> to end... " << std::endl;
  fflush(stdin); // consider removing these 2 lines and making the above a loop
  std::cin.get();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.