Fraction Class

I need some guidance and constructive feedback on how to get my code to execute adding, subtraction, multiplication, division. I have created overloaded functions, and multiple constructors to take in and set the information and do the math. The subtraction member I can't comprehend the whole negation aspect yet. Currently my code builds but when I start to add two fractions it always returns 1/0. whats wrong with the code? Math maybe?


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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

 #pragma once
#include<iostream>

using namespace std;

//Function prototypes for overloaded stream operators



class Fraction
{
private:
	int num;
	int den;
public:


	//Default Constructor creates space for num and den, then initializes 
        //its default value to 
	//num=0, den=1
	Fraction();
	//constructors sets num
	Fraction(int n);
	Fraction(int n, int d);

	void simplify();


	//Overloaded Opertators

	Fraction& operator +=(const Fraction &rhs);
	Fraction& operator *=(const Fraction &rhs);

	Fraction operator -();
	Fraction& operator /=(const Fraction &rhs);

	


	//Helper Functions

	friend ostream& operator << (ostream &out, const Fraction &obj)
	{
		
		out << obj.num << "/" << obj.den;
		return out;
	}
	friend istream& operator >> (istream &strm, Fraction &obj)
	{
		
		strm >> obj.num >> obj.den;
		obj.simplify();

		return strm;
	}

};



Fraction::Fraction()
{
	num = 0;
	den = 1;
}
//simplify function will allow the constructor to quickly deligate thisat the commence of the 
//the code
Fraction::Fraction(int n)
{
	num = n;
	den = 1;

}
Fraction:: Fraction(int n, int d)
{
	num = n;
	den = d;
   simplify();
}
//non member funtion to add new fraction object and return its value
//a copy of the left hand side is made into a fraction,
//modifies the left hand side. 
//right handside is not modified.
//a copy of the value is return...all with the help of math already performed in your 
//public member function plusequals.
Fraction operator+ (Fraction lhs, const Fraction &rhs)
{
	return lhs += rhs;
}

//negate the right hand side
//modify the numerator then add both values by calling a member function
Fraction Fraction::operator -()
{
	return(-num, den);
}


Fraction operator *(Fraction lhs, const Fraction& rhs)
{
	return lhs *= rhs;
}

Fraction operator /(Fraction lhs, const Fraction& rhs)
{
	return lhs /= rhs;
}




Fraction& Fraction::operator /=(const Fraction &rhs)
{
	// a/b (/) c/d = a*d/b*c
	int a = num;
	int b = den;
	int c = rhs.num;
	int d = rhs.den;

	num =(a * d);
	den = (b * c);

	simplify();
	return (*this);
}


Fraction& Fraction::operator *=(const Fraction &rhs)
{
	//  a/b *c/d = a*c/b*d

	int a = num;
	int b = den;
	int c = rhs.num;
	int d = rhs.den;

	num = (a * c);
	den = (b * d);
	simplify();

	return (*this);
}

Fraction& Fraction ::operator+=(const Fraction& rhs)
{
	//  a/b + c/d =(a*d +b*c)/(b*d)
	int a = num;
	int b = den;
	int c = rhs.num;
	int d = rhs.den;

	num = (a * d + b*c);
	den = (b * d);

	simplify();
	return (*this);
}



int GCD(int a, int b)
{
	if (b == 0)
		return a;
	return GCD(b, a%b);
}
void Fraction::simplify()
{
	/*for (int i = num * den; i > 1; i--)
	{
		if ((num%i) == 0 && (den% i) == 0)
		{
			num = num / i;
			den = den / i;
		}
	}*/
	int division_Factor = GCD(num,den);
	num = num/ division_Factor;
	den = den/ division_Factor;

	
}



int main()
{

	Fraction f1(1, 2), f2;
	cout << "My Fraction is: " << f1 << endl;
	cout << "Enter a fraction: ";
	cin >> f2;
	cout << "Addition: " << (f1 += f2 )<< endl;
	//cout << "Subtraction: " << f1 - f2 << endl;
	//cout << "Multiplication: " << (f1 *= f2 )<< endl;
	//cout << "Division: " << (f1 /= f2) << endl;

	
	
	
	system("Pause");
	return 0;
}
Last edited on
Please edit your post and add the closing code tag
[/code]
Use a debugger to step through your code line by line so that you can pinpoint exactly when your calculations start to go wrong.

https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017
http://wiki.codeblocks.org/index.php/Debugging_with_Code::Blocks
http://eilat.sci.brooklyn.cuny.edu/cis1_5/HowToDebug.htm (Dev C++)
https://www.cs.cmu.edu/~gilpin/tutorial/ (GNU debugger)

You never told us what input you are giving to your program.
Notice the format of your >> operator
strm >> obj.num >> obj.den;

Always check to make sure input is successful.
1
2
3
4
5
6
7
8
9
10
    cout << "Enter a fraction: ";
    if (cin >> f2)
    {
        cout << "Success!\n";
        // do calculations here
    }
    else
    {
        cout << "Failure!\n";
    }


Last edited on
@Ganado I tried adding in your code, and i got an infinite result of "Enter a fraction:". I guess im not understanding what's going at all. How did I get an infinite return?
You never told us what input you are giving to your program.

You still never told us what input you are giving to your program.

Post the code you changed. You're getting an infinite result because your cin object is in a bad state where it won't accept input, but your program is still trying to feed it input while printing.

Fine, I'll cut to the chase -- It sounds like you're trying to input a number as "3/4" but the program expects input to be entered as "3 4".
Last edited on
@Ganado I understand now. And that part I never could get to work so impatiently I took it out. I would like to make this code work so that the use can simply enter 3/4. the stream in can't output the forward slash- if it can I obviously dont know how.Here's my code:
friend istream& operator >> (istream &strm, Fraction &obj)
{
//char a = "/";
cout << "Enter a fraction: ";
if (cin >> obj)
{
cout << "success! \n";
strm >> obj.num >> "/">> obj.den;

}
else
{
cout << "Fail\n";
}
obj.simplify();

return strm;
}
If you're going to overload the >> operator for input, don't then use cin in the code body, use the strm istream& object you declared as a parameter.

If you want the user to type "3/4", then you need to do something like this:

1
2
3
4
5
6
7
8
9
10
11
	friend istream& operator >> (istream &strm, Fraction &obj)
	{
		char dummy;
		strm >> obj.num >> dummy >> obj.den;
		if (dummy != '/')
		    strm.setstate(std::ios::failbit);
		obj.simplify();

		return strm;
	}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{

	Fraction f1(1, 2), f2;
	cout << "My Fraction is: " << f1 << endl;
	cout << "Enter a fraction: ";
	
	if (!(cin >> f2))
	{
	    std::cout << "Invalid input\n";
	    return 1;
	}
	
	cout << "Addition: " << (f1 += f2) << endl;

	return 0;
}


Please use CODE FORMATTING tags: [code] and [/code] around your code.

For the record,
1
2
3
4
5
friend istream& operator >> (istream &strm, Fraction &obj)
{
    cout << "Enter a fraction: ";
    if (cin >> obj) { ... }
}

Recursively calls itself, making an infinite loop (or makes your program crash due to a stack overflow, whichever comes first).
Last edited on
@Granado Wow that was wonderful. Thank you for sticking around to help. I still have some tweaking to do but I should be able to manage. Have a great day!
Topic archived. No new replies allowed.