EnglishWeight conversion

Dear all
I've been looking for further help with my program while I can run it but the result came incorrectly.

here's the introduction to my program
Write a class to implement English weights. The type name of the class must be EnglishWeight.
Declare the class in a header file called EnglishWeight.h and implement the class in separate file called EnglishWeight.cpp.
Include appropriate constructors.
Include new versions of the stream insertion and stream extraction operators appropriate to this class.
Provide the following operators:

EnglishWeight + EnglishWeight

EnglishWeight - EnglishWeight

EnglishWeight / EnglishWeight (this one takes some thought!)

EnglishWeight * number

number * EnglishWeight

Englishweight / number
Write a small test program in a file called EW_Driver.cpp to allow a user to enter two weights (in pounds and ounces) and a number. The program will output the results of the six operations described above.

here's my cppfile
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include "EnglishWeight.h"
#include <string>
#include <cmath>

using namespace std;

//No argument constructor
EnglishWeight::EnglishWeight()
{
    m_pounds = 0;
    m_ounces = 0.0;
} 

//Conversion constructor
EnglishWeight::EnglishWeight(istream& str)
{
	char pounds_array[10], ounces_array[2];

	//Parse lb entry into an array
	str.get(pounds_array, 10, 'l');
	m_pounds = atoi(pounds_array);

	str.ignore();
        str.ignore();
        str.ignore();
    
	str.get(ounces_array, 2, 'o');
	m_ounces = atoi(ounces_array);
	
}
	
//Define operator + that adds English Weights 
EnglishWeight operator + (EnglishWeight lhs, EnglishWeight rhs)
{
	int    sum_pounds;
	double sum_ounces;
	
	EnglishWeight sum_weights;

	sum_pounds = lhs.m_pounds + rhs.m_pounds;
	sum_ounces = lhs.m_ounces + rhs.m_ounces;

	sum_weights.m_pounds = sum_pounds;
	sum_weights.m_ounces = sum_ounces; 

	sum_weights = convertOunces(sum_weights);
	
	return sum_weights;
}

// Define operator - that subtracts EnglishWeights 
EnglishWeight operator - (EnglishWeight lhs, EnglishWeight rhs)
{
	int diff_pounds;
	double diff_ounces;

	EnglishWeight diff_weights;

	diff_pounds = lhs.m_pounds - rhs.m_pounds;
	diff_ounces = lhs.m_ounces - rhs.m_ounces;

	if (diff_ounces < 0)
	{
		diff_pounds--;
		diff_ounces += 16;
	}

	diff_weights.m_pounds = diff_pounds;
	diff_weights.m_ounces = diff_ounces;

	return diff_weights;
}

//Define operator / that operates on 2 EnglishWeights
double operator / (EnglishWeight lhs, EnglishWeight rhs)
{
    int quotient_pounds;
    quotient_pounds= 0;
	double oz_1, oz_2;

	double quotient;

	oz_1 = (lhs.m_pounds * 16) + lhs.m_ounces;
	oz_2 = rhs.m_pounds * 16 + rhs.m_ounces;

	quotient = oz_1 / oz_2;

	return quotient;
}

//Define operator * that multiplies an EnglishWeight and int num
EnglishWeight operator * (EnglishWeight lhs, int num)
{
	int product_pounds;
	double product_ounces;

	EnglishWeight product; 

	product_pounds = lhs.m_pounds * num; 
	product_ounces = lhs.m_ounces * num;

	product.m_pounds = product_pounds;
	product.m_ounces = product_ounces;

	product = convertOunces(product); 

	return product;
}

//Define operator * for number * EnglishWeight
EnglishWeight operator * (int num, EnglishWeight rhs)
{
	int product_pounds;
	double product_ounces;

	EnglishWeight product;


	product_pounds =  num * rhs.m_pounds; 
	product_ounces = num * rhs.m_ounces;

	product.m_pounds = product_pounds;
	product.m_ounces = product_ounces;

	product = convertOunces(product);

	return product;
}

