overload + and - are not yielding correct results

I have 3 files compiled with the code below. I am not getting the correct result when I add two numbers with the overload functions. Can anyone shed any light on this for me please???

I have to make the program work like this..."if the sum of the decimalPart after adding two objects are greater than 100, deduct 100 from the decimalPart and add 1 to the the integerPart. If the difference of the decimalPart after subttracting one object from another is negative, deduct 1 from the integerpart and add 100 to the decimalPart...test display with at least 4 objects of the DecimalClass the addition and subtraction of the different objects."

 ``1234567891011121314151617181920212223242526`` ``````//Decimal Class #include #ifndef DECIMAL_H_ #define DECIMAL_H_ class Decimal { private: int integerPart; int decimalPart; public: Decimal(int iPart = 0, int dPart=0); void setDecimal(int i, int d) {integerPart = i, decimalPart = d; } int getiNum() const; int getdNum() const; Decimal operator+(const Decimal& d2) const; Decimal operator-(const Decimal& d2) const; //friend ostream& operator<<(ostream& out, const Decimal& d); }; #endif ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````//Decimal implementation #include "DecimalClass.h" #include using namespace std; Decimal::Decimal(int iPart, int dPart) { integerPart = iPart; decimalPart = dPart; } int Decimal::getiNum() const{ return integerPart; } int Decimal::getdNum() const{return decimalPart;} Decimal Decimal::operator+(const Decimal& d2) const { Decimal ReturnObj; do { if( decimalPart + d2.decimalPart > 100) { ReturnObj.decimalPart = decimalPart + d2.decimalPart - 100; ReturnObj.integerPart = integerPart + 1; } else if( decimalPart - d2.decimalPart < 0) { ReturnObj.integerPart = integerPart - 1; ReturnObj.decimalPart = decimalPart - d2.decimalPart + 100; } return ReturnObj; } while (((decimalPart + d2.decimalPart) > 100)||((decimalPart - d2.decimalPart <0))); } Decimal Decimal::operator-(const Decimal& d2) const { Decimal ReturnObj; if( decimalPart - d2.decimalPart < 0) { ReturnObj.integerPart = integerPart - 1; ReturnObj.decimalPart = decimalPart - d2.decimalPart + 100; } return ReturnObj; }``````

 ``123456789101112131415161718192021222324`` ``````//Decimal Test #include "DecimalClass.h" #include using namespace std; int main() { Decimal d1; int a, b; cout<<"Please enter the integer and decimal parts of a number, seperated by a space: "; cin >> a >> b; d1.setDecimal(a, b); Decimal d2; int c, d; cout<<"Please enter another integer and decimal parts of a number: "; cin>>c>>d; d2.setDecimal(c, d); cout <<"The first number you entered is: "<
Last edited on
¿did you bother with pseudocode, flow diagram, desk test?
You need to add two numbers, like in elementary school.
I realize that these newbie questions are very annoying to many of you. I apologize for the trouble. It's just that I've been working on this for about 7 hours today already and I could really use some help with it. If anyone can help it is GREATLY appreciated.

Thanks!
-noo 1
First of all, an exact description of the problem would be helpful.

I think that the sentence
 If the difference of the decimalPart after subttracting one object from another is negative, deduct 1 from the integerpart and add 100 to the decimalPart
refers to Decimal::operator-() and should not be in the code of Decimal::operator+.
 ``1234567`` ``````ReturnObj.decimalPart = decimalPart + d2.decimalPart; ReturnObj.integerPart = integerPart + d2.integerPart ; if( ReturnObj.decimalPart >= 100 ) { ReturnObj.decimalPart -= 100; ReturnObj.integerPart += 1; }``````
Thank you @toum, I changed it...does it look correct below?

Also, when I run it, it doesn't seem to be going into the if statement when it's supposed to. Am I calling it wrong? For example, if the decimal parts are 50 and 60, it should result in a decimal of 10 with 1 added to the integer part. But it gives .110 instead.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````//Decimal implementation #include "DecimalClass.h" #include using namespace std; Decimal::Decimal(int iPart, int dPart) { integerPart = iPart; decimalPart = dPart; } int Decimal::getiNum() const{return integerPart; } int Decimal::getdNum() const{return decimalPart;} Decimal Decimal::operator+(const Decimal& d2) const { Decimal ReturnObj; ReturnObj.decimalPart = decimalPart + d2.decimalPart; ReturnObj.integerPart = integerPart + d2.integerPart; if (ReturnObj.decimalPart >= 100) { ReturnObj.decimalPart -= 100; ReturnObj.integerPart += 1; } return ReturnObj; } Decimal Decimal::operator-(const Decimal& d2) const { Decimal ReturnObj; ReturnObj.decimalPart = decimalPart - d2.decimalPart; ReturnObj.integerPart = integerPart - d2.integerPart; if( decimalPart - d2.decimalPart < 0) { ReturnObj.decimalPart += 100; ReturnObj.integerPart -= 1; } return ReturnObj; }``````

Here is main():

 ``12345678910111213141516171819202122232425262728`` ``````//Decimal Test #include "DecimalClass.h" #include using namespace std; int main() { Decimal d1; int a, b; Decimal d2; int c, d; int i =0; for (i; i < 5; i++) { cout<<"Please enter the integer and decimal parts of a number, seperated by a space: "; cin >> a >> b; d1.setDecimal(a, b); cout<<"Please enter another integer and decimal parts of a number: "; cin>>c>>d; d2.setDecimal(c, d); cout <<"The first number you entered is: "<
Last edited on
To call the operator+ you simply do `d1+d2;` (that's the point of operator overloading)

> I realize that these newbie questions are very annoying to many of you
Your do-while didn't make sense, as it had a `return` statement that would always execute.
Also there was a path where you were returning a default object
(making a flow diagram, or a desk test would allow you to see that)

By the way, your operator- is not subtracting
Last edited on
Thank you very much for all your help.

Again, I apologize if I offended. It was not my intention.
So, does it work now ?
Topic archived. No new replies allowed.