Surface Area and Volume project

Hello again, I am wondering on how to stop these warnings from coming up while trying to do another project. I get:

warning C4305: 'initializing' : truncation from 'double' to 'const float'
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

here is what I have:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{


float radius, height;
float SA_answer, V_answer;

const float PI = 3.14;


cout<< "Please enter a positive whole number that will be used as the radius. \n";
cin>>radius;

cout<< "Please enter a positive whole number that will be used as the height. \n";
cin>>height;

cout<< "The program will now compute the surface area and volume: \n";


SA_answer= 2.0 *PI * pow (radius, 2) + 2.0 * PI * radius * height;
cout<< "The surface area is: " << SA_answer << "squared ft" << endl;


V_answer= PI * pow (radius, 2) * height;
cout<< "The volume is: " << V_answer << "cubed ft" << endl;


system ("pause");

return 0;
}


BTW this is for a right circular cylinder
Last edited on
The variable PI is already defined in one of your included header files. It's warning you that you're re-defining it as something else in this program.
Oh so I wouldn't need to define PI as a const float at all then? Just put it in the equation?

nvm Umm..... I've tried having PI as a const, const float, and const double but still can't figure out exactly what the program wants
Last edited on
Comment that line out of your program and see if it still works. :P
It says that PI is now an undeclared identifier.
His warnings are warning him that converting double to float will potentionally lose information. This is because doubles have a greater accuracy than floats, so converting double to float will lose accuracy.

Number literals like 2.0 or 4.523 are by default doubles. To make float literals, simply append "f", like 3.45f.

Also, I remember reading that the PI macro in header files are implementation dependent, as in the names aren't consistent from compiler to compiler. Making your own PI onstant would be better.
Sweet!! Thanks Daleth, it works now :D
I don't remember in C++ how to access the predefined version of PI exactly. I could do it for you in C, but that's not the point, is it? As long as you get the right answer from the program, you should be able to safely ignore those two warnings, but only so long as you understand that somewhere there's an identifier with the same name declared as a double, which just means that it's more precise. Since you're only using about 1/3 of the precision of a float, you shouldn't be having too much of an issue (which is why I say that you can ignore them). However, if you want to do anything on a grander scale than just turning in a homework assignment, you'll need to look up how PI is defined in the C++ libraries.
Okay, I'll try to sharpen my skills more by doing that.
Thanks for the tip ciphermagi
Topic archived. No new replies allowed.