totalOunces is an integer. This means it can only store whole numbers.
kgs * 2.2f * 16 + .5f is a floating point number. This means it can store decimal numbers.
You are trying to store a decimal number in an integer; accordingly, you will lose some information there. If you don't want to lose that information, make totalOunces a type that can hold a decimal number.
I assume you're getting the warning at line 35, and this is because you're using two different types. kgs was defined as a float, a decimal if you will, and the compiler looks at every float as it possibly having a decimal. Well if you change that decimal to a whole number, int, you can lose all of the data after the decimal. It's a standard warning.
Like moschops said, it's just a warning and your code is fine, but to force remove the warning, you can do: totalOunces = static_cast<int>(kgs * 2.2f * 16 + .5f);
What casting does is tell the compiler that you know you want to explicitly change the type from whatever to int, in this case from float to int.
Edit: Sorry, I copied the wrong line. You WILL lose every place after the decimal though. for example, if total ounces whould have been 50.87329, you will only get 50, it will not round either.
If you want to store decimal values (1.1, 2.25, 9.3850935) then you will need to store them in float or double (double is 64-bit and will store ~15 significant digits, float is 32-bit and will store ~7 significant digits).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int main()
{
double pi = 3.141592653589793;
float f = pi;
int i = f;
std::cout.precision(15);
std::cout << "pi = " << std::fixed << pi << std::endl; // will display pi = 3.141592653589793
std::cout.precision(7);
std::cout << "pi = " << std::fixed << f << std::endl; // will display pi = 3.1415927
std::cout << "pi = " << std::fixed << i << std::endl; // will display pi = 3
return 0;
}
// Preprocessor statements
#include <iostream> //for cin, cout, endl
#include <iomanip> //for setprecision
#include <conio.h> // for _getch()
usingnamespace std; //for cin, cout, endl
// named constants
constint OZ_PER_POUND = 16; //ounces in one pound
constfloat POUND_PER_KILO = 2.2; //pounds in one kilogram
/* Changed double to float, you don't use any doubles. */
int main() {
float kgs; //variable for converted kilograms
int lbs; //variable for user input pounds
int ounces; //variable for user input ounces
int totalOunces;
cout << "Enter your weight in pounds and ounces: "; //takes user input
cin >> lbs >> ounces; //assigns input pounds to lbs
cout << endl << endl; //and input ounces to ounces
// devide ounces by 16 to get pounds equivalence and add to pounds
// kilograms is total pounds divide by 2.2
kgs = ((lbs + (ounces / OZ_PER_POUND)) / POUND_PER_KILO); /* What is the purpose of this? + .11f; */
/* Added your Global conversion variables */
//outputs starting and ending values
cout << fixed << showpoint << setprecision(2) ; //format output to two decimal places
cout << lbs << " pounds and "
<< ounces << " ounces equal to "
<< kgs << " kilograms\n\n";
cout << "Enter kilo: ";
cin >> kgs;
totalOunces = static_cast<int>(kgs * POUND_PER_KILO * OZ_PER_POUND); /* What is the purpose of this? + .5f; */ //convert kilo to total ounces
lbs = (totalOunces / OZ_PER_POUND); //integer division total ounces to 16 to get pounds
ounces = (totalOunces % OZ_PER_POUND); //the remainder is ounces
cout << endl << endl
<< kgs << " kilograms equals to "
<< lbs << " pounds and "
<< ounces << " ounces\n\n";
_getch();
return 0;
}
Enter your weight in pounds and ounces: 15 20
15 pounds and 20 ounces equal to 7.27 kilograms
Enter kilo: 7.27
7.27 kilograms equals to 15 pounds and 15 ounces
Did you notice the data loss? It's a simple program, and not meant to be perfect, but I changed some things in the code, just look for the /* */ comments.
If you want to keep your values, I'd suggest changing the output variables to floats, and then setting precision of 0 so it doesn't print out anything less than a whole number.
Yes I see what you mean. . It really helps when you post it like this as I can see exactly what you did, and changed.
The .5 and .11 that was added was added to help in some form of rounding as the results were not coming up as I wanted them to, so I figured I'd add that little decimal to get the result where I wanted them to be.
If you want accurate answers, use floats for your variables, there is no data loss then and no need for a static_cast then either, and like I said, you can setprecision(0) before each variable output to make it appear as you used an int anyways.
IF you want to round 5.5 to 6, for example, you need to learn about rounding functions.