Function program won't compile

Nov 5, 2016 at 2:10am
Any particular reason my code isn't working? I'm trying to use a function in a function to find the volume of a cylinder. Any help would be appreciated thanks.

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

using namespace std;

float areaofcircle(float circle, int radius)
{
	//scope of the function
	float area = 0;

	area = circle * radius;
	return area;
}

float volumeofcylinder(float areaofcircle, int height)
{
	//volume of cylinder = areaofcircle * height
	float cylinder = 0;
	cylinder = areaofcircle * height;
		return cylinder;
}
int main()
{
	float radius = 2.5;
	float heightofcylinder = 5.0;

	float cylinder = volumeofcylinder(float radius, float heightofcylinder)
		cout << cylinder;
	cin.get();
	cin.get();
}


Also it said I had problems at line 26
Last edited on Nov 5, 2016 at 2:11am
Nov 5, 2016 at 2:23am
float cylinder = volumeofcylinder(float radius, float heightofcylinder)

Should be :
float cylinder = volumeofcylinder(radius, heightofcylinder); // Don't forget the semi-colon (;)!
Nov 5, 2016 at 2:40am
Thank you I got it fixed and the answer was 12.5 but I did the calculations myself and got 98 as my answer
Nov 5, 2016 at 2:42am
Thank you I got it fixed and the answer was 12.5 but I did the calculations myself and got 98 as my answer

How was 98 calculated?
Nov 5, 2016 at 2:45am
well the volume of a cylinder is pi*r^2*height
pi* (2.5)^2*5 gives me 98
Nov 5, 2016 at 2:50am
1
2
3
4
5
6
7
float volumeofcylinder(float areaofcircle, int height)
{
	//volume of cylinder = areaofcircle * height
	float cylinder = 0;
	cylinder = areaofcircle * height;
	return cylinder;
}


Should be :
1
2
3
4
5
float volumeofcylinder(float areaofcircle, int height)
{
	float cylinder = 3.14 * (areaofcircle * areaofcircle) * height;
	return cylinder;
}
Last edited on Nov 5, 2016 at 2:50am
Nov 5, 2016 at 2:55am
wow that makes sense now thank you so much!
Nov 5, 2016 at 7:30am
Just to make sure something important does not get forgotten, I do have a few points that need to be looked into by the OP:

1.
1
2
3
4
5
6
7
8
float areaofcircle(float circle, int radius)
{
	//scope of the function
	float area = 0;

	area = circle * radius;
	return area;
}


if radius receives a float value (i.e. 2.5) then it will lose precision and end as a radius of 2 because it is defined as an integer in the function.
I would change the above function anyway and remove the first parameter float circle
and fix the formula for area.

2.
The same will happen with the height of the cylinder in

1
2
3
4
5
6
7
float volumeofcylinder(float areaofcircle, int height)
{
	//volume of cylinder = areaofcircle * height
	float cylinder = 0;
	cylinder = areaofcircle * height;
		return cylinder;
}


where height is also defined as an integer.

3.
In the main() function, I would think of replacing
float cylinder = volumeofcylinder(float radius, float heightofcylinder)

with
float cylinder = volumeofcylinder( areaofcircle(radius), heightofcylinder);

With corrections made, the value printed would be in the range of 98.1748 depending on the precision of pi. (given r=2.5 and h=5.0)
Nov 6, 2016 at 1:42am
makes sense thank you
Topic archived. No new replies allowed.