Output is printing twice with overloaded operators

Hey there! I'm having an issue with output, luckily everything else works!!
I'm working with Mixed Numbers and operations on them.
So, here's the code I'm testing with:
1
2
3
4
5
6
7
8
int main()
{
	Mixed  m1(5), m2(1,1,1), m5(2,2,3);
cout << "m1+m2= " << m1+m2 << endl;
cout << "m1 + 10=" << m1+10 << endl;
cout << "m1 - 10=" << m1-10 << endl;
	return 0;
}


And here is the output for that code:


7/1

m1+m2= 7/1

15/1

m1 + 10= 15/1

-5/1

m1 - 10= -5/1


I don't know where the error is and I feel like I've checked everywhere, Can someone else review my code and please help me find it?

Here is my Header file:

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
/*=========================================================->
File name: fract.h
	This file contains all the fract class information
	for project 3 in COP3330 taught by Dr. David Gaitros.

Written by: Taylor Jay Brockhoeft
Last edit date: 2/16/13
<-=========================================================*/
#ifndef fract_H
#define fract_H

#include <iostream>
using namespace std; 

const int DEFAULT_VAL = 0;

class fract
{
	friend ostream &operator<<(ostream &out, fract a);
	friend istream &operator>>(istream &in, fract &c);
	friend fract operator/ (const fract &fract1,  const fract &fract2);
	friend fract operator* (const fract &fract1,  const fract &fract2);
	friend fract operator+ (const fract &fract1,  const fract &fract2);
	friend fract operator- (const fract &fract1,  const fract &fract2);
	friend bool operator== (const fract &fract1,  const fract &fract2);
	friend bool operator!= (const fract &fract1,  const fract &fract2);
	friend bool operator< (const fract &fract1,  const fract &fract2);
	friend bool operator> (const fract &fract1,  const fract &fract2);
	friend bool operator<= (const fract &fract1,  const fract &fract2);
	friend bool operator>= (const fract &fract1,  const fract &fract2);

public:
	fract(const int i=0);
	fract(const int i, const int n, const int d);
	double Evaluate();
	void ToFraction();
	void Simplify();
	int gcd(int, int);
	fract& operator++ ();
	fract operator++ (int);
	fract& operator-- ();
	fract operator-- (int);

private:
	int integer;
	int numerator; 
	int denominator;
	char slash;//For input of slash
};
#endif 


