Having trouble returning values

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;
}

double area(double &radius, double &areacircle)
{
const double PI = 3.14159;
cout << fixed << showpoint << setprecision(2);
areacircle = PI * radius * radius;
return areacircle;
}

Yes to the compiler "areacircle" is undefined because its a parameter of the function "area".

look at the modifed code:


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

#include<iostream>
using namespace 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

	const double 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 :)
Last edited on
Topic archived. No new replies allowed.