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;
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).
#include<iostream>
#include<cmath>
usingnamespace 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;
}
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;
#include <iostream>
usingnamespace std;
// give these magic numbers a name to help
// with readability
constdouble 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;
}