### In need of help with basic functions for finding the area of a circle Write a program that determines the area of a circle. It should include the following functions :

getRadius - this function asks the user for the radius of a circle and returns it as a double.

getArea - this function accepts the radius of a circle as it's only argument and returns the circle's area as a double. Area is calculated by multiplying 3.14159 * radius * radius.

displayData - this function accepts the circle's radius and area as it's only arguments and displays them in an appropriate message on the screen. The area should be formatted in fixed point notation to 2 decimal places of precision.

~ I got this far and I'm stuck with multiple issues. If someone could help, that would be fantastic.
Thanks
-Max

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` `````` #include #include #define PI 3.14159 using namespace std; float getRadius(); float getArea(); void displayData(float radius, float area); int main() { float radius = getRadius(); float area = getArea(); displayData(radius, area); cout << "Please enter the radius:"; cin >> getRadius; cout << setprecision(2) << radius << area; } float getRadius() { cin >> radius; while (radius < 0) { cout << "Not a valid input, please input a positive number greater than 0\n"; cin >> radius; } return (radius); } float getArea() { area = PI * radius * radius; return (area); } float displayData(float radius, float area) { return area; } `````` I'm very new to coding, and this may not be perfect, but I think I managed to get it working.

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include #define PI 3.14159 using namespace std; float getRadius(); float getArea(float radius); int main() { float radius; float area; cout << "Please enter the radius: "; radius = getRadius(); area = getArea(radius); cout << fixed << setprecision(2) << "\nRadius: " << radius << "\nArea: " << area; } float getRadius() { float radius; cin >> radius; while (radius < 0) { cout << "\nNot a valid input, please input a positive number greater than 0: "; cin >> radius; } return (radius); } float getArea(float radius) { float area; area = PI * radius * radius; return (area); }``````
Last edited on ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include // see: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-macros2 // #define PI 3.14159 const double PI = 3.14159 ; // using namespace std; // favour double as the default floating point type double getRadius(); // float getRadius(); double getArea( double radius ); // float getArea(float radius); int main() { // https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-init // float radius; // float area; std::cout << "Please enter the radius: "; // const: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-const const double radius = getRadius(); const double area = getArea(radius); //std::cout << std::setprecision(6) << "\nRadius: " << radius << "\nArea: " << area; std::cout << std::setprecision(6) << "\nRadius: " << radius << "\nArea: " << area << '\n' ; } double getRadius() { double radius; std::cin >> radius; while (radius < 0) { std::cout << "\nNot a valid input, please input a positive number greater than 0: "; std::cin >> radius; } return radius ; } double getArea( double radius ) { // double area; const double area = PI * radius * radius; return area; }`````` Rather than having free-floating functions in the global namespace it would be better to create a `Circle` class that owns its data, encapsulating it from external access and then has accessor functions returning its attributes (e.g., radius).

An insertion operator (i.e., `operator<<`) that takes an instance of `Circle`, implemented in terms of a print member function on `Circle` could then dictate the format of its output in a localised way.

 `main.cpp`
should then only include a header for your new `Circle` class and its `operator<<` and a header containing your numerical constants (libraries like boost have these already if your curious http://www.boost.org/doc/libs/1_46_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/constants.html).

Try to encapsulate the contents of both headers in their own namespaces, e.g., `namespace geometry` and refer to them outside of it as via a scope specification operator e.g., `geometry::Circle`.
Topic archived. No new replies allowed.