Incorrect Output for certain overloaded operators

I feel like I'm getting really close here with this operator overloading assignment, but I'm getting some weirdness with my output...what I should be getting:

Enter amount, dollars.cents : 22.2222
You entered $44.22
Enter amount, dollars.cents : 61.1111
You entered $72.11
Enter amount, dollars.cents : 10.3333
You entered $43.33

Testing: logical operators
$44.22 <  $44.22: false
$44.22 <= $44.22: true
$44.22 >  $44.22: false
$44.22 >= $44.22: true
$44.22 == $44.22: true
$44.22 != $44.22: false

$44.22 <  $72.11: true
$44.22 <= $72.11: true
$44.22 >  $72.11: false
$44.22 >= $72.11: false
$44.22 == $72.11: false
$44.22 != $72.11: true

$44.22 <  $43.33: false
$44.22 <= $43.33: false
$44.22 >  $43.33: true
$44.22 >= $43.33: true
$44.22 == $43.33: false
$44.22 != $43.33: true

Testing: DC1 + DC2 = DC3
$44.22 + $44.22 = $88.44
$44.22 + $72.11 = $116.33
$44.22 + $43.33 = $87.55

Testing: DC1 - DC2 = DC3
$72.11 - $44.22 = $27.89
$44.22 - $43.33 = $0.89

Testing: DC1 * int = DC2
$44.22 * 2 = $44.44
$72.11 * 2 = $72.22
$72.11 * 3 = $72.33
$43.33 * 3 = $43.99

Testing: DC1 / int = DC2
$44.22 / 2 = $22.11
$72.11 / 2 = $36.05
$72.11 / 3 = $24.03
$43.33 / 3 = $14.44

Testing: DC1 = DC1 + DC2
$44.22 += $44.22 -- after assignment: DC1=$88.44
$44.22 += $72.11 -- after assignment: DC1=$116.33
$44.22 += $43.33 -- after assignment: DC1=$87.55

Testing: DC1 = DC1 - DC2
$72.11 -= $44.22 -- after assignment: DC1=$27.89
$44.22 -= $43.33 -- after assignment: DC1=$0.89

Testing: DC1 = DC1 * int
$44.22 *= 2 -- after assignment: DC1= $88.44
$72.11 *= 2 -- after assignment: DC1= $144.22
$43.33 *= 2 -- after assignment: DC1= $86.66
$44.22 *= 3 -- after assignment: DC1= $132.66
$72.11 *= 3 -- after assignment: DC1= $216.33
$43.33 *= 3 -- after assignment: DC1= $129.99

Testing: DC1 = DC1 / int
$44.22 /= 2 -- after assignment: DC1= $22.11
$72.11 /= 2 -- after assignment: DC1= $36.05
$43.33 /= 2 -- after assignment: DC1= $21.66
$44.22 /= 3 -- after assignment: DC1= $14.74
$72.11 /= 3 -- after assignment: DC1= $24.03
$43.33 /= 3 -- after assignment: DC1= $14.44


What I'm getting instead:

Enter amount, dollars.cents : 22.2222
You entered $22.2222
Enter amount, dollars.cents : 61.1111
You entered $61.1111
Enter amount, dollars.cents : 10.3333
You entered $10.3333

Testing: logical operators
$22.2222 <  $22.2222: false
$22.2222 <= $22.2222: true
$22.2222 >  $22.2222: false
$22.2222 >= $22.2222: true
$22.2222 == $22.2222: true
$22.2222 != $22.2222: false

$22.2222 <  $61.1111: true
$22.2222 <= $61.1111: true
$22.2222 >  $61.1111: false
$22.2222 >= $61.1111: false
$22.2222 == $61.1111: false
$22.2222 != $61.1111: true

$22.2222 <  $10.3333: false
$22.2222 <= $10.3333: false
$22.2222 >  $10.3333: true
$22.2222 >= $10.3333: true
$22.2222 == $10.3333: false
$22.2222 != $10.3333: true

Testing: DC1 + DC2 = DC3
$22.2222 + $22.2222 = $88.44
$22.2222 + $61.1111 = $116.33
$22.2222 + $10.3333 = $87.55

