Have it compiled and running but NOT getting calculation right

I have to have the user to enter a code to signal if the person wants volume surface area or cross sectional area of a cylinder
I have it working but the calculation is off anybody think they can help?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void get_sentinel (char code);
void volume(float&, float&);
void surface_area (float&, float&);
void cross_area (float&, float&);


int main()
{
	float radius;
	float height;
	char code;
cout<<" WELCOME! Please enter in CAPITAL letters.\n" 
	" Enter V for volume, A for surface area, X for cross-sectional area,\n"" or Q to quit.\n";
	cin>>code;	
get_sentinel (code);




return 0;
	
}
void get_sentinel(char code)
{
	const float PI=3.14159;
	float radius;
	float height;
	float answer;
	while (code!= 'Q')                         
 {
                                           
      
      if(code == 'V')                         
      {
      	cout<<" Please enter RADIUS.\n";
      	cin>>radius;
      	cout<<" Thank you! Now, please enter HEIGHT.\n";
      	cin>>height;
      	
      	
      	volume(radius, height);
      	
      	cout<<" The volume of the cylinder is\n"<<volume;
    
    
                        
      }
      else if( code == 'A')
      {
        cout<<" Please enter RADIUS.\n";
      	cin>>radius;
      	cout<<" Thank you! Now, please enter HEIGHT.\n";
      	cin>>height;
      	
      	
      	surface_area(radius, height);
      	
      	cout<<" The surface area of the cylinder is\n"<<answer;            
      }
      else if(code == 'X')
      {
        cout<<" Please enter RADIUS.\n";
      	cin>>radius;
      	cout<<" Thank you! Now, please enter HEIGHT.\n";
      	cin>>height; 
      	
      	
      	cross_area(radius, height);
      	
      	cout<<" cross-sectional area of the cylinder is\n"<<answer;          
      }
      	cout<<"\n Please enter in CAPITAL letters.\n" 
	" Enter V for volume, A for surface area, X for cross-sectional area,\n"" or Q to quit. ";	
	cin>> code;
   
 }
}

void volume(float&,float&)
{
 	const float PI=3.14159;
	float radius;
	float height;
	float answer;
 	answer=height * (PI*(radius*radius)) ;
 	 
}

void surface_area(float&,float&)
{
	const float PI=3.14159;
	float radius;
	float height;
	float answer;
	answer=((2 * (PI * (radius * radius))) + ((2 * PI * radius) * height));
}

void cross_area(float&,float&)
{
	const float PI=3.14159;
	float radius;
	float height;
	float answer;
	answer=PI * (radius*radius);
}




  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Leaving the parameters unnamed leaves you unable to
//actually use the parameters in the body of the function.
void volume(float&,float&)
{
 	const float PI=3.14159;
	//This merely declared two floats unrelated to the
	//parameters. How could the compiler know that
	//these two words refer to the parameters?
	float radius;
	float height;
	float answer;
 	answer=height * (PI*(radius*radius)) ;
 	 
}
You aren't using function parameters correctly, I suggest you read this very carefully and see the difference when passing variables. http://www.cplusplus.com/doc/tutorial/functions/

You are passing the radius/height into a function so it should look something like void volume(float& radius,float& height)

You shouldn't redefine them inside the function.
Changed some things.. and still my calculations still get this random number for every calculation. Ive read the tutorial, my book, and changed my parameters like so:


#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void get_sentinel (char code);
void volume(float&, float&);
void surface_area (float&, float&);
void cross_area (float&, float&);


