User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,483 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 644 | Replies: 3
Reply
Join Date: Oct 2007
Posts: 3
Reputation: gee1288 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gee1288 gee1288 is offline Offline
Newbie Poster

conversion

  #1  
Oct 19th, 2007
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";
Last edited by gee1288 : Oct 19th, 2007 at 1:00 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: conversion

  #2  
Oct 19th, 2007
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?
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: conversion

  #3  
Oct 19th, 2007
Look up the modf( ) function
I am in mourning for my country.
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: conversion

  #4  
Oct 19th, 2007
>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:
  1. #include <iostream>
  2.  
  3. double integral_part ( double value )
  4. {
  5. return int ( value );
  6. }
  7.  
  8. double fractional_part ( double value )
  9. {
  10. return value - integral_part ( value );
  11. }
  12.  
  13. int main()
  14. {
  15. double d;
  16.  
  17. std::cout<<"Enter a floating-point value: ";
  18.  
  19. if ( std::cin>> d ) {
  20. std::cout<<"Integer half: "
  21. << integral_part ( d ) <<'\n';
  22. std::cout<<"Fractional half: "
  23. << fractional_part ( d ) <<'\n';
  24. }
  25. }
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC