One last minor issue....

I cannot get the Volume calculation to output correctly.

(thi * pi) * (r * hei) = 1/3 * 3.14159 * 36 * 12 = 452.4

But my output shows a different 11 digit floating point every time. Never the same number twice. Area is consistently correct every time.

Help

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

const float pi = 3.14159; //constant pi
float rad; //radius
float hei; //height
float thi = (1.0 / 3.0);
float area; //area
float r; //radius squared
float h; //height squared
float vol; //volume
int s; //part of the slant height calculation (s = r squared + h squared)

void output(float radius, float height, float volume, float area) //function
{

cout << setw(8) << "Results:" << endl;
cout << setw(27) << "For a cone with a radius of " << radius << " inches and a height of " << height << " inches" << endl << endl;
cout << setw(13) << "Cone Volume: " << volume << " cubic inches " << endl;
cout << setw(19) << "Cone Surface Area: " << area << " square inches " << endl << endl;

}

int main()

{

float sla; //slant height
float vol; //volume
float are; //area
int s; //r squared + h squared
float thi; // 1.0 / 3.0

cout << "Program to calculate the volume and surface area of a cone" << endl << endl;
cout << "Enter cone radius (in inches):";
cin >> rad;
cout << endl;
cout << "Enter cone height (in inches):";
cin >> hei;
cout << endl << endl;

cout << setprecision (1);
cout << fixed;

r = pow (rad, 2); //radius squared

h = pow (hei, 2); //height squared

s = r + h; //part of slant height equation

sla = sqrt(s); //part of slant height equation

vol = (thi * pi) * (r * hei); //volume equation

are = (pi * r) + (pi * (rad * sla)); //area equation


output(rad, hei, vol, are); //function call


system("PAUSE");

}


Last edited on
1 / 3 is integral division which results in 0. 1 / 3.0 would result in a double, if I remember right (either that or a float). Make sure you don't just store it in an integer or it will be truncated back to 0.
1/3.0 is indeed a double value. 1/3.f or 1/3.0f are floats.
Note that the smaller operand gets converted to the type of the larger operand when the types of the operands of a binary operation don't match, so 1/3.0, 1.0/3, and 1.0/3.0 all mean the same thing.
Ok, I got the 1/3 issue, which I should have caught since my professor specifically said to watch out for that. But after fixing it I still get these huge numbers for the <volume> output. Just to test my math I replaced <vol = (thi * pi) * (r * hei);> with <vol = 0> and the function output <0> as the <volume>, as it should have. I am assuming this would indicate my volume formula has an issue, correct? Which I cannot find. But....since I get a different <volume> output every time I run the program, there must be something else going on right? Is it because I have more than one math function in the same user-defined void function?
You're declaring two variables called thi. One in the global scope and another in the local scope of main(). The latter obscures the former, and is also uninitialized.
So I simply changed <1.0 / 3.0> to 3.333 and it works great. I will read up on globals since I really do not know what they are or what you mean (haven't covered them yet). Your earlier comments did lead me to the <thi> issue so, thank you. I now have a great understanding of functions. Once again thank you.
Topic archived. No new replies allowed.