distance class

I was working on a program that deals with adding and subtracting distances (given in feet and inches). the output is suppose to be :

d1: 0' 0"
d2: 2' 5.9"
d3: 0' 3.75"
d4: 6' 7.34"
d5: 8' 4"
d4 + d5: 14' 11.34"
d2 + d4: 9' 1.24"
d3 - d1: 0' 3.75"
d1 - d3: 0' 3.75"
d4 - d2: 4' 1.44"
d2 - d4: 4' 1.44"
d4 - d5: 1' 8.66"
d5 - d2: 5' 10.1"

But instead, I got:
d1: 0' 0''

d2: 2' 5.9''

d3: 0' 3.75''

d4: 6' 7.34''

d5: 8' 4''

d4 + d5: 14' 11.34''

d2 + d4: 9' 1.24''

d3 - d1: 0' 3.75''

heyd1 - d3: 0' 3.75''

d4 - d2: 4' 1.44''

d2 - d4: 4' -1.44''

d4 - d5: 2' 3.34''

heyd5 - d2: 6' 1.9''

I think the problem is somewhere in the const Distance Distance::operator-(const Distance &rhs) const function but I just can't point it out. Please give me some feedback. Here is the code:

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  #include <iostream>
using namespace std;

class Distance
{
  public:

    /* Constructs a default Distance of 0 (0 feet and 0.0 inches)
    */
    Distance();
    /* Constructs a distance of ft feet and in inches,
        unless in >= 12, in which case the values of feet and inches
        are adjusted accordingly. A Distance will always be positive.
    */
    Distance(int, double);
    /* Constructs a distance of 0 ft and in inches, 
        unless in >= 12, in which case the values of feet and inches
        are adjusted accordingly. A Distance will always be positive.
    */
    Distance(double in);
    /* Returns just the feet portion of the Distance
    */
    unsigned getFeet() const;
    /* Returns just the inches portion of the Distance
    */
    double getInches() const;
     /* Returns the entire distance as the equivalent amount of inches.
        (e.g., 4 feet 3.5 inches would be returned as 51.5 inches)
    */
    double distanceInInches() const;
    /* Returns the entire distance as the equivalent amount of feet.
        (e.g., 3 feet 6 inches would be returned as 3.5 feet)
    */
    double distanceInFeet() const;
    /* Returns the entire distance as the equivalent amount of meters.
        1 inch equals 0.0254 meters.
        (e.g., 2 feet 8.12 inches would be returned as 0.815848 meters)
    */
    double distanceInMeters() const;
    /* Returns the sum of 2 Distances.
    */
    const Distance operator+(const Distance &rhs) const;
    /* Returns the difference between 2 Distances.
    */
    const Distance operator-(const Distance &rhs) const;
    /* Outputs to the stream out the Distance in the format: 
        feet' inches'' (i.e. 3' 3.41'')
    */
    friend ostream & operator<<(ostream &out, const Distance &rhs);
    /* Used by the 2 parameterized constructors to convert any negative values to positive and
        inches >= 12 to the appropriate number of feet and inches.
    */
    //this could be a mutator function?
    friend void init(int& , double& );
    
  private:
    int feet;
    double inches;
};

Distance::Distance() {
     feet = 0;
     inches = 0.0;
    }

Distance::Distance(int ft, double in) {
         if ((in >= 12) || (in < 0)) {
            void init(int& , double& );
            init(ft, in);
            feet = ft;
            inches = in;
        }
        else
        {
            inches = in;
            feet = ft;
        }
}

Distance::Distance(double in) {
        if ((in >= 12) || (in < 0)) {
            int ft = 0;
            void init(int& , double& );
            init(ft, in);
            feet = ft;
            inches = in;
        }
        else
        {
            inches = in;
            feet = 0;
        }
}

unsigned Distance::getFeet() const {
        return feet;
}

double Distance::getInches() const {
        return inches;
}

double Distance::distanceInInches() const {
        return (inches + (feet * 12));
}

double Distance::distanceInFeet() const {
        return (feet + (inches / 12));
}

