Hi I'm having calculation error is the function - fraction::GCD(int& gcd, int e, int f)
Example: If u enter 1/3 for both first and second fraction
The unsimplified fraction would be 6/9
And when u call the GCD function and do the calculation by urself u can see that it's not correct

Hope someone can help me figure it out cus I tried and can't seem to find the problem in the GCD function.

Thank You Very Much!!

#include <iostream>
using namespace std;

class fraction
{
public:
	void plus_equal(fraction frac1, fraction frac2);
	void input();
	void output();
	void GCD(int& gcd, int e, int f);
	void simplify(int gcd, int e, int f);
private:
	int num;
	int denom;
};

void fraction::input()
{
	cin >> num >> denom;
}

void fraction::output()
{
	cout << num << "/" << denom << endl;
}

void fraction::plus_equal(fraction frac1, fraction frac2)
{
	fraction frac_sum;
	int a, b, c, d, e, f;
	int gcd=0;

	a = frac1.num;
	b = frac1.denom;
	c = frac2.num;
	d = frac2.denom;
	e = frac_sum.num=0;
	f = frac_sum.denom=0;

	e = ((a * d) + (c * b));
	f = (b * d);

	cout << "The fraction is: " << e << "/" << f << endl;
	GCD(gcd, e, f);
	simplify(gcd, e, f);
}

void fraction::GCD(int& gcd, int e, int f)
{
	while(f != 0)
	{
		int temp;

		temp = f;
		f = e % f;
		gcd = temp;
	}
	cout << "GCD is: " << gcd << endl;
}

void fraction::simplify(int gcd, int e, int f)
{
	e = e / gcd;
	f = f / gcd;
	cout << "Simplified fraction is: " << e << "/" << f << endl;
}

int main()
{
	fraction frac1, frac2;

	cout << "Enter a fraction: ";
	frac1.input();
	frac1.output();
	cout << "Enter a fraction: ";
	frac2.input();
	frac2.output();
	frac1.plus_equal(frac1, frac2);

	return 0;
}

The order you process the numbers in is important, you have to be taking out multiples of the smaller number from the larger one. A few if statements should correct the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.