Why my output produces the wrong result?

I'm trying to add two objects, price and tax
in this case, price is 9.99 and tax is 0.87
Supposedly, 9.99 + 0.87=10.86, but somehow my program produces 10.86 as the ouput.
Would anyone point out my mistake? I'm stuck and couldn't figure out any other way to make my program produces the correct result.
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
  #include <iostream>
using namespace std;

/*class definition*/
class Money {
private:
   int dollars;
   int cents;
public:
   Money ();
   Money (int d, int c);
   void print () const;
   void set (int d, int c);
   void addDollars (int d);
   void addCents (int c);
   Money operator+(const Money & m) const;
};

int main()  {
   Money price;
   price.set (9, 99);     // sets the value to $9.99
   price.print();        // displays "$9.99" on console
   Money tax;
   tax.set (0, 87);   // sets the value to $0.87
   tax.print();   // displays "$0.87" on console
   Money total;
   total= price+tax;
   cout<< "total = ";
   total.print();
}

Money::Money ()   {
   dollars=0;
   cents=0;
}

Money::Money (int d, int c)   {
   dollars = d;
   cents = c;
}

void Money::print () const{
   /*Display the money on the screen */
   cout<<"$"<<dollars<<"."<<cents<<endl;
}

void Money::set (int d, int c) {
   /*sets the value for the object to "h" hours, "m" minutes and "s" seconds.*/
   dollars=d;
   cents=c;
}

void Money::addDollars (int d) {
   dollars += d;
}

void Money::addCents(int c) {
   cents += c;
   dollars += cents/100;
   cents &= 100;
}

Money Money::operator+(const Money & m)const {
   Money sum;
   sum.cents= cents + m.cents;
   sum.dollars= dollars + m.dollars + sum.cents/100;
   sum.cents &= 100;
   return sum;
}


/*------MY OUTPUT-----
$9.99
$0.87
total = $10.32 *this is supposed to be 10.86
--------------------------*/
line 67 sum.cents &= 100;
to sum.cents %= 100;
done
Thanks a lot !
Can't believe I made such a silly mistake lol
Topic archived. No new replies allowed.