Keep getting Warning

Hi everybody, glad to join, and will be around for a long time to come hopefully.

I am debugging this code, and I keep getting this warning.

So far I have eliminated every warning that popped up, but this last one I can't seem to figure out how to fix it.

This is the warning

Warning C4244: '=' : conversion from 'float' to 'int', possible loss of data.

Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Preprocessor statements
#include <iostream> 		//for cin, cout, endl
#include <iomanip>			//for setprecision
#include <conio.h>			// for _getch()
using namespace std;		//for cin, cout, endl

// named constants
const int OZ_PER_POUND = 16;			//ounces in one pound
const double POUND_PER_KILO = 2.2;		//pounds in one kilogram
	
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 / 16) / 2.2f + .11f;	
														
	//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 = kgs * 2.2f * 16 + .5f;	//convert kilo to total ounces

	lbs=totalOunces / 16;			//integer division total ounces to 16 to get pounds
	ounces=totalOunces % 16;		//the remainder is ounces

	cout << endl << endl 
		 << kgs << " kilograms equals to "
		 << lbs << " pounds and " 
		 << ounces << " ounces\n\n";
	
	_getch();
	return 0;
}
Last edited on
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.
Looks like line 24:
kgs = (lbs + ounces / 16) / 2.2f + .11f;

Try:
kgs = (lbs + ounces / 16.0) / 2.2f + .11f;

The original warning message comes up for line 35 (Sorry forgot to post that in the original post to make it easier)

ok now I just tried

this

kgs = (lbs + ounces / 16) / 2.2f + .11f;

to this

kgs = (lbs + ounces / 16.0) / 2.2f + .11f;

now I get

line 24 saying conversion to 'double' to 'float'

and also line 35 'float' to 'int'

In C++, it is considered bad form to convert one type to another without deliberately choosing to do so.

If you are meaning to make these conversions between types, do so explicitly in the C++ style, using static_cast.

Note that these are warnings, not errors. You code will still compile and run.
You are correct Volatile it is line 35. . I just can't seem to figure out how to fix it.

Can you elaborate on what I should change so that I can see the difference and add it to my hundreds of pages of notes.


by the way, thank you guys for putting in your input, I really do appreciate it
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.
Last edited on
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;
}
WOW, thank you SO MUCH guys.

This thing had been killing me for 2 days straight now.

Again thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Preprocessor statements
#include <iostream> 		//for cin, cout, endl
#include <iomanip>			//for setprecision
#include <conio.h>			// for _getch()
using namespace std;		//for cin, cout, endl

// named constants
const int OZ_PER_POUND = 16;			//ounces in one pound
const float 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.
Last edited on
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.
Topic archived. No new replies allowed.