1 pound outputs as 16 ounces


Enter kilograms:0.453597
Enter grams:0
0.453597 kilograms, 0 grams is equal to
0 pounds, 16 ounces

This code reads in kilograms and grams then outputs the equivalent in pounds and ounces.
When I read in 0.453597 kilograms, 0 grams
the output should be 1 pound, 0 ounces.
Instead I get 0 pounds, 16 ounces.
I have
1
2
3
4
5
	if (ounces >= 16) 
	{                    
		ounces -= 16;    
		pounds += 1;     
	}

But that doesn't have any effect on the output

What can I do to have my program output 1 pound, 0 ounces
instead of 0 pounds, 16 ounces?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>

const double POUNDS_PER_KILOGRAM = 2.2046;

void read_in_kilograms_and_grams(double& kilograms, double& grams);
double calc_total_kilograms(double kilograms, double grams);
double translate_kilograms_to_pounds(double total_kilograms);
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces);
void output_pounds_ounces(double kilograms, double grams, double pounds, double ounces);

int main()
{
	using namespace std;
	double pounds, ounces, kilograms, grams, total_pounds, total_kilograms;
	char ans;

	do
	{
		read_in_kilograms_and_grams(kilograms, grams);
		total_kilograms = calc_total_kilograms(kilograms, grams);
		total_pounds = translate_kilograms_to_pounds(total_kilograms);
		calc_pounds_ounces(total_pounds, pounds, ounces);
		output_pounds_ounces(kilograms, grams, pounds, ounces);
		cout << "Would you like to run the program again? Y/N :";
		cin >> ans;
		cout << endl;
	} while ((ans == 'Y') || (ans == 'y'));
	return 0;
}
void read_in_kilograms_and_grams(double& kilograms, double& grams)
{
	using namespace std;
	cout << "Enter kilograms:";
	cin >> kilograms;
	cout << "Enter grams:";
	cin >> grams;
}
double calc_total_kilograms(double kilograms, double grams)
{
	using namespace std;
	double gramKilograms;
	gramKilograms = grams / 1000;
	return kilograms + gramKilograms;
}
double translate_kilograms_to_pounds(double total_kilograms)
{
	using namespace std;
	return total_kilograms * POUNDS_PER_KILOGRAM;
}
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
	using namespace std;
	pounds = static_cast<int>(total_pounds) / 1;
	ounces = (total_pounds - pounds) * 16;
	//////////////////////////////////////////
	if (ounces >= 16)    // This is the code I used to try to get the program
	{                    // to output 1 pound, 0 ounces
		ounces -= 16;    //
		pounds += 1;     //
	}                    //
}
void output_pounds_ounces(double kilograms, double grams, double pounds, double ounces)
{
	using namespace std;
		cout << kilograms << " kilograms, "
		<< grams << " grams is equal to\n"
		<< pounds << " pounds, "
		<< ounces << " ounces\n"
		<< endl << endl;
}
Last edited on
First calculate ounces. To get the pound/ounces you use integer division for pouns and modulo for the remaining ounces:
1
2
pounds = ounces / 16; // integer division
ounces %= 16; // modulo 
I tried changing the function to this:

1
2
3
4
5
6
7
8
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
	using namespace std;
	double total_ounces;
	total_ounces = total_pounds * 16;
	pounds = total_ounces / 16;
	ounces = total_ounces % 16;
}

But I can't use the modulo operand on type double. So I changed total_ounces to type int and added some variable tracing:
1
2
3
4
5
6
7
8
9
10
11
12
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
	using namespace std;
	cout << "total_pounds = " << total_pounds << endl;
	int total_ounces;
	total_ounces = total_pounds * 16;
	cout << "total_ounces = " << total_ounces << endl;
	pounds = total_ounces / 16;
	ounces = total_ounces % 16;
	cout << "pounds = " << pounds << endl
		<< "ounces = " << ounces << endl;
}

this was my output:

total_pounds = 1
total_ounces = 15
pounds = 0
ounces = 15
0.453597 kilograms, 0 grams is equal to
0 pounds, 15 ounces
Last edited on
You can actually use modulo with double:

http://www.cplusplus.com/reference/cmath/fmod/?kw=fmod

ounces = fmod(total_ounces, 16);

pounds however should be still non floating point. What type exactly (int / int64) depends on the how big the number could be.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
	double total_ounces = total_pounds * 16;

	pounds =(int) total_ounces / 16;
	ounces = total_ounces - ( 16 * pounds);

return;
}


@kemort
line 6 is exactly what fmod(...) does.
closed account (48T7M4Gy)
I know

[ Actually that's not quite right because my 2 lines solves the OP's problem while fmod doesn't. ]
Last edited on
closed account (E0p9LyTq)
@coder777

I believe modf would work better, breaking a floating point into integral and fractional parts.

http://www.cplusplus.com/reference/cmath/modf/?kw=modf



closed account (48T7M4Gy)
The problem is modf only produces pounds and fractional pounds in the single step referred to so we are still left with determining the remaining ounces which is the second step.

So any difference is trivial, and the answer will (necessarily) be the same.
I tried both solutions
1
2
3
4
5
6
7
8
9
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
	using namespace std;
	double total_ounces;
	total_ounces = total_pounds * 16;
	pounds = total_ounces / 16;
	ounces = fmod(total_ounces, 16);
	
}

The above code produced this output:

0.453597 kilograms, 0 grams is equal to
1 pounds, 16 ounces


1
2
3
4
5
6
7
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
using namespace std;
double total_ounces = total_pounds * 16;
pounds = (int)total_ounces / 16;
ounces = total_ounces - (16 * pounds);
}

The above code produced this output:

0.453597 kilograms, 0 grams is equal to
0 pounds, 16 ounces

I'm beginning to think the problem might be my compiler. I'm using Microsoft Visual Studio Express 2013 for Windows Desktop. I downloaded it for free. Has anyone heard of this compiler being problematic? If so, is there a better compiler I should look for? I still can't figure out why I'm not getting the desired output below:

0.453597 kilograms, 0 grams is equal to
1 pounds, 0 ounces
Last edited on
closed account (48T7M4Gy)
pounds = (int)total_ounces / 16;

to be safer pounds = (int)(total_ounces / 16);

I tried it out in the shell and the reason is you are getting rounding errors so even though 0lb 16oz is correct, rounding is playing a part. Because of the way I constructed the calculation in my line 6, the answer will always balance. The fmod approach as written doesn't, but it is not because the underlying fmod function is at fault. i.e. you need to construct the calculation in such a way that it too doesn't build in 'truncation' problems.

Since you've decided to use double precision your conversions need to be 'very' accurate, re-check physical constant tables and get more significant figures.
kemort, after reading your last post I decided to change my constant declaration to include another digit of accuracy, as shown below.
 
const double POUNDS_PER_KILOGRAM = 2.20462;

With this change my output is

0.453597 kilograms, 0 grams is equal to
1 pounds, 0.00014429 ounces

Thank you, everyone who helped!
closed account (48T7M4Gy)
Looks good, if you like try also the following from https://en.wikipedia.org/wiki/Pound_%28mass%29:

"The United States and countries of the Commonwealth of Nations agreed upon common definitions for the pound and the yard. Since 1 July 1959, the international avoirdupois pound has been defined as exactly 0.45359237 kg."
Last edited on
Topic archived. No new replies allowed.