Converting pounds and ounces to kilograms and grams

Any advice as to even how to start this problem would be greatly appreciated.
I am not even sure that I have the correct headers for my functions and I am completely lost.
The problem is as follows:

Write a program that reads in a weight in pounds and ounces and outputs the equivalent weight in kilograms and grams. Use at least three functions: one for input, one or more for calculating, and one for output. Include a loop that lets the user repeat this computation for new input value 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 15 ounces in a pound.

I don't have much towards it... but here is all I could figure out before realizing I have no idea how to do this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>

using namespace std;

double weightInput(double pounds, double ounces)
{
    
}
double weightCalculating(double pounds, double ounces)
{
   return pounds / 2.2046 && (((ounces * 15)*2.2046)/ 1000);  
}
double weightOutput(double pounds, double ounces)
{
    
}
int main(int argc, char** argv) {

    return 0;
}

Line 12: && is the logical and operator used for boolean logic and therefore the expression will evaluate to a bool and not a double.
Read more here: http://www.cplusplus.com/doc/tutorial/operators/

I can understand what you're trying to do with the weightCalculating function. You want to pass the arguments by reference, so that any change made inside the function will act upon the original.
Read about passing by reference here: http://www.cplusplus.com/doc/tutorial/functions/
Thank you,
I'll go ahead and read that and learn more about functions.
But can you help me differentiate what I need to implement into each function?
Or maybe just the input function itself and then I'll be able to determine what then would need to go into the calculating function. Once I know how to set up my input function the hardest part will be setting up my calculation function. Once I figure that out I'm sure I will be able to determine how to put it all into the output function
I haven't had to break up my functions this way before and I'm not sure how to correctly code them
For this one all you should need is #include <iostream>

In my beginner experience I have always used void for the input function type
1
2
3
4
5
6
void weightInput(double &pounds, double &ounces){
    //output a question like "enter pounds: "
    //input pounds
    //output "enter ounces: "
    //input pounds
}

You have to use the & so that you can change the value of pounds and ounces. Without it, any changes made to those variables inside weightInput() would be ignored.

void for weightCalculating() too because you are changing variables but not returning a value
1
2
3
4
5
6
7
8
9
void weightCalculating(double &pounds, double &ounces, double &kilograms, double &grams){

    //It is easier to work with 2 variables (pounds and kilograms) than 4 (lbs, oz, kg, g)
    pounds = pounds + ounces/16; // 16 ounces in a pound

    //do conversion from pounds to kilograms here (kilograms = pound conversion)

    //do conversion from kilograms to kilograms and grams (10.5555 kg = 10kg, 555.5g)
}


still using void for weightOutput()
1
2
3
void weightOutput(double &kilograms, double &grams){
    //output the variables next to their unit
}


In the main function I would declare 4 variables:
1
2
3
4
5
6
7
8
9
10
11
12
double pounds;
double ounces;
double kilograms;
double grams;

    //loop these:
    weightInput(pounds, ounces);
    weightCalculating(pounds, ounces, kilograms, grams);
    weightOutput(kilograms, grams);

    return 0;
}


sample input
1
2
Enter pounds: 10
Enter ounces: 5

sample output
4kg, 677.677g

Hope this helps. I was trying to help without completely writing the program for you but I may have overdone it.
Last edited on
Thank you so much.
I have another problem similar to this but it deals with length conversion.
So I will just glance at what you said and try to figure it out on my own.
I appreciate it.
I do actually want to learn, and need to know how if I want to have a job as a programmer.
So this will help me get started on this problem. Thank you. I'll work out this program and see if I can get it all working correctly.
I have used google to verify the conversion... and it's wrong. I don't know why.
I've looked all over for proper code to convert my pounds and ounces into kilograms and grams...
To no avail.
Here is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <iostream>

using namespace std; 

