Coding with Function problem

I am working on a code using a function, to calculate the area and circumference of a circle once the radius is collected from the user. The issue I am experiencing is after I get the radius from the user, The program exits before excuting the function. I entered my code so far. Any help would be appreciated.

Thank you.


#include<iostream>
#include<cmath>
using namespace std;
double circumferencecalc(double circumference,double x);
int main()
{
double radius;
double area = radius*radius*3.14159;

cout << "Please enter the radius of the circle";
cin >> radius;

cout << "The area of the circle is " << area << endl;

}

double circumferencecalc(double circumference, double x)
{
double radius;
cout << "The circumference of the circle is " << circumference << endl;

system("pause");
return circumference;
}
Last edited on
Your previous thread about this program is: http://www.cplusplus.com/forum/beginner/202867/

I told there that you should read something. Do it again, carefully.
Last edited on
Hello,

Your trying to calclate the area before you input the radius.
You never call "double circumferencecalc(double circumference, double x)" from main.
Apart from that the circumference of a circle is 2 x Radius x Pi.
Main should return a value (0 or 1, success or failure).
Try these adjustments and repost (Please use code tags).

Regards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<cmath>
using namespace std;
double circumferencecalc(double circumference,double x);
int main()
{
double radius;
double area = radius*radius*3.14159;

cout << "Please enter the radius of the circle";
cin >> radius;

cout << "The area of the circle is " << area << endl;

}

double circumferencecalc(double circumference, double x)
{
double radius;
cout << "The circumference of the circle is " << circumference << endl;

system("pause");
return circumference;
}
Last edited on
All:

I really appreciate the help. I am still struggling with getting the main function to transfer into the function I added. My new code is below. Not really sure if I am close or completely off. Do we know of any examples I could look at?

#include<iostream>
using namespace std;
double calculatecircumference(double);
int main()
{
double i;
cout << "Please enter the radius of the circle: ";
cin >> i;

cout << calculatecircumference(i);


double calculatecircumference(double);
{

if (i > 0)
{
double area = 3.14159 * i*i;
double circumference = 2 * 3.14159*i;
cout << "The area of your circle is: " << area << endl;
cout << "The circumference of your circel is: " << circumference << endl;
}
else
cout << "Please enter a positive integer." << endl;

system("pause");
return 0;
}

}

Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

You can't define a function in a function.
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
#include <iostream>
using namespace std;

// give these magic numbers a name to help
// with readability
const double PI{ 3.14159 };

double calculate_circumference( double );
double calculate_area( double radius );

int main( )
{
    // use more descriptive names
    double radius;
    cout << "Please enter the radius of the circle: ";
    cin >> radius;

    cout << "circumference = " << calculate_circumference( radius ) << "\n";
    cout << "radius = " << calculate_area( radius ) << "\n";
}

// this function is made to calculate the circumference
// and return it -> no more and no less
// also please use underscores or camel case
// so that the function doesn't look like a reallylongwordlikethis
double calculate_circumference( double radius )
{
    // invalid radius so we return an invalid circumference
    // that way we can check in the calling function
    if( radius < 0.0 ) return -1.0;
    return 2 * PI * radius;
}
double calculate_area( double radius )
{
    if( radius < 0.0 ) return -1.0;
    return PI * radius * radius;
}
Last edited on
All:

Thank you. I am reviewing and studying this for better understanding. Again everything from everyone is appreciated.

Also thank you for the link to code tags, I didn't know how to do that either.
Topic archived. No new replies allowed.