double Distance::distanceInMeters() const {
        return ((feet + (inches / 12))*0.3048);
}

const Distance Distance::operator+(const Distance &rhs) const {
      Distance newSum;
      newSum.feet = (feet + rhs.feet);
      newSum.inches = (inches + rhs.inches);
      if (newSum.inches >= 12.0) {
          int tempoft = newSum.feet;
          double tempoin = newSum.inches;
          void init(int& , double& );
          init(tempoft, tempoin); 
          newSum.feet = tempoft;
          newSum.inches = tempoin;
      }
      return newSum;
}

const Distance Distance::operator-(const Distance &rhs) const {
      Distance newDiff;
      newDiff.feet = (feet - rhs.feet);
      newDiff.inches = (inches - rhs.inches);
      if (!(newDiff.feet >= 0)) {
          newDiff.feet = (rhs.feet - feet);
          //cout << "hey";
      }
      else if ((!(newDiff.inches >= 0.0)) || (newDiff.inches >= 12.0)) {
          int tempoft2 = newDiff.feet;
          double tempoin2 = newDiff.inches;
          void init(int& , double& );
          init(tempoft2, tempoin2); 
          newDiff.feet = tempoft2;
          newDiff.inches = tempoin2;
          //cout << "hey";
      }
      return newDiff;
}

void init(int& ft, double& in) {
        if(in < 0.0) {
            in = -in;
            cout << "hey";
        }
        else if (in >= 12.0) {
            ft = ft + (in / 12.0);
            double decimDigits = (in - static_cast <int>(in));
            in = (static_cast <int>(in) % 12);
            in = in + decimDigits;
        }
}


ostream & operator<<(ostream &out, const Distance &rhs) {
      out << rhs.feet << "' " << rhs.inches << "''" << endl;
      return out;
}

int main()
{
  Distance d1;
  cout << "d1: " << d1 << endl;

  Distance d2 = Distance(2, 5.9);
  Distance d3 = Distance(3.75);
  cout << "d2: " << d2 << endl;
  cout << "d3: " << d3 << endl;

  //test init helper function
  Distance d4 = Distance(5, 19.34);
  Distance d5 = Distance(100);
  cout << "d4: " << d4 << endl;
  cout << "d5: " << d5 << endl;

  //test add (<12 inches)
  Distance sum1, sum2, tot1, tot2, tot3, tot4, tot5, tot6;
  sum1 = (d4 + d5);
  //Distance newSum2(sum2.feet, sum2.inches);
  cout << "d4 + d5: " << sum1 << endl;
  sum2 = (d2 + d4);
  //test add (>12 inches)
  cout << "d2 + d4: " << sum2 << endl;
  tot1 = (d3 - d1);
  //test sub (0 ft)
  cout << "d3 - d1: " << tot1 << endl;
  //test sub (0 ft, negative conversion)
  tot2 = (d1 - d3);
  cout << "d1 - d3: " << tot2 << endl;
  tot3 = (d4 - d2);
  //test sub (positive ft & inch)
  cout << "d4 - d2: " << tot3 << endl;
  tot4 = (d2 - d4);
  //test sub (negative ft & inch)
  cout << "d2 - d4: " << tot4 << endl;
  tot5 = (d4 - d5);
  //test sub (negative ft, positive inch)
  cout << "d4 - d5: " << tot5 << endl;
  tot6 = (d5 - d2);
  //test sub (positive ft, negative inch)
  cout << "d5 - d2: " << tot6 << endl;

  return 0;
}
Hi,

Consider using a debugger, hopefully there is a GUI one in your IDE. Create a watch-list of variables you need to keep an eye on, then step through the code 1 line a t a time, deduce where things go wrong. This is by far the best way to find problems, it will save you days of staring at code, and it is what developers do.

Good Luck !!
closed account (48T7M4Gy)
The error appears to be in the subtract operation.

Why not convert the distances to inches, perform the subtraction, convert the result to feet and inches and return that?