void weightInput(double& pounds, double& ounces)
{
    cout << "Enter pounds: ";
    cin >> pounds;
    cout << "Enter ounces: ";
    cin >> ounces;
}
void weightCalculating(double& pounds, double& ounces, double& kilograms, 
        double& grams)
{
    ounces = pounds / 16;
    kilograms = pounds * 2.2406;
    grams = kilograms / 1000;
    
}
void weightOutput(double& kilograms, double& grams)
{
    cout << kilograms << " kilograms and " << grams <<
            " grams.\n";
}
int main(int argc, char** argv) {

    double pounds;
    double ounces;
    double kilograms;
    double grams;
    char userInput;
    do{
    weightInput(pounds, ounces);
    weightCalculating(pounds, ounces, kilograms, grams);
    weightOutput(kilograms, grams);
    cout << "Would you like to do another conversion?\n Y for yes or any other"
            " key to quit.";
    cin >> userInput;
    }while(userInput == 'Y');
    return 0;
}

I am guessing it's something wrong in my weightCalculating function.
I am not sure what is wrong, any advice?
first: Line 16 ounces = pounds / 16; // bad
To combine ounces and pounds into 1 variable, you want to use pounds. To convert both to pounds, 10 pounds and 5 ounces would be (10 + 5/16) pounds. In your code that would be
pounds = pounds + ounces/16; // good


second: Line 17 kilograms = pounds * 2.2406; // bad
1 kg = 2.2046 lbs (you wrote 2.2406 instead of 2.2046, but there's more)
which means
kilograms = pounds / 2.2046; // good

you can check by plugging in 2.2046 pounds (the answer should be 1 since 1 kg = 2.2046lbs)

kilograms = pounds / 2.2046
kilograms = (2.2046) / 2.2046
kilograms = 1


third: Line 18 grams = kilograms / 1000; // bad
1kg = 1000g which means grams = kilograms * 1000

All of these seem backwards at first.

When calculating grams, you have to take away kilograms. Otherwise you end up with an output like
4.67768 kilograms and 4677.68 grams

to fix that use typecasting
1
2
3
4
  grams = 1000 * (kilograms - int(kilograms));
//grams = 1000 * (4.67768 - 4)
  kilograms = int(kilograms);
//kilograms = 4 

telling the compiler to treat kilograms like an int[eger] makes it drop the decimal. Do you see why this works?

and for the sake of formatting add and extra \n to the output on Line 23-24
cout << kilograms << " kilograms and " << grams <<" grams.\n\n";
Last edited on
Thank you so much for the help once again. I can't thank you enough.
There are two things that I don't understand thought.
1
2
3
4
  grams = 1000 * (kilograms - int(kilograms));
//grams = 1000 * (4.67768 - 4)
  kilograms = int(kilograms);
//kilograms = 4  

I've heard of static casting before, so I'm guessing typecasting is similar.
I can kind of see what is being done here. If there is 4 kilograms we are subtracting 4 from kilograms to leave the 0.67768 number and then we are multiplying that by 1000 to get the grams. I also didn't know you can typecast like that. I figured with static casting you'd have to do static_cast<double>kilograms - int (kilograms); Or something similar to that.
So I learned how to typecast. Thank you :]
When I enter 10 pounds and 5 ounces, I output 4 kg, 602.562 grams. Which I looked on google that 10 pounds is about 4 kg so that is correct... But am not sure about the grams calculation.
Second,
pounds = pounds + ounces / 16;
Does this cover all I need to calculate for pounds and ounces in my weightCalculating function? Or do I need to have a calculation for ounces as well?

Again, I appreciate your help. After I understand this problem, I have a very similar problem that has to deal with length instead of weight, so I'm looking forward to using this as a reference to figuring out how to do my length program.
You got 602.562 grams because you used kilograms = pounds / 2.2406; instead of kilograms = pounds * 2.2046;
(2.2406 vs 2.2046) Use 2.2046

And yes after you combine pounds and ounces pounds = pounds + ounces / 16; you don't need the ounces variable anymore.
Topic archived. No new replies allowed.