Testing: DC1 - DC2 = DC3
$61.1111 - $22.2222 = $39.-1111
$22.2222 - $10.3333 = $12.-1111

Testing: DC1 * int = DC2
$22.2222 * 2 = $-858993460.-858993460
$61.1111 * 2 = $-858993460.-858993460
$61.1111 * 3 = $-858993460.-858993460
$10.3333 * 3 = $-858993460.-858993460

Testing: DC1 / int = DC2
$22.2222 / 2 = $-858993460.-858993460
$61.1111 / 2 = $-858993460.-858993460
$61.1111 / 3 = $-858993460.-858993460
$10.3333 / 3 = $-858993460.-858993460

Testing: DC1 = DC1 + DC2
$22.2222 += $22.2222 -- after assignment: DC1=$88.44
$22.2222 += $61.1111 -- after assignment: DC1=$116.33
$22.2222 += $10.3333 -- after assignment: DC1=$87.55

Testing: DC1 = DC1 - DC2
$61.1111 -= $22.2222 -- after assignment: DC1=$44.22
$22.2222 -= $10.3333 -- after assignment: DC1=$43.33

Testing: DC1 = DC1 * int
$22.2222 *= 2 -- after assignment: DC1= $88.44
$61.1111 *= 2 -- after assignment: DC1= $144.22
$10.3333 *= 2 -- after assignment: DC1= $86.66
$22.2222 *= 3 -- after assignment: DC1= $132.66
$61.1111 *= 3 -- after assignment: DC1= $216.33
$10.3333 *= 3 -- after assignment: DC1= $129.99

Testing: DC1 = DC1 / int
$22.2222 /= 2 -- after assignment: DC1= $22.11
$61.1111 /= 2 -- after assignment: DC1= $35.55
$10.3333 /= 2 -- after assignment: DC1= $21.66
$22.2222 /= 3 -- after assignment: DC1= $14.40
$61.1111 /= 3 -- after assignment: DC1= $23.70
$10.3333 /= 3 -- after assignment: DC1= $14.11

My 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
#include <cstdlib>
#include <string>

using namespace std;

class DollarsCents
{

  public:
    // Constructors
    DollarsCents (int dollars, int cents);
	DollarsCents();

    // getters
    int getDollars() const;
    int getCents() const;
    
    //utility function that simplifies the data members dollars and cents so that 0 <= cents <100
	void DollarsCents::simplify()
	{		
		while (cents >= 100)
		{
			cents-=100;
			dollars++;
		}
	}
    
    // new object = this + money
    DollarsCents operator+ (const DollarsCents& money);     
    
    // new object = this - money
    DollarsCents operator- (const DollarsCents& money);     

    // new object = this * num
    DollarsCents operator* (const int num);     

    // new object = this / num
    DollarsCents operator/ (const int num);     

    // this = this + money
    DollarsCents& operator+= (const DollarsCents& money);     

    // this = this - money
    DollarsCents& operator-= (const DollarsCents& money);     

    // this *= num
	DollarsCents& operator*= (const int num);                    

    // this /= num
    DollarsCents& operator/= (const int num);                    
   
	// comparison operators
    bool operator== (const DollarsCents& money) const;               
    bool operator!= (const DollarsCents& money) const;              
    bool operator<  (const DollarsCents& money) const;               
    bool operator<= (const DollarsCents& money) const;               
    bool operator>  (const DollarsCents& money) const;               
    bool operator>= (const DollarsCents& money) const;               

    // Global friend functions supporting input/output.  
    // Use dollars, cents

    friend istream& operator>> (istream& in, DollarsCents& money); 
    friend ostream& operator<< (ostream& out, const DollarsCents &money);  

  private:
    int dollars;   // valid values: >0
    int cents;     // valid values: 0-99
};
#endif 


.cpp 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
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include "DollarsCents.h"
using namespace std;



