### Cylinder Weight Function

Hey guys,
I've been working on this for awhile now and I can't seem to get it right. I'm pretty sure I've got the formulas right to find the weight of the drum, but it's not calculating it right. I don't know what it is about functions and modules that get me, but they really stump me and I mean all the time. Here is the current code that I have. I guarantee it's going to be a duh moment when someone answers this. I can't quite see what I'm missing from it.

#include <iostream>
#include <cmath>
using namespace std;

double cylinderWaterWeight(double height, double diameter);

int main()
{

double height;
double diameter;

cout << "Please enter the height using inches: ";
cin >> height;
cout << "Please enter the diameter using inches: ";
cin >> diameter;

cout << "\n";
cout << "|-----------------|" << endl;
cout << "| Calculating |" << "\n";
cout << "|-----------------|" << endl;
cout << "\n";

double cylinderWaterWeight;

cout << "The weight of the cylinder is: " << cylinderWaterWeight << endl;

system("pause");
return 0;
}

double cylinderWaterWeight(double height, double diameter)
{
double volume = height * M_PI * diameter * diameter / 4.0;

const double density = 1.936;
double waterWeight = volume * density;
return waterWeight;

}
Last edited on
Where do you ever call your function?
I honestly thought it was in the line where it says "double cylinderWaterWeight"

But I might be trying to calling it wrong. I may not have the correct syntax.
You declared a variable called "cylinderWaterWeight" of type double.

To call a function;
`functionName( parameters );`
Thanks loads for pointing that one out haha! I've been trying all types of things and I forgot to go back and change that.

The weird thing though is that it still calculated something and came up with the number: 2.24362e-306

I've been extremely tired, so I'm missing a lot of things I don't normally miss. When I ask for the height and diameter, I type in 33.5 and 22.5 for the height and diameter.

I keep thinking that I've got something either backwards or my formula within the function isn't right. By the way, to the people who are helping me, I truly do appreciate it. This is the first time I've come to a C++ forum for help. Incredibly, it's the first time I've come to a programming forum for help. Before this C++ class, I was taking Python, but I like C++ better. Anyway, I'm done ranting. I'll keep checking in.
 ``123`` ``````double cylinderWaterWeight; cout << "The weight of the cylinder is: " << cylinderWaterWeight << endl;``````

As you haven't initialized the variable "cylinderWaterWeight", it's filled with a 'garbage' value.

Visual Studio used to let me cout un-initialized variables, but not now. But, what you're seeing, is the 'garbage' value.

If you do the following, you'll see another output:
 ``123`` ``````double cylinderWaterWeight = 0.0; cout << "The weight of the cylinder is: " << cylinderWaterWeight << endl;``````
Thanks Lynx! I see what you mean there. I was having a brain fart moment and now I've got it! I truly do appreciate you checking in and helping me out with this! It's been driving me nuts all day lmao This is a rather simple program to write and it baffles me to think that I couldn't understand it.

Below is the code that I fixed and I'm sticking with. This is for a test, so I can't have too much help, but having a fresh pair of eyes on the code really helps me find the problems that I made.

-----------------------------------------------------------------------------------------------

#include <iostream>
#include <cmath>
using namespace std;

double cylinderWaterWeight(double,double );

int main()
{
cout << "Please enter the height using inches: ";
double height;
cin >> height;
cout << "Please enter the diameter using inches: ";
double diameter;
cin >> diameter;

cout << "\n";
cout << "|-----------------|" << endl;
cout << "| Calculting |" << "\n";
cout << "|-----------------|" << endl;
cout << "\n";

double weight = cylinderWaterWeight(height, diameter);
cout << "The weight of the cylinder is: " << weight << endl;

system("pause");
return 0;
}

double cylinderWaterWeight(double height, double diameter)
{
// The site I obtained the conversion was from
// http://www.engineeringtoolbox.com/water-density-specific-weight-d_595.html

double volume = height * M_PI * diameter * diameter / 4.0;

const double density = 1.936;
double waterWeight = volume * density;
return waterWeight;

}
No worries! (:

When I get stuck with code etc. I come here for a fresh set of eyes too! lol. I can stare at code for hours, post on here and the problems can be solved in minutes!
Haha! I know the feeling. :D
I think your formula is wrong, it should be radius squared, not diameter.
Edit: And why are you dividing by 4?
Last edited on
Topic archived. No new replies allowed.