There are no if's and no complicated lines if you do it that way.

It would be worthwhile to consider doing the same for addition.
Line 54: Why is init a friend function?

Line 68,83,122,141: Why do you have function prototypes embedded in the middle of if blocks?

Line 67-77: Why not put all your checking and initialization inside init() instead of repeating it for each constructor. Makes only a single function you have to debug:

Line 81-92, 116-122, 131-146: ditto


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
 
Distance::Distance () 
{   init (0, 0.0);
}

Distance::Distance (int ft, double in) 
{   init (ft, in); 
}

Distance::Distance (double in) 
{   init (0, in); 
}

const Distance Distance::operator+(const Distance &rhs) const 
{   return Distance (feet + rhs.feet, inches + rhs.inches);
}

const Distance Distance::operator-(const Distance &rhs) const 
{   return Distance (feet - rhs.feet, inches - rhs.inches);
}

// Converted to a member function
void Distance::init (int ft, double in)   //  No need for reference arguments
{   if (ft < 0)
        ft = -ft;
    if (in < 0.0) 
        in = -in;
    if (in >= 12.0) 
    {   ft = ft + (in / 12.0);
        double decimDigits = (in - static_cast <int>(in));
        in = (static_cast <int>(in) % 12);
        in = in + decimDigits;
    }
    feet = ft;
    inches = in;
}
d1: 0' 0''
d2: 2' 5.9''
d3: 0' 3.75''
d4: 6' 7.34''
d5: 8' 4''
d4 + d5: 14' 11.34''
d2 + d4: 9' 1.24''
d3 - d1: 0' 3.75''
d1 - d3: 0' 3.75''
d4 - d2: 4' 1.44''
d2 - d4: 4' 1.44''
d4 - d5: 2' 3.34''
d5 - d2: 6' 1.9''







Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

class Distance
{
public:
    Distance();
    Distance(int, double);
    Distance(double in);
    int getFeet() const;
    double getInches() const;
    double distanceInFeet() const;
    double distanceInMeters() const;
    const Distance operator+(const Distance &rhs) const;
    const Distance operator-(const Distance &rhs) const;
    friend std::ostream & operator<<(std::ostream &out, const Distance &rhs);

private:
    double inches;
};

Distance::Distance() {
    inches = 0.0;
}

Distance::Distance(int ft, double in) {  
    inches = ft * 12.0 + in;
}

Distance::Distance(double in) {
    inches = in;
}

int Distance::getFeet() const {
    return inches / 12;
}

double Distance::getInches() const {
    return inches;
}

double Distance::distanceInFeet() const {
    return inches / 12.0;
}

double Distance::distanceInMeters() const {
    return inches * 25.4 / 1000;
}

const Distance Distance::operator+(const Distance &rhs) const {
    return this -> inches + rhs.inches;
}

const Distance Distance::operator-(const Distance &rhs) const {
    return this -> inches - rhs.inches;
}

std::ostream & operator<<(std::ostream &out, const Distance &rhs) {
    if ( rhs.inches < 0 )
        out << -rhs.getFeet() << "' " << -rhs.inches + rhs.getFeet() * 12.0 << "\"";
    else
        out << rhs.getFeet() << "' " << rhs.inches - rhs.getFeet() * 12.0 << "\"";
    
    return out;
};


