Comparing and adding fractions

For this project I am suppose to first display a number in proper fraction form by using the greatest common factor. After this I'm suppose to compare and add two fractions, then finally report if they're equal or not.
The program runs but the numbers are wrong.

I notice the denominator isn't being divided by the GCD for some reason.
Any hints would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<conio.h>

class Fraction
{
	friend void compare(Fraction a, Fraction b);
	friend void sum(Fraction a, Fraction b);
private:
	int numerator;
	int denominator;
public:
	Fraction(int num, int denom);
	int GCD();
	void proper();
};
Fraction::Fraction(int num =0, int denom =1)
{
	numerator= num;
	denominator = denom;
}

int Fraction::GCD()
{
	int gcd = 0;
	int r = 0;
	int u = numerator;
	int v = denominator;
	while (true) {
		if (v != 0) {
			gcd = u;
			break;
		}
		else {
			r = u % v;
			u = v;
			v = r;
		}
	}
	return gcd;
}

void Fraction::proper()
{
	if (numerator > 0 && denominator > 0){
		numerator = (numerator / GCD());
		denominator = (denominator / GCD());

		std::cout << "A fraction in proper form is as follows:" << std::endl;
		std::cout << numerator << "/" << denominator << std::endl;
	}
	else
		std::cout << "This fraction is an invalid number! "<< "\nCannot divide by a 0" << std::endl;
}

void sum(Fraction a, Fraction b)
{
	std::cout << "The sum of the two fractions is :" << std::endl;
	if (a.denominator != b.denominator){
		int sumNum = (a.numerator*b.denominator + b.numerator*a.denominator);
		int sumDenom = (a.denominator*b.denominator);
		std::cout << sumNum << "/" << sumDenom << std::endl;
	}
	else{
		int sumNum = (a.numerator + b.numerator);
		int sumDenom = a.denominator;
		std::cout << sumNum << "/" << sumDenom << std::endl;
	}
}

void compare(Fraction a, Fraction b){
		if ((a.numerator == b.numerator) && (a.denominator == b.denominator))
			std::cout << "The numbers are equal!!" << std::endl;
		else	
			std::cout << "The numbers are NOT equal!!" << std::endl;
}

void main(){

	Fraction a(2,10);
	Fraction b(4, 20);
	std::cout<< "This program will display a number as a proper fraction and then carry on to addtwo fractions togeather" << std::endl;
	std::cout << "-------------------------------------------------------" << std::endl;
	a.proper(); 
	b.proper();
	sum(a, b);
	compare(a, b);
	
 system("pause");
}


Thanks,
Tyler
Last edited on
Topic archived. No new replies allowed.