Calculating cylinder Volume

Hi all newbie here.
I need some help. Can anyone tell me why the following program only gives me a round number for the volume when it is below 1 SQ ft but a decimal when it is above 1 SQ ft? (Ex 25 vs 1.25). This is a school assignment, to write a function and pass the value to main. I know I have made some errors but hey, I've only been coding c++ for a few weeks.

P.S. I have to turn this in tomorrow so any help you can give would be appreciated. Oh and also, please provide explanations that a newbie can understand...lol....

Code starts here:

#include <iostream>
#include <math.h>
using namespace std;

int CylVol(double, double);//initialize function

//-------------Function CylVol----------------
int CylVol(double Length, double Radius)
{
//Declare Variables
int vol;
const double pi = 3.1416;

vol = pi * pow(Radius, 2) * Length;// calculate the volume using
// V = 3.1416 * R^2 * L

return vol;//Calculated Volume
}//end CylVol


int main()
{
//Declare Variables
double Volume;
double Length;
double Radius;
double Multiplier = 144;


cout << endl;//print blank line
cout << "Calculate Cylinder Volume by\n"
<< "entering the Length and Radius.\n"
<< "*****NOTE*****\n"
<< "Dimensions MUST be entered in INCHES." << endl;
cout << endl;
cout << "Enter Length of Cylinder in INCHES: " << endl;
cin >> Length;
cout << "Enter Radius of Cylinder in INCHES: " << endl;
cin >> Radius;
cout << endl;

Volume = CylVol(Length, Radius);//execute function

if (Volume > Multiplier)
{
Volume = Volume/Multiplier;
cout << "The Volume is " << Volume << " Square Feet" << endl;
}
else
cout << "The Volume is of the Cylinder is: " << Volume << " Square Inches" << endl;//print calculated volume to the screen

return 0;

system ("pause");
}
int CylVol(

CylVol is returning an int. ints can only contain integers (ie, no decimal places).

Therefore your calculated volume is being truncated. For example, if you have a volume of 23.443, this will get truncated to 23, which is what the function will return.

To fix, you should change CylVol to return a double instead of an int. Also, you'll need to change the 'vol' variable in CylVol so it is also a double.
Awesome thanks! I knew it had to be something easy. I didn't realize that the "type" controlled what kinda info the function returned. Guess I should have. Thanks for the help, you guys will probably get tired of my questions. ;)
The units of volume should be cubic somethings, not square somethings.

To convert from cubic inches to cubic feet, the conversion factor is 12^3 or 1728, not 144.
Topic archived. No new replies allowed.