### 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:
 ``12345678`` ``````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?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````/*=========================================================-> 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 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276`` ``````#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<>(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.