Link to home
Start Free TrialLog in
Avatar of cuong5985
cuong5985

asked on

Convert pounds and ounces to kilograms and grams.

Write a program that reads in a weight in pounds and ounces and putputs the equivalent weight in kilograms and grams.  Use at least three functions: one for input, one or more for calculating, and one for output.  Inlcude a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. There are 2.2046 pounds in a kilogram, 1000 grams in a kilogram, and 16 ounces in a pound.


#include <iostream>
using namespace std;

const double poundsPerKilogram = 2.2046;
const int ouncesPerPound = 16;
const int gramsPerKilogram = 1000;

void getInput(double& pounds, double& ounces);
// Precondition: User is ready to enter values correctly.
// Postcondition: The values of pounds and ounces have been set to the weight
// of the object.

void convertToGrams(double poundss, double ounces, double& kilograms, double& grams);
// Precondition: User is ready to enter values correctly.
// Postcondition: The values of kilograms and grams have been set to

void giveOutput(double kilograms, double grams);
// Precondition: The weight of the object is stored in kilograms and grams.
// Postcondition: The values of kilograms and grams have been written to the screen.

int main()
{
      double pounds, ounces, kilograms, grams;
      char ans;

      do
      {

            getInput(pounds, ounces);
            convertToGrams(kilograms, grams, pounds, ounces);
            giveOutput(kilograms, grams);

      
            cout << "Do you want to do another calculation?\n"
                 << "Press y for yes, n for no,\n"
                 << "and then press return: ";
            cin >> ans;
            cout << endl;

      } while (ans == 'y' || ans == 'Y');

      return 0;
}


void getInput(double& pounds, double& ounces)
{
      cout << "Enter the weight of the object in pounds and ounces\n";
      cout << "pounds: ";
      cin >> pounds;
      cout << "ounces: ";
      cin >>ounces;
}

void convertToGrams(double pounds, double ounces, double& kilograms, double& grams)
{
   return (pounds/2.2046)


void giveOutput(double kilograms, double grams)
{
      cout.setf(ios::fixed);
      cout.precision(0);
      cout << "The object weighs " << kilograms << " kilograms and " << grams << " grams\n";
}

    I cant continue with the code in function   void convertToGrams, can someone help????
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cuong5985
cuong5985

ASKER

{
     double kg;
     double total kg = return pounds / 2.2046;
     kilograms = floor(kg);   // extract just entire portion of kg
     grams = (kg - kilograms)*1000;    // the substraction will be remaining grams
}

when i put this into my program it shown that Parse Error before =, what does that mean???
There is a typo in my code:
double total kg = return pounds / 2.2046;
must be:
double kg = pounds / 2.2046;
But with your code, it doesnt give out the output.  
No, it doesn't prints the output, just return kilograms and grams in passed variables.
You have to make a function for that.
can you please post the full length vesrion with output for this program please?