simplify function help

I am building a class fraction. I have it set up to take in two parameters such as fraction f1(4/10); and reducing it would be 2/5.
I need to write a simplify function that will simplify the fraction to its smallest simplification. my number keeps outputting 0/1 (which is my default constructor for when denominator is 0.)

1
2
3
4
5
6
7
8
9
10
11
void fraction::reduce()
{
    for(int i = numer * denom; i > 1; i--)
    {
        if((numer % i) == 0 && (denom % i) == 0)
        {
            numer = numer / i;
            denom = denom / i;
        }
    }
}

If anyone could help me out it would be greatly appreciated!
it seems to me this should work? There's one thing I think can be improved: I think std::max(numer, denom) is also adequate to start i at.

Can you debug / print what the values are right after the for loop?
I thought it should too, but it doesn't. I have a constructor with a boolean setvalue function that I called inside it that basically says if you have a fraction with a denominator that is zero, then automatically make the fraction 0/1. So this is what my input/output is:

1
2
3
4
5
6
fraction f(20,40)    //declaring fraction object

cout << f << " simplified is ";
f.simplify();          //simplify function call
cout << f << '\n';
//outputs: 0/1 simplified is 0/1 


please help
never mind I figured it out. It does work, I just had put in my original fraction f(20/40) instead of f(20,40) haha oops. Now it works perfect, thanks!
Topic archived. No new replies allowed.