I'm trying to return an object from the function toFraction, which takes a decimal number as input and returns an object with the converted fraction class. The problem I'm having is storing the object, and then displaying it. Any help is appreciated.
Code:
class Fraction {
int top, bottom;
public:
Fraction(int a = 0, int b = 1);
~Fraction(){}
Fraction::Fraction (int a, int b) {
if (b < 0){
a = -a; b = -b;}
top = a; bottom = b;
}
Fraction Fraction::toFraction(double x) {
int n;
double u,v;
for (n = 1; u < 1; n++){
u = x*10*n; cout << u;
v = 10*n;}
Fraction temp(u,v);
return temp;
}
int main(void)
{
Fraction a(5,-8);
Fraction b(1,0);
Fraction c;
double dec = 0.00345;
cout << "Decimal value = " << dec <<endl;
c.toFraction(dec);
cout << "Converted to fraction = ";
c.display();
return 0;
}