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

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
 #include <iostream>
#include <iomanip>
#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.

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

// 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.