Pounds To Ounces


Program Listing

LbsToOzs.cpp

/****************************************************************************
*                                                                           *
*   Program:    LbsToOzs                                                    *
*                                                                           *
****************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Get a weight from the user

    cout << "Enter a weight in pounds:  " << flush;
    float DecLbs;                       // Weight in decimal pounds
    cin >> DecLbs;

//  Convert the weight to pounds and ounces

    int TotalOzs = round(16*DecLbs);    // Total ounces rounded off
    int Lbs = TotalOzs/16;              // Whole number of pounds
    int Ozs = TotalOzs % 16;            // No. of ounces left over

//  Print the results

    cout << "The weight is " << Lbs << " pounds and ";
    cout << Ozs << " ounces" << endl;
    
    return 0;
}

Sample Run #1

Enter a weight in pounds:  4.89
The weight is 4 pounds and 14 ounces

Sample Run #2

Enter a weight in pounds:  4.99999
The weight is 5 pounds and 0 ounces



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:25 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/LbsToOzs.html

Return to Coms 261 home page