Again, an extra pair of eyes and review would be super appreciated!!!
Last edited on
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "fract.h"
fract::fract(const int i)
{
	integer = i;
	numerator = 0;
	denominator = 1;
}
fract::fract(const int i, const int n, const int d)
{
	integer = i;
	numerator = n;
	denominator = d;
}
ostream &operator<<(ostream &out, fract a)     
{
	cout << ' ';//Prints leading whitespace
	if(((a.integer<0)^(a.numerator<0))&&(a.denominator<0))
	{
		a.integer = abs(a.integer);
		a.numerator = abs(a.numerator);
		a.denominator = abs(a.denominator);
	}
	if((a.integer<0)&&(a.numerator<0))
		a.integer = -1*a.integer;
	if((a.integer != 0))
		out<<a.integer<<' ';
	if(a.numerator != 0)
	{
		out<<a.numerator<<'/'<<a.denominator;
	}
	if((a.integer == 0)&&(a.numerator == 0)&&(a.denominator == 0))
	{
		out << DEFAULT_VAL << endl;
	}
	cout << endl;//Prints newline

	return out;
}
istream &operator>>(istream &in, fract &c)
{
	cout << "Please enter an Integer and a fraction as \"I N/D\": " << endl;
	in >> c.integer;
	in >> c.numerator;
	in >> c.slash;
	in >> c.denominator;
	if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0))
	{
		c.integer = 0;
		c.numerator = 0;
		c.denominator = 0;
	}
	if((c.integer>0)&&(c.numerator<0))
	{
		c.integer = c.integer*-1;
		c.numerator = abs(c.numerator);
		c.denominator = abs(c.denominator);
	}
	return in;
}
double fract::Evaluate()//Not all paths return a value Warning error C4715 on 2/15/13
{
	bool isNegative = false;
	double fraction = (double)numerator/(double)denominator;
	if((numerator==0)&&(denominator==0))
		return integer;
	if(integer < 0)
	{
		abs(integer);
		isNegative = true;
	}
	if(isNegative == false)
		return integer+fraction;
	else if(isNegative == true)
		return (integer-fraction);
}
void fract::ToFraction()
{
		if(integer<0)
			numerator = (integer*denominator) - numerator;
		else
			numerator = (integer*denominator) + numerator;
		integer = 0;
}
void fract::Simplify()
{
	int greatestCommonDenominator = gcd(numerator,denominator);

	if(greatestCommonDenominator==0)
		cout << integer << endl;
	else if((denominator/greatestCommonDenominator != 0)&&(numerator/greatestCommonDenominator != 0))
	{	
		numerator=numerator/greatestCommonDenominator;
		denominator=denominator/greatestCommonDenominator;
		if(integer != 0)
			cout << integer << ' ';
		cout << numerator << "/" << denominator << endl << endl;
	}
	else
		cout << 0;
}
int fract::gcd(int a, int b)
{
	if(b == 0)
		return a;
	else
		return gcd(b, a%b);
}
bool operator== (const fract &fract1,  const fract &fract2)
{
	fract a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	a.Simplify();
	b.Simplify();
	if((a.numerator == b.numerator)&&(a.denominator == b.denominator))
				return true;
	else
		return false;
}
bool operator!= (const fract &fract1,  const fract &fract2)
{
	fract a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	a.Simplify();
	b.Simplify();
	if((a.numerator != b.numerator)||(a.denominator != b.denominator))
		return true;
	else
		return false;
}
bool operator> (const fract &fract1,  const fract &fract2)
{
	fract a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	a.Simplify();
	b.Simplify();
	if((a.numerator*b.denominator) > (b.numerator*a.denominator))
		return true;
	else
		return false;
}
bool operator< (const fract &fract1,  const fract &fract2)
{
	fract a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	a.Simplify();
	b.Simplify();
	if((a.numerator*b.denominator) < (b.numerator*a.denominator))
		return true;
	else
		return false;
}
bool operator>= (const fract &fract1,  const fract &fract2)
{
	fract a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	a.Simplify();
	b.Simplify();
	if((a.numerator*b.denominator) >= (b.numerator*a.denominator))
		return true;
	else
		return false;
}
bool operator<= (const fract &fract1,  const fract &fract2)
{
	fract a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	a.Simplify();
	b.Simplify();
	if((a.numerator*b.denominator) <= (b.numerator*a.denominator))
		return true;
	else
		return false;
}
fract operator/ (const fract &fract1,  const fract &fract2)
{
	fract fraction, a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	
	fraction.numerator = (a.numerator * b.denominator);
	fraction.denominator = (b.numerator * a.denominator);

	fraction.Simplify();
	return fraction;

}
fract operator* (const fract &fract1,  const fract &fract2)
{
	fract fraction, a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	
	fraction.numerator = (a.numerator * b.numerator);
	fraction.denominator = (a.denominator * b.denominator);

	fraction.Simplify();
	return fraction;

}
fract operator+ (const fract &fract1,  const fract &fract2)
{
	fract fraction, a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();

	fraction.numerator = (a.numerator * b.denominator) + (b.numerator * a.denominator);
	fraction.denominator = (a.denominator * b.denominator);
	
	fraction.Simplify();

	return fraction;

}
fract operator- (const fract &fract1,  const fract &fract2)
{
	fract fraction, a,b;
	a = fract1;
	b = fract2;
	a.ToFraction();
	b.ToFraction();
	
	fraction.numerator = (a.numerator * b.denominator) - (b.numerator * a.denominator);
	fraction.denominator = (a.denominator * b.denominator);

	fraction.Simplify();
	return fraction;

}
fract& fract::operator++ () 
{
	integer++;
	return *this;
}
fract fract::operator++ (int ignorethis)
{
	fract temp = *this;
	++integer;
	return temp;
}
fract& fract::operator-- ()
{
	integer--;
	return *this;
}
fract fract::operator-- (int ignorethis)
{
	fract temp = *this;
	--integer;
	return temp;
}

Last edited on
For a really weird reason, there are output statements in `Simplify()'
Topic archived. No new replies allowed.