hey guys im tryin to do a conversion program im trying to convert pounds to kilograms i input 10 pounds and 4 ounces and it gives me a total of 4.65 kilograms but i want to split that up so it says kilograms and grams so it would be like 4.00 kgms and 649.33 gms what i wanna know is how do you output the .65 kilograms

#include <iostream>
#include <cmath>

using namespace std;
int main()
{
cout << "Please enter number of pounds: ";
cin >> pounds;
cout << "Please enter number of ounces: ";
cin >> ounces;

tpds = ounces / 16;
tkgms = (tpds + pounds) / 2.2046;
toun = (tpds + pounds) % 2.2046;

cout << "Kilograms: " << tkgms << endl;

cout << "For " << pounds << " pounds and ";
cout << ounces << " ounces:" << endl;
cout << "Conversion is: " << floor(tkgms) << " kilograms and ";
cout << toun << " grams.\n";

Are we supposed to guess what the types are for your variables?

Was the program that much longer that you thought you would edit them out for convenience?

>what i wanna know is how do you output the .65 kilograms
If you assign the value to an int, the fractional part is truncated. Subtract that from the original value and you get just the fractional part:

#include <iostream>

double integral_part ( double value )
{
  return int ( value );
}

double fractional_part ( double value )
{
  return value - integral_part ( value );
}

int main()
{
  double d;

  std::cout<<"Enter a floating-point value: ";

  if ( std::cin>> d ) {
    std::cout<<"Integer half: "
      << integral_part ( d ) <<'\n';
    std::cout<<"Fractional half: "
      << fractional_part ( d ) <<'\n';
  }
}
This article has been dead for over six months. Start a new discussion instead.