Void function not returning correct value

The program should return the total surface area, but instead only returns 1. How can I fix this?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 #include <iostream>
#include <cmath>
using namespace std;

//function prototypes
float calcBaseArea(float r);
float calcSideArea(float r, float h);
void prntSurfArea(float r, float h);

int main()
{
	float h;
	float r;
	float base_area;
	float side_area;
	
	
	cout << "Enter the length of the radius for the right cylinder in feet: ";
	cin >> r;
	
	cout << "Enter the height of the right cylinder in feet: ";
	cin >> h;
	
	base_area = calcBaseArea(r);
	cout << "Base surface area of the right cylinder is " << base_area<< " square feet."<< endl;
	
	side_area = calcSideArea(r, h);
	cout << "Side surface area of the right cylinder is " << side_area<< " square feet."<<endl;
	
	
	prntSurfArea( base_area, side_area);
	cout << "Total surface area of the right cylinder is " << prntSurfArea << " square feet." <<endl;
	
	return 0;
	
}

float calcBaseArea(float r)
{
	return 3.14*pow(r, 2);
}

float calcSideArea(float r, float h)
{
	return h*(2*3.14*r);
}

void prntSurfArea(float r, float h)
{
	calcBaseArea(r) + calcSideArea(r, h);
}
You're calling the function wrong.
First, calling a function requires using (), e.g. prntSurfArea(r, h).

But your prntSurfArea function is void. That means it doesn't return anything.

Perhaps you instead want:
1
2
3
4
float surfArea(float r, float h)
{
    return calcBaseArea(r) + calcSideArea(r, h);
}

and calling it like: surfArea(r, h); from main.

PS: I think you need to add the base area twice (one for each circle face).
Last edited on
Thank you!I think I got confused do to the wording of the assignment. I am still a little confused as the assignment my professor gave me said the return type of the function should be void.
Here is the assignment:

"Your other task is to add a new function called prntSurfArea that accepts
the base area and side area values (i.e., these are the parameters) and calculates
and prints out the total surface area of the right cylinder inside this function.
Since this function does not return a value to the calling function, the return type
of this function should be void."
Two issues:
1. You're passing in the calculated base_area and side_area. This is good, but you are treating it as if you're passing in the radius and height from within the actual function, so you're calculations will be wrong.
2. Okay, so keep it as a void function, but you want a cout << statement to be inside the prntSurfArea function itself. Then remove the existing line 32, and keep line 31 unchanged.
Last edited on
Topic archived. No new replies allowed.