Little help with functions c++?

Pages: 12
(4/3) is integer division.
use (4.0/3.0).
you don't have to create a function to get cube...there is one already made!! you can use pow( radius, 3.0), but first you have to include the cmath library!!

and why are you using float?? I would have done it with double
Hi Nass, You can simplify your code right down if you want to, and still incorporate a function for practice. Anything more than this seems a little too complicated! Cheers, Don from Down Under.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

double SphereVol(float r)
{ return 4*3.1416*r*r*r/3; } //Works best when division done at end.

int main()
   {
   float r;
   cout <<"Enter a radius in inches: ";
   cin >> r;
   cout <<"\nVolume of a sphere with a radius of "<<r<<" inches is "<< SphereVol(r)<<" cubic inches.";

   cin.get ();
   cin.get ();
   return 0;
   }

Topic archived. No new replies allowed.
Pages: 12