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."
//Decimal Test
#include "DecimalClass.h"
#include <iostream>
usingnamespace 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;
}
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.
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
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.
//Decimal Test
#include "DecimalClass.h"
#include <iostream>
usingnamespace 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;
}
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)