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";

Recommended Answers

All 3 Replies

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?

Look up the modf( ) function

>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';
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.