int main()
{

	char code;
cout<<" WELCOME! Please enter in CAPITAL letters.\n" 
	" Enter V for volume, A for surface area, X for cross-sectional area,\n"" or Q to quit.\n";
	cin>>code;	
get_sentinel (code);




return 0;
	
}
void get_sentinel(char code)
{
	
	float radius;
	float height;
	float answer;
	while (code!= 'Q')                         
 {
                                           
      
      if(code == 'V')                         
      {
      	
		cout<<" Please enter RADIUS and HEIGHT of the cylinder.\n"<<"*NOTE* Please enter in that order. (radius,height)"<<endl;
      	cin>>radius>>height;
      	
      	volume(radius, height);
      	 
    
      	cout<<" The volume of the cylinder is\n"<<answer;
    
    
                        
      }
      else if( code == 'A')
      {
        cout<<" Please enter RADIUS and HEIGHT of the cylinder.\n"<<"*NOTE* Please enter in that order. (radius,height)"<<endl;
      	cin>>radius>>height;
      	
      	
      	surface_area(radius, height);
      	
      	cout<<" The surface area of the cylinder is\n"<<answer;            
      }
      else if(code == 'X')
      {
      	cout<<" Please enter RADIUS and HEIGHT of the cylinder.\n"<<"*NOTE* Please enter in that order. (radius,height)"<<endl;
      	cin>>radius>>height;
      	
      	
      	cross_area(radius, height);
      	
      	cout<<" cross-sectional area of the cylinder is\n"<<answer;          
      }
      	cout<<"\n Please enter in CAPITAL letters.\n" 
	" Enter V for volume, A for surface area, X for cross-sectional area,\n"" or Q to quit. ";	
	cin>> code;
    
 }
}

void volume(float&r,float&h)
{
 	const float PI=3.14159;
	float answer;
 	answer=h * (PI*(r*r)) ;
 	 
}

void surface_area(float&r,float&h)
{
	const float PI=3.14159;
	float answer;
	answer=((2 * (PI * (r * r))) + ((2 * PI * r) * h));
}

void cross_area(float&r,float&h)
{
	const float PI=3.14159;
	float answer;
	answer=PI * (r*r);
}




Thank you for all the help!!
1
2
3
4
5
6
7
void volume(float&r,float&h)
{
 	const float PI=3.14159;
	float answer;
 	answer=h * (PI*(r*r)) ;
 	 
}
You're not doing anything with the value stored in 'answer'. Two local variables in two different functions are not connected in any way by merely having the same name.
You need to return the value in order to be able to use it in the caller, and the caller needs to store the returned value somewhere.
YEEESS! works perfectly thanks guys ! It all makes sense now!

#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void get_sentinel (char code);
void volume(float radius, float height,float& answer);
void surface_area (float radius, float height, float& answer);
void cross_area (float radius, float height, float& answer);


int main()
{
char code;
	cout<<" WELCOME! Please enter in CAPITAL letters.\n" 
	" Enter V for volume, A for surface area, X for cross-sectional area,\n"" or Q to quit.\n";
	cin>>code;	
get_sentinel (code);

return 0;
	
}
void get_sentinel(char code)
{
	
float radius;
float height;
float answer;
	
	while (code!= 'Q')                         
 {
                                           
      
      if(code == 'V')                         
      {
      	
		cout<<" Please enter RADIUS and HEIGHT of the cylinder.\n"<<"*NOTE* Please enter in that order. (radius,height)"<<endl;
      	cin>>radius>>height;
      	
      	volume(radius, height, answer);
      	 
      	cout<<" The VOLUME of the cylinder is\n"<<answer;                 
      }
      else if( code == 'A')
      {
        cout<<" Please enter RADIUS and HEIGHT of the cylinder.\n"<<"*NOTE* Please enter in that order. (radius,height)"<<endl;
      	cin>>radius>>height;
      	
      	surface_area(radius, height, answer);
      	
      	cout<<" The SURFACE AREA of the cylinder is\n"<<answer;            
      }
      else if(code == 'X')
      {
      	cout<<" Please enter RADIUS and HEIGHT of the cylinder.\n"<<"*NOTE* Please enter in that order. (radius,height)"<<endl;
      	cin>>radius>>height;
      	
      	cross_area(radius, height, answer);
      	
      	cout<<" The CROSS-SECTIONAL AREA of the cylinder is\n"<<answer;          
      }
      	cout<<"\n Please enter in CAPITAL letters.\n" 
	" Enter V for volume, A for surface area, X for cross-sectional area,\n"" or Q to quit. "<<endl;	
	cin>> code;
    
 }
}

void volume(float radius, float height,float& answer)
{
 	const float PI=3.14159;

 	answer=height * (PI*(radius*radius)) ;
 	 
}

void surface_area (float radius, float height, float& answer)
{
	const float PI=3.14159;

	answer=((2 * (PI * (radius * radius))) + ((2 * PI * radius) * height));
}

void cross_area (float radius, float height, float& answer)
{
	const float PI=3.14159;

	answer=PI * (radius*radius);
}



helios and James2250 THANK YOU
Last edited on
Topic archived. No new replies allowed.