//Define operator / that divides an EnglishWeight by an int 
EnglishWeight operator / (EnglishWeight lhs, int num)
{
	double temp_oz;
	EnglishWeight quotient;

	temp_oz = lhs.m_ounces + (lhs.m_pounds * 16);
	quotient.m_ounces = temp_oz / num;

	quotient = convertOunces(quotient);

	return quotient;
}

// Define stream insertion operator for EnglishWeights.
ostream& operator << (ostream& str, EnglishWeight ewt)
{
	str << ewt.m_pounds << "lb " << ewt.m_ounces << "oz";

	return str;
}

// Define stream extraction operator for EnglishWeights.
istream& operator >> (istream& str, EnglishWeight& ewt)
{
	EnglishWeight _weight(str);
	ewt = _weight; 
	
	return str;
}

//Function to keep ounces under 16
EnglishWeight convertOunces(EnglishWeight ewt)
{
	if (ewt.m_ounces >= 16)
	{
	ewt.m_ounces -= 16;
        ewt.m_pounds++;
	}
	return ewt;
}

here's the header file
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>
#include <string>
#include <cmath>

using namespace std;

class EnglishWeight
{
public:
	//No argument constructor
	EnglishWeight();

	//Conversion constructor
	EnglishWeight(istream&);
    
        EnglishWeight(int lb, double oz);
    
	friend nglishWeight operator + (EnglishWeight, EnglishWeight);

	friend EnglishWeight operator - (EnglishWeight, EnglishWeight);

	friend double operator / (EnglishWeight, EnglishWeight);

	friend EnglishWeight operator * (EnglishWeight, int);

	friend EnglishWeight operator * (int, EnglishWeight);

	friend EnglishWeight operator / (EnglishWeight, int);

	friend ostream& operator << (ostream& str,EnglishWeight);

	friend istream& operator >> (istream& str,EnglishWeight&);

	friend EnglishWeight convertOunces(EnglishWeight);
    

private:
	int    m_pounds;
	double m_ounces;
   
};


here's the outcome
Welcome to English Weight!
Here, you may enter two English weights and an integer, and I will perform arithmetic operations on them.
Please enter the first English weight in the standard imperial form.

 For Example: 7lb 12oz
12lb 12oz
Please enter the second English weight in the standard imperial form.
4lb 4oz
Please provide an integer.
12lb 1.000000oz + 2lb 4.000000oz = 14lb 5.000000oz
12lb 1.000000oz - 2lb 4.000000oz = 9lb 13.000000oz
12lb 1.000000oz / 2lb 4.000000oz = 5.361111
12lb 1.000000oz * 0 = 0lb 0.000000oz
0 * 2lb 4.000000oz = 0lb 0.000000oz
12lb 1.000000oz / 0 = 2

Please help me!!!
Last edited on
the result came incorrectly
Please don't make us wade through the whole thing, especially when you haven't shown the program that generates this output. Which result(s) are incorrect? What result do you expect?

A few comments on the code.

Most importantly of all, this could be done far easier if you stored the total number of ounces rather than the pounds and ounces. Convert to/from pounds/ounces on input and output. There's a very important lesson to learn here: store the data in a format that's convenient for the computer, not in the format that's convenient for the user. These are often not the same.

Why are none of these functions/operators members of the class? While there are sometimes good reasons to do this, in a beginner's class your prof probably wants to see methods.

EnglishWeight::EnglishWeight(istream& str) isn't necessary. Just have the >> operator read into the object.

You've made some of these methods too complicated. For example:
1
2
3
4
5
6
7
8
//Define operator * for number * EnglishWeight
EnglishWeight operator * (int num, EnglishWeight rhs)
{
	rhs.m_pounds *= num;
	rhs.m_ounces *= num;
	return convertOunces(rhs);
	return rhs;
}


convertOunces() isn't quite right. When I add this main program:
int main()
{
EnglishWeight w(12,4);
EnglishWeight w1 = w * 5;
cout << w1 << '\n';
}
I get this output:
61lb 4oz

Topic archived. No new replies allowed.