//friend functions
istream& operator>> (istream& in, DollarsCents& money)
{
	char dot;

    in >> money.dollars >> dot >> money.cents;

    return in;
}
ostream& operator<< (ostream& out, const DollarsCents &money)
{
    out << "$" << money.dollars << "." << money.cents;
return out;
}

//default constructor
DollarsCents::DollarsCents()
{
	dollars = 0;
	cents = 0;
}

//Two-parameter constructor
DollarsCents::DollarsCents (int dollars, int cents)
{
	while (cents < 0)
	{
		cents += dollars*100 + cents;
		dollars--;
	}
	simplify();
}

//Calculation Functions
DollarsCents DollarsCents::operator+ (const DollarsCents& money)
{
	DollarsCents dc_add;

	dc_add.dollars = dollars + money.dollars;
	dc_add.cents = cents + money.cents;

	dc_add.simplify();

	return dc_add;
} 

DollarsCents DollarsCents::operator- (const DollarsCents& money)
{	
	DollarsCents dc_subtract;

	if(cents > money.cents) 
	{
	dc_subtract.dollars = (dollars - 1) - money.dollars;
	dc_subtract.cents = (cents + 100) - money.cents;
	}
	else
	{
	dc_subtract.dollars = dollars - money.dollars;
	dc_subtract.cents = cents - money.cents;
	}

	dc_subtract.simplify();

	return dc_subtract;
}

DollarsCents DollarsCents::operator/(const int num)
{
	int totalCents = (dollars*100) + cents;

	totalCents /= num;

	DollarsCents dc_divide(0, totalCents);

	dc_divide.simplify();
	
	return dc_divide;
}

DollarsCents DollarsCents::operator*(const int num)
{
	int totalCents = (dollars*100) + cents;

	totalCents *= num;
	
	DollarsCents dc_multiply(0, totalCents);
	
	dc_multiply.simplify();

	return dc_multiply;
}

DollarsCents& DollarsCents::operator+= (const DollarsCents& money)
{
	dollars += money.dollars;
	cents += money.cents;
	simplify();
	return *this;
}
DollarsCents& DollarsCents::operator-= (const DollarsCents& money)
{
	if(money.cents > cents) 
	{
	dollars -= (dollars - 1) - money.dollars;
	cents -= (cents + 100) - money.cents;
	}
	else
	{
	dollars -= dollars - money.dollars;
	cents -= cents - money.cents;
	}

	simplify();
	return *this;
}
DollarsCents& DollarsCents::operator/=(const int num)
{
	this->dollars /= num;
	this->cents /= num;
	simplify();

	return *this;
}
DollarsCents& DollarsCents::operator*=(const int num)
{	
	this->dollars *= num;
	this->cents *= num;
	simplify();
	return *this;
}
Last edited on
Been working on this for a while, and I'm just not sure what I'm doing wrong any suggestions?

Edit! I figured out the -= function! (yay!) just the /, and /= functions are stopping me up and I'm reallllly close... and the fact that I'd like it to convert 22.2222 to 44.22, 61.1111 to 72.11, and 10.3333 to 43.33...
Last edited on
In your two parameter constructor you don't initialize the member variables "dollars" and "cents".
Could that be your problem?

Also, you should choose parameter names that are different from member variable names.
http://en.wikipedia.org/wiki/Variable_shadowing


Edit: typo
Last edited on
Ok so I got rid of the other constructor and just did this:
1
2
3
4
5
6
7
8
9
10
11
DollarsCents::DollarsCents (int numDollars = 0, int numCents = 0)
{
        dollars = numDollars;
        cents = numCents;
	while (cents < 0)
	{
		cents += dollars*100 + cents;
		dollars--;
	}
	simplify();
}


But now it's giving me error messages saying
1
2
3
4
error C2572: 'DollarsCents::DollarsCents' : redefinition of default parameter : parameter 2
: see declaration of 'DollarsCents::DollarsCents'
error C2572: 'DollarsCents::DollarsCents' : redefinition of default parameter : parameter 1
: see declaration of 'DollarsCents::DollarsCents'
Try it without the "=0" in the declaration (assuming you have it there) and the definition.

Edit: Got rid of the other constructor???

