Adding a fractions and classes - Woot!

Hey guys to start off I will say that I have looked into a lot of similar programs before posting this question and still need some help. My problem lies in the addition fraction class function where I need to add one fraction to another. I have one class and am currently working with to instances of that class (fractionObject and fractionObject2). I am storing my fractions separately, one in fractionObject and one in fractionObject2. How can I add these in my fraction class function 'Add'?

Any tips will be much appreciated! Thanks for your time!

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// Regular prototypes
int stringToNumber(const string &Text);
int GCD(int, int);
int LCM(int, int);

class fraction{

public: // Access Specifier
	int numerator;
	int  denominator; // Can never be 0



	// Function Prototypes
	fraction();
	void setNumDen();
	void reduce();
	void add();


};

// Member functions definitions
fraction::fraction()
{
	numerator = 0;
	denominator = 0;
}

void fraction::setNumDen()
{
	string numString;
	string denString;
	do{
		cout << "Enter a numerator and denominator of fraction 1 separated by whitespace: ";
		getline(cin, numString, ' ');
		getline(cin, denString);
		if (denString == "0")
			cout << endl << "Please enter a number that isn't zero." << endl;
	} while (denString == "0"); // Making sure denominator is not zero

	numerator = stringToNumber(numString);
	denominator = stringToNumber(denString);

}

void fraction::reduce()
{
	int leastCommonMultiple = 0;

	leastCommonMultiple = LCM(numerator, denominator);

	numerator /= leastCommonMultiple;
	denominator /= leastCommonMultiple;
}

void fraction::add()
{
	int leastComDen;
		
}

int main()
{

	fraction fractionObject, fractionObject2;

	fractionObject.setNumDen();
	fractionObject2.setNumDen();
	// Reducing and displaying the reduced fractions
	fractionObject.reduce();
	fractionObject2.reduce();
	cout << "Reduced Fraction 1 = " << fractionObject.numerator << "/" << fractionObject.denominator << "\t" << "Reduced Fraction 2 = " << fractionObject2.numerator << "/" << fractionObject2.denominator << endl;
	// Adding and displaying the fractions



	system("pause");
	return 0;
}

// Function to convert string to number
int stringToNumber(const string &Text)//Text not by const reference so that the function can be used with a 
{                               //character array as argument
	stringstream ss(Text);
	int result;
	return ss >> result ? result : 0;
}

// result=GCD(a,b)
int LCM(int a, int b) {
	int temp = 0;

	while (b != 0) {
		temp = b;
		b = a%b;
		a = temp;
	}
	return a;
}

// result=LCM(a,b);
int GCD(int a, int b) {
	int result = 0;

	result = a * (b / LCM(a, b));
	return result;
}
closed account (48T7M4Gy)
a/b + c/d = (ad + bc)/bd is a start.
Topic archived. No new replies allowed.