I have to compute the area of a circle in a fuction called area. Main is supposed to prompt the user for the radius first. I keep getting that the variable areacircle is undefined. I am not sure why.
#include<iostream>
#include <iomanip>
using namespace std;
double area(double &radius, double &areacircle);
int main()
{
cout << "Please enter the radius of the circle: ";
double radius;
cin >> radius;
area(radius, areacircle);
cout << "A circle with a radius of " << radius << " has an area of " << areacircle << endl << endl;
return 0;
}
#include<iostream>
usingnamespace std;
double area(double radius); // removed areacircle, dont need the &
int main()
{
double result; // for result of function
cout << "Please enter the radius of the circle: ";
double radius;
cin >> radius;
result = area(radius); // removed areacircle, result holds value returned from area
cout << "A circle with a radius of " << radius << " has an area of " << result << endl << endl;
return 0;
}
double area(double radius) // dont need the &, removed areacircle
{
double areacircle; // new
constdouble PI = 3.14159;
cout << fixed << showpoint << setprecision(2);
areacircle = PI * radius * radius;
return areacircle;
}
If you have any questions please give me a shout :)
EDIT: Btw, it would really help if you put your code inside code tags in future :)