CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012
    Posts
    5

    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

    Code:
    // 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;
    }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Keep getting warning

    That's absolutely normal. Compiler makes implicit casting and warns you about losing fraction part.

    Make your intention be explicit, and the warning will go.
    Code:
    	totalOunces = (int)(kgs * 2.2f * 16 + .5f);	//convert kilo to total ounces
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2008
    Posts
    6

    Re: Keep getting warning

    Igor is correct. Although based on what I can see from your code, it seems "totalOunces" should be a float, not int, based on the calculation precision you are using.

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Keep getting warning

    Quote Originally Posted by chrisGz View Post
    ...based on what I can see from your code, it seems "totalOunces" should be a float, not int, based on the calculation precision you are using.
    Not really. May be if Imperial system was decimal-based...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured