Little help with functions c++?

Pages: 12
May 7, 2013 at 12:46am
I'm trying to get this program to work but something is wrong, don't know what exactly. The formula - if you're wondering - for the volume of a sphere is
4/3 * 3.1416 * radius(cubed)

are my functions wrong? it says num2 isn't initialized to something. 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
  
#include <iostream>
using namespace std;

const float PI = 3.1416;

float SphereVol(float, float);
float Cube(float);

void main()
{
   float radius,
   num2;

   cout << "Enter a radius: ";
   cin >> radius;

   cout << "Volume of a sphere with a radius " << radius << " is " << num2;


}

float SphereVol(float radius, float r)
{
   float num2;

   num2 = ( 4 / 3 * PI * r );

   return num2;
}

float Cube(float radius)
{
   float r;

   r = ( radius * radius * radius );

   return r;
}


May 7, 2013 at 12:48am
well um you never actually called your sphere volume function after getting the radius then you tried to output the volume. try putting your function on line 17 right now if you output num2 you're probably getting something like 644123123
Last edited on May 7, 2013 at 12:49am
May 7, 2013 at 12:51am
That's beause you try to cout the value of num2 while it is "dirty"(not initialized/set to anything). You must give it a value before trying to cout it.

Aceix.
Last edited on May 7, 2013 at 12:52am
May 7, 2013 at 12:52am
main() should never ever be of type void. Ever.
May 7, 2013 at 12:55am
i'm trying to call SphereVol so it displays on the output, but something is wrong. do I need to modify the function (SphereVol) or main?
May 7, 2013 at 12:59am
num2 = sphereVol(radius); not sure what the 'R' is there for
THat is how you set a variable equal to the return of a function with a paramater of something

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

These will help
Last edited on May 7, 2013 at 1:01am
May 7, 2013 at 1:20am
yep i did what it says.

First i made function prototypes,
then I calculated the cube in Cube function
then I calculated SphereVol, calling Cube() in SphereVol.


what I want to do is to call SphereVol to main so it will displays the total. but apparently there's a mistake when i call the function
May 7, 2013 at 1:23am
How do you call it?...I mean post your new code.

Aceix.
May 7, 2013 at 1:49am
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
#include <iostream>
using namespace std;
 
const float PI = 3.1416;                            // Do (1) - constants
 
float SphereVol(float, float);                           // Do (2) - function prototypes
float Cube(float);
 
void main()
{
   float r,
   radius;
    
   cout << "Enter a radius: ";
   cin >> radius;
   
   cout << "Volume of a sphere with a radius " << radius << " is " << r;
    
                                     // Do (3) - print output and call SphereVol()
}

float SphereVol(float radius, float num)
{
   float r;
   
   r = ( 4 / 3 * PI * num );                // Do (4) - calculate SphereVol, calling Cube()
   
   return r;
}     
 
float Cube(float radius)
{
   float num;
  
   num = ( radius * radius * radius );        // Do (5)- calculate the cube
   
   return num;
}


May 7, 2013 at 1:50am
you still have NOT called it in the main function. calling a function is this
1
2
3
4
int main()
{
secondFunction();
}
May 7, 2013 at 1:52am
it's still doesn't work. what could be the problem?
May 7, 2013 at 2:02am
You should remove the radius parameter from SetSphere function, since it is not being used, and do this:
r=SphereVol(radius);
in the main function, just before cout-ing r.

Aceix.
May 7, 2013 at 2:04am
You can read: http://cplusplus.com/doc/tutorial/functions/ for more information on functions.

Aceix.
May 7, 2013 at 2:15am
yep it worked now, but it says that the formula is not right even though I'm pretty sure i did what it say :|
May 7, 2013 at 2:16am
Do it this way:
1
2
3
4
5
{ float r, radius;  
 cout << "Enter a radius: ";
 cin >> radius;  
r = SphereVol(radius); //put it here!!!
cout << "Volume of a sphere with a radius " << radius << " is " << r;  // Do (3) - print output and call SphereVol() } 


You should put it after cin in a value to radius, as I've done above.


Aceix.
Last edited on May 7, 2013 at 2:20am
May 7, 2013 at 2:28am
That would be because your sphere function doesn't call the cube function when it should.
May 7, 2013 at 2:40am
@Ispil

how can I call the cube function inside the sphere function?
May 7, 2013 at 2:48am
Replace num with Cube(num).
May 7, 2013 at 2:51am
nass before any of us CAN help you need to read those two pages I linked you. That is a pretty basic thing and I already told you how a couple of times.
You are not calling the functions you need to call them by something like
function(parameter);
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on May 7, 2013 at 2:52am
May 7, 2013 at 2:53am
@gilblit

I've been reading them and I couldn't get what's wrong. I'm an absolute beginner in c++ and sometimes i run into things that I don't understand well

did that but it's still doesn't show the correct answer

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

#include <iostream>
using namespace std;


const float PI = 3.1416;                            
 
float SphereVol(float num);                        
float Cube(float radius);
 
void main()
{
   float r,
   radius;
   
   cout << "Enter a radius: ";
   cin >> radius;
   
   r = SphereVol(radius);
   cout << "Volume of a sphere with a radius " << radius << " is " << r;
    
                                    
}

float SphereVol(float num)
{
   float r;
   
   r = ( ( 4 / 3 ) * PI * Cube(num) );   
   
   return r;
}     
 
float Cube(float radius)
{
   float num;
  
   num = ( radius * radius * radius );       
   
   return num;
}




Edit:

I made some changes to my code and it's still not working (tried to call it)


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


const float PI = 3.1416;                            // Do (1) - constants
 
float SphereVol(float num);                           // Do (2) - function prototypes
float Cube(float radius);
 
void main()
{
   float r,
   radius;
   
   cout << "Enter a radius: ";
   cin >> radius;
   
   r = SphereVol(radius);
   cout << "Volume of a sphere with a radius " << radius << " is " << r;
    
                                     // Do (3) - print output and call SphereVol()
}

float SphereVol(float num)
{
   float r, radius;
   
   Cube(radius);

      
   r = ( ( 4 / 3 ) * PI * Cube(num) );    // Do (4) - calculate SphereVol, calling Cube()
   
   return r;
}     
 
float Cube(float radius)
{
   float num;
  
   num = ( radius * radius * radius );        // Do (5)- calculate the cube
   
   return num;
}


Last edited on May 7, 2013 at 3:06am
Pages: 12