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."

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
//Decimal Class

#include <iostream>
#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 

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
//Decimal implementation


#include "DecimalClass.h"
#include <iostream>

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;
  }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Decimal Test

#include "DecimalClass.h"
#include <iostream>
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: "<<d1.getiNum()<<"."<<d1.getdNum()<<endl;
	cout <<"The second number you entered is: "<<d2.getiNum()<<"."<<d2.getdNum()<<endl;
	cout<<"Adding d1 and d2 is: "<<(d1+d2).getiNum()<<"."<<(d1+d2).getdNum()<<endl;
cout<<"Subtracting d2 from d1 is: "<<(d1-d2).getiNum()<<"."<<(d1-d2).getdNum()<<endl;
	return 0;
}
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+.
And your forgot to add the integer parts. It should be
1
2
3
4
5
6
7
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.

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
//Decimal implementation

#include "DecimalClass.h"
#include <iostream>

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():

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
//Decimal Test

#include "DecimalClass.h"
#include <iostream>
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: "<<d1.getiNum()<<"."<<d1.getdNum()<<endl;
		cout <<"The second number you entered is: "<<d2.getiNum()<<"."<<d2.getdNum()<<endl;
		cout<<"Adding d1 and d2 is: "<<d1.getiNum()+d2.getiNum()<<"."<<d1.getdNum()+d2.getdNum()<<endl;
		cout<<"Subtracting d2 from d1 is: "<<d1.getiNum()-d2.getiNum()<<"."<<d1.getdNum()-d2.getdNum()<<endl;
	}
	return 0;
}
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
It is not your question, it's your attempt.
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.