There are two problems with your reduce() function.
1) If the condition a != b is true, the program will enter an infinite loop. Why? Because when a != b, neither a nor b is modified, so the condition a != b in
while(a != b)
will never be false. Thus, an infinite loop as the program has no way to exit the loop. This is what might have caused your error, though infinite loops usually just have your program run ad infinitum.
2) Also, what if a call
reduce(7, 5)
is made? You cannot accurately predict the result of a/d and b/d because d is never initialized, so line 31 has a chance to use garbage data.
Other things to point out:
-Your Fraction class is incomplete. You don't have a way to store the numerator and denominator, and the class interface is practically empty.
-The reduce function does not actually change the original values. You are passing by value, which means the parameter values are copied. What you are looking for is probably more similar to this:
1 2 3 4 5 6 7
|
//Pass by reference, which allows you to modify the original variables
void reduce(int& numerator, int& denominator);
//...
int num(16), denom(120);
reduce(num, denom); //Now num and denom will change to 2 and 15, respectively
|
Considering you have a Fraction class, it would probably be more beneficial (and more logical) to have one of the two functions:
1 2
|
void reduce(Fraction& fract); //Non-member (friend) function
void Fraction::reduce(); //Member function (simplify() sounds like a better name, though)
|
-I think your reduce() function should be trivial since you have a gcd function. It makes little sense to copy-paste perfectly reusable code.
1 2 3 4 5
|
void reduce(int& numerator, int& denominator){
const int divisor = GCD(numerator, denominator);
numerator /= divisor;
denominator /= divisor;
}
|