int main()
{
    Distance d1;
    std::cout << "d1: " << d1 << std::endl;
    
    Distance d2 = Distance(2, 5.9);
    Distance d3 = Distance(3.75);
    std::cout << "d2: " << d2 << std::endl;
    std::cout << "d3: " << d3 << std::endl;
    
    //test init helper function
    Distance d4 = Distance(5, 19.34);
    Distance d5 = Distance(100);
    std::cout << "d4: " << d4 << std::endl;
    std::cout << "d5: " << d5 << std::endl;
    
    //test add (<12 inches)
    Distance sum1, sum2, tot1, tot2, tot3, tot4, tot5, tot6;
    sum1 = (d4 + d5);
    //Distance newSum2(sum2.feet, sum2.inches);
    std::cout << "d4 + d5: " << sum1 << std::endl;
    sum2 = (d2 + d4);
    //test add (>12 inches)
    std::cout << "d2 + d4: " << sum2 << std::endl;
    tot1 = (d3 - d1);
    //test sub (0 ft)
    std::cout << "d3 - d1: " << tot1 << std::endl;
    //test sub (0 ft, negative conversion)
    tot2 = (d1 - d3);
    std::cout << "d1 - d3: " << tot2 << std::endl;
    tot3 = (d4 - d2);
    //test sub (positive ft & inch)
    std::cout << "d4 - d2: " << tot3 << std::endl;
    tot4 = (d2 - d4);
    //test sub (negative ft & inch)
    std::cout << "d2 - d4: " << tot4 << std::endl;
    tot5 = (d4 - d5);
    //test sub (negative ft, positive inch)
    std::cout << "d4 - d5: " << tot5 << std::endl;
    tot6 = (d5 - d2);
    //test sub (positive ft, negative inch)
    std::cout << "d5 - d2: " << tot6 << std::endl;
    
    return 0;
}


d1: 0' 0"
d2: 2' 5.9"
d3: 0' 3.75"
d4: 6' 7.34"
d5: 8' 4"
d4 + d5: 14' 11.34"
d2 + d4: 9' 1.24"
d3 - d1: 0' 3.75"
d1 - d3: 0' 3.75"
d4 - d2: 4' 1.44"
d2 - d4: 4' 1.44"
d4 - d5: 1' 8.66"
d5 - d2: 5' 10.1"
 
