# Volume always coming 0

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-04-2007
SVXX
Volume always coming 0
In a nutshell,this is the code of the area,lateral area and volume of a cone from a class structure -:
Code:

```else if(g[0] == 'N' || g[0] == 'n')                         {                               cout << "Enter radius of the cone - ";                               cin >> a;                               cout << "\nEnter slant height of the cone - ";                               cin >> b;                               cout << "\nEnter perpendicular height of the cone - ";                               cin >> x;                               float Area = (M_PI * a) * (a + b);                               float L_area = M_PI * a * b;                               float Volume = 1/3 * M_PI * pow(a,2) * x;                               cout << "\nThe CSA is " << L_area << endl;                               cout << "The TSA is " << Area << endl;                               cout << "The volume is " << Volume << endl;                         }```
But the volume keeps coming 0 for whatever value I put..I don't seem to find anything wrong..help please
• 10-04-2007
hk_mp5kpdw
Code:

`float Volume = 1/3 * 22/7 * pow(a,2) * x;`
Integer division... 1/3 is 0, not 0.3333 which means that the result for Volume is always 0.

Code:

```float Area = (22.0/7.0 * a) * (a + b); float L_area = 22.0/7.0 * a * b; float Volume = 1.0/3.0 * 22.0/7.0 * pow(a,2) * x;```
...and you'd be better off using double instead of float.
• 10-04-2007
robatino
Not only is 22/7 a fairly poor approximation for pi, since it's evaluated using integer arithmetic, it will be evaluated as 3, exactly. Similarly, 1/3 evaluates as 0, which is probably the source of your problem. Also, you can use a*a instead of pow(a,2), which is both shorter and faster.
• 10-04-2007
SVXX
I'm using M_PI,which is a pi constant declared in cmath. K thanks its working now.
• 10-04-2007
robatino
M_PI isn't guaranteed to exist by the standard. Something like
Code:

```#include <cmath> const double Pi = 4.*std::atan(1.);```
would be 100&#37; portable.
• 10-04-2007
Salem
> it will be evaluated as 3, exactly.
Nothing wrong with that :p

> would be 100% portable.
• 10-04-2007
SVXX
lol Salem...I have another problem now. I made a loading screen for the program with the following code
Code:

```for(int i = 0; i < 100; i ++)         {               gotoxy(35,12);               setcolor(12);               cout << "Loading the Math-O-Matic 5000";               gotoxy(38,14);               cout << i << "% done";                        Sleep(100);               setcolor(7);               clrscr();         }```
clrscr() and gotoxy() I defined on my own. Those work. The thing is,if I want to break or skip the loading screen,how would I do so?
• 10-04-2007
robatino
Quote:

Originally Posted by Salem
> would be 100% portable.

On my Linux box, GCC's <cmath> is implemented by #including <math.h>, which defines M_PI as

# define M_PI 3.14159265358979323846 /* pi */

which is the correct value to 20 places. On the other hand, a 64-bit IEEE double uses 52 bits for the mantissa, which is roughly 15 or 16 decimal digits.
Code:

```#include <iostream> #include <cmath> int main() {   const double Pi = 4.*std::atan(1.);   std::cout.precision(25);   std::cout << "Pi == " << Pi << '\n';   std::cout << "M_PI == " << M_PI << '\n'; }```
Pi == 3.141592653589793115997963
M_PI == 3.141592653589793115997963

So for a 64-bit IEEE double, the values are essentially the same to machine precision.
• 10-04-2007
SVXX
• 10-04-2007
matsp
Quote:

Originally Posted by SVXX
lol Salem...I have another problem now. I made a loading screen for the program with the following code
Code:

```for(int i = 0; i < 100; i ++)         {               gotoxy(35,12);               setcolor(12);               cout << "Loading the Math-O-Matic 5000";               gotoxy(38,14);               cout << i << "% done";                        Sleep(100);               setcolor(7);               clrscr();         }```
clrscr() and gotoxy() I defined on my own. Those work. The thing is,if I want to break or skip the loading screen,how would I do so?

Why have it in the first place - it doesn't do any "loading", and waiting ten seconds (10000 ms) just to write out 100 percentage values is pretty meaningless, don't you think? If it was ACTUALLY loading something, that's a differnet matter, but you're just pretending to load something... Pointless...

--
Mats
• 10-04-2007
SVXX
Well,as the guy who suggested it says, it "gives a pro look to the program". Lmao.
Yer really too serious about this Mat...I just added it for fun,it gives a more interactive look to the program and thats it. Who likes wadding about DOS these days? All of my pals work on Windows,and the purpose of this program is to pwn every math concept we have in our textbook. Thats why I'm making this. This is only basic ......... I'll be adding trigonometric equation solving and more later..I should make a Win32 API,yes, but unfortunately I'm not so fluent in Win32 programming. Anything else?
• 10-04-2007
matsp
Quote:

Originally Posted by SVXX
Well,as the guy who suggested it says, it "gives a pro look to the program". Lmao.
Yer really too serious about this Mat...I just added it for fun,it gives a more interactive look to the program and thats it. Who likes wadding about DOS these days? All of my pals work on Windows,and the purpose of this program is to pwn every math concept we have in our textbook. Thats why I'm making this. This is only basic ......... I'll be adding trigonometric equation solving and more later..I should make a Win32 API,yes, but unfortunately I'm not so fluent in Win32 programming. Anything else?

But you can't skip the "loading" as it stands. If you want to you could do a "if (kbhit()) break;" type thing. How you do that in a modern Windows system, I'm not entirely sure. I could find out if I needed to, but I still fail to see the point.

In my opinion, it doesn't make it look professional, it makes it look pretentious - by which I mean "it pretends to be more important than it is" - like sticking a BIG wing on a 60 bhp not-so-sporty car sort of thing. The car can't even get to a speed where that wing is needed, but it's there to make it "look fast". Pointless rubbish.

--
Mats
• 10-04-2007
anon
If you know how to read input without blocking the execution this might be possible. But it is going to look very unprofessional if your program stops loading at 34&#37; is you press a key...

You may try to reduce the sleep period and/or skip a few percentages.

To make it look more professional, you should introduce some irregularity. E.g the first 86% get loaded really fast, then the next 4% take almost an eternity and the last 10% go fast again. :)
• 10-04-2007
SVXX
And then again, you're not one of my classmates, neither do you know me.
I'll get advanced as I progress, and probably feel the same about that crap.
True, it may be pointless rubbish, but I don't really care.

Lmao anon...if that makes it look professional.
"I might be wrong" XD
• 10-04-2007
matsp
Quote:

Originally Posted by SVXX
And then again, you're not one of my classmates, neither do you know me.
I'll get advanced as I progress, and probably feel the same about that crap.
True, it may be pointless rubbish, but I don't really care.

Lmao anon...if that makes it look professional.
"I might be wrong" XD

Well, if this is for Windows, have a look at:
http://msdn2.microsoft.com/en-us/library/ms684344.aspx

--
Mats
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last