Edit edit: just had a chance to relook at this
Last edited on
When I change it to just DollarsCents (int numDollars, int numCents); in the definition it tells me that error C2512: 'DollarsCents' : no appropriate default constructor available
Last edited on
Is the following what you have now?

header file:
DollarsCents (int numDollars=0, int numCents=0); // declaration

implementation file:
1
2
3
4
5
6
DollarsCents::DollarsCents (int numDollars, int numCents) // definition
{
        dollars = numDollars;
        cents = numCents;
        //rest of ctor
 }
Oh whoop I switched it around, it compiles now, thanks! However, I'm still coming across the errors I was before...the output is incorrect for the operator/ and operator/= functions and it doesn't change 22.2222 to 44.22, 61.1111 to 72.11, or 10.3333 to 43.33
With your latest code it works okay for me:
Testing: DC1 / int = DC2
$22.2222 / 2 = $22.11
$61.1111 / 2 = $36.5
$61.1111 / 3 = $24.3
$10.3333 / 3 = $14.44

<snip>

Testing: DC1 = DC1 / int
$22.2222 /= 2 -- after assignment: DC1= $22.11
$61.1111 /= 2 -- after assignment: DC1= $35.55
$10.3333 /= 2 -- after assignment: DC1= $21.66
$22.2222 /= 3 -- after assignment: DC1= $14.40
$61.1111 /= 3 -- after assignment: DC1= $23.70
$10.3333 /= 3 -- after assignment: DC1= $14.11


Halamaker wrote:
it doesn't change 22.2222 to 44.22, 61.1111 to 72.11, or 10.3333 to 43.33


If you mean always show 42.22 etc. call simplify() in operator>>().

Enter amount, dollars.cents : 22.2222
You entered $44.22
Enter amount, dollars.cents : 61.1111
You entered $72.11
Enter amount, dollars.cents : 10.3333
You entered $43.33
Yes I'm getting that output as well, but I should be getting this, which is super close admittedly:


Testing: DC1 / int = DC2
$44.22 / 2 = $22.11
$72.11 / 2 = $36.05
$72.11 / 3 = $24.03
$43.33 / 3 = $14.44

<skip>

Testing: DC1 = DC1 / int
$44.22 /= 2 -- after assignment: DC1= $22.11
$72.11 /= 2 -- after assignment: DC1= $36.05
$43.33 /= 2 -- after assignment: DC1= $21.66
$44.22 /= 3 -- after assignment: DC1= $14.74
$72.11 /= 3 -- after assignment: DC1= $24.03
$43.33 /= 3 -- after assignment: DC1= $14.44


In order to call simplify(); in a friend function, do I have to do something special? I'mm getting this error
error C3861: 'simplify': identifier not found
... I don't get the error when I call it like this DollarsCents& simplify(); but it doesn't seem to do anything...
Last edited on
Sorry, I just looked at the first answer in each of those and assumed (yes, I know) the rest were correct.

$72.11 / 2 = $36.5
$72.11 / 3 = $24.3

This is an output formatting problem. Your statement in operator<<() needs to be:

out << "$" << money.dollars << "." << std::setfill('0') << std::setw(2) << money.cents;
http://www.cplusplus.com/reference/iomanip/setw/?kw=setw
http://www.cplusplus.com/reference/iomanip/setfill/?kw=setfill

For the others the problem is here:
1
2
3
4
5
6
DollarsCents& DollarsCents::operator/=(const int num)
{
    this->dollars /= num; // you don't hande a remainder. e.g. - 43/3 you drop a dollar
    this->cents /= num;
    simplify();
    . . .


Try it this way:
1
2
3
4
5
6
7
DollarsCents& DollarsCents::operator/=(const int num)
{
    cents += dollars*100;
    dollars = 0;
    cents /= num;
    simplify();
    . . .	


halamaker wrote:
In order to call simplify(); in a friend function, do I have to do something special?
money.simplify();

HTH
Ahhhh!!! yayayay thanks so much, you've been a wonderful help I appreciate you, have a wonderful day/night thanks!!!
Topic archived. No new replies allowed.