Exit code: 0 (
Last edited on
@kemort: Your output does not match the OP's expected output.

d1: 0' 0"
d2: 2' 5.9"
d3: 0' 3.75"
d4: 6' 7.34"
d5: 8' 4"
d4 + d5: 14' 11.34"
d2 + d4: 9' 1.24"
d3 - d1: 0' 3.75"
d1 - d3: 0' 3.75"
d4 - d2: 4' 1.44"
d2 - d4: 4' 1.44"
d4 - d5: 1' 8.66"
d5 - d2: 5' 10.1"

Thank you all for your input. Thanks to your help, finally got it to work:

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
173
174
#include <iostream>
using namespace std;

class Distance
{
  public:

    /* Constructs a default Distance of 0 (0 feet and 0.0 inches)
    */
    Distance();
    /* Constructs a distance of ft feet and in inches,
        unless in >= 12, in which case the values of feet and inches
        are adjusted accordingly. A Distance will always be positive.
    */
    Distance(int, double);
    /* Constructs a distance of 0 ft and in inches, 
        unless in >= 12, in which case the values of feet and inches
        are adjusted accordingly. A Distance will always be positive.
    */
    Distance(double in);
    /* Returns just the feet portion of the Distance
    */
    unsigned getFeet() const;
    /* Returns just the inches portion of the Distance
    */
    double getInches() const;
     /* Returns the entire distance as the equivalent amount of inches.
        (e.g., 4 feet 3.5 inches would be returned as 51.5 inches)
    */
    double distanceInInches() const;
    /* Returns the entire distance as the equivalent amount of feet.
        (e.g., 3 feet 6 inches would be returned as 3.5 feet)
    */
    double distanceInFeet() const;
    /* Returns the entire distance as the equivalent amount of meters.
        1 inch equals 0.0254 meters.
        (e.g., 2 feet 8.12 inches would be returned as 0.815848 meters)
    */
    double distanceInMeters() const;
    /* Returns the sum of 2 Distances.
    */
    const Distance operator+(const Distance &rhs) const;
    /* Returns the difference between 2 Distances.
    */
    const Distance operator-(const Distance &rhs) const;
    /* Outputs to the stream out the Distance in the format: 
        feet' inches'' (i.e. 3' 3.41'')
    */
    friend ostream & operator<<(ostream &out, const Distance &rhs);
    /* Used by the 2 parameterized constructors to convert any negative values to positive and
        inches >= 12 to the appropriate number of feet and inches.
    */
    //this could be a mutator function?
    void init(int , double );
    
  private:
    int feet;
    double inches;
};

Distance::Distance() {
     feet = 0;
     inches = 0.0;
    }

Distance::Distance(int ft, double in) {
    init(ft, in);
}

Distance::Distance(double in) {
    init(0, in);
}

unsigned Distance::getFeet() const {
        return feet;
}

double Distance::getInches() const {
        return inches;
}

double Distance::distanceInInches() const {
        return (inches + (feet * 12));
}

double Distance::distanceInFeet() const {
        return (feet + (inches / 12));
}

double Distance::distanceInMeters() const {
        return ((feet + (inches / 12))*0.3048);
}

const Distance Distance::operator+(const Distance &rhs) const {
    return Distance ((feet + rhs.feet), (inches + rhs.inches));
}

const Distance Distance::operator-(const Distance &rhs) const {
    double diff = (distanceInFeet() - rhs.distanceInFeet());
    double diffdig = diff - static_cast<int>(diff);
    diffdig = (diffdig*12.0);
    return Distance (static_cast<int>(diff), diffdig);
}

void Distance::init(int ft, double in) {
        if (ft < 0) {
            ft = -ft;
            //cout << "hey";
        }
        if (in < 0.0) {
            in = -in;
            //cout << "hey";
        }
        if (in >= 12.0) {
            ft = ft + (in / 12.0);
            double decimDigits = (in - static_cast <int>(in));
            in = (static_cast <int>(in) % 12);
            in = in + decimDigits;
        }
        feet = ft;
        inches = in;
}

ostream & operator<<(ostream &out, const Distance &rhs) {
      out << rhs.feet << "' " << rhs.inches << "''" << endl;
      return out;
}

int main()
{
  Distance d1;
  cout << "d1: " << d1 << endl;

  Distance d2 = Distance(2, 5.9);
  Distance d3 = Distance(3.75);
  cout << "d2: " << d2 << endl;
  cout << "d3: " << d3 << endl;

  //test init helper function
  Distance d4 = Distance(5, 19.34);
  Distance d5 = Distance(100);
  cout << "d4: " << d4 << endl;
  cout << "d5: " << d5 << endl;

  //test add (<12 inches)
  Distance sum1, sum2, tot1, tot2, tot3, tot4, tot5, tot6;
  sum1 = (d4 + d5);
  //Distance newSum2(sum2.feet, sum2.inches);
  cout << "d4 + d5: " << sum1 << endl;
  sum2 = (d2 + d4);
  //test add (>12 inches)
  cout << "d2 + d4: " << sum2 << endl;
  tot1 = (d3 - d1);
  //test sub (0 ft)
  cout << "d3 - d1: " << tot1 << endl;
  //test sub (0 ft, negative conversion)
  tot2 = (d1 - d3);
  cout << "d1 - d3: " << tot2 << endl;
  tot3 = (d4 - d2);
  //test sub (positive ft & inch)
  cout << "d4 - d2: " << tot3 << endl;
  tot4 = (d2 - d4);
  //test sub (negative ft & inch)
  cout << "d2 - d4: " << tot4 << endl;
  tot5 = (d4 - d5);
  //test sub (negative ft, positive inch)
  cout << "d4 - d5: " << tot5 << endl;
  tot6 = (d5 - d2);
  //test sub (positive ft, negative inch)
  cout << "d5 - d2: " << tot6 << endl;

  return 0;
}


you can check to see that the output is as desired

d1: 0' 0''

d2: 2' 5.9''

d3: 0' 3.75''

d4: 6' 7.34''

d5: 8' 4''

d4 + d5: 14' 11.34''

d2 + d4: 9' 1.24''

d3 - d1: 0' 3.75''

d1 - d3: 0' 3.75''

d4 - d2: 4' 1.44''

d2 - d4: 4' 1.44''

d4 - d5: 1' 8.66''

d5 - d2: 5' 10.1''
Topic archived. No new replies allowed.