heya folks. i'm working on an chunk of code here and i don't really understand what i am doing wrong in trying to increment and decrement a fraction. if you could provide some insight i would appreciate it. thanks.

#include <iostream.h>
#include "fraction.h"

void main( )
{
        Positive_Fraction a,b,c;
	int s,t;
	
	cout << "Enter two ints:";
	cin >> s >> t;
	b.Assign(s,t);

	++b;
	b.Print( );
	
	cout << endl;

	cout << "Enter two ints:";
	cin >> s >> t;
	b.Assign(s,t);
	
	--b;
	b.Print( );
	cout << endl;
}
class Positive_Fraction
{
public:
	Positive_Fraction();
	Positive_Fraction(int,int);
	void Print();  // added 9-19
	int Get();  // added 9-19
	int Assign(int s, int t);
	
	Positive_Fraction operator++();
	Positive_Fraction operator--();
	bool operator==(Positive_Fraction f2);
	friend ostream& operator<<(ostream& c, Positive_Fraction f1);
	friend istream& operator>>(istream& c, Positive_Fraction& f1);
private:
	int top;
	int bottom;
	void Reduce();
};
#include <iostream.h>
#include "fraction.h"


Positive_Fraction :: Positive_Fraction()
{
	top = 0;
	bottom = 1;
}


Positive_Fraction :: Positive_Fraction(int a, int b)
{
	Assign(a,b);
}

 
void Positive_Fraction :: Print() 
{
	if (top == 0) 
		cout << 0 << endl;
	else
	{
		if (top / bottom != 0)
		{
			cout << top / bottom;
			if (top % bottom != 0)
				cout << " and ";
		}
		if (top%bottom != 0)
			cout << top % bottom << '/' << bottom;
	}

}


int Positive_Fraction :: Get()
{
	char ch;
	int s,t;
	cin >> s >> ch >> t;
	if (ch != '/')
	{
		cout << "Positive_Fraction not assigned!\n";
		return 1;
	}
	return Assign(s,t);
}



int Positive_Fraction :: Assign(int s, int t)
{
	if (t == 0)
	{
		cout << "Positive_Fraction not assigned!\n";
		return 1;
	}
	else
	{
	top = s;
	bottom = t;
	Reduce();
	return 0;
	}
}



ostream& operator<<(ostream& co, Positive_Fraction f1)
{
	if (f1.top == 0) co << 0;
	if (f1.top >= f1.bottom)
	{
		co << f1.top / f1.bottom;
		if (f1.top % f1.bottom != 0)
			co << " and ";
	}
	if (f1.top % f1.bottom != 0)
		co << f1.top % f1.bottom << "/" << f1.bottom;
	return co;
}


istream& operator>>(istream& c, Positive_Fraction& f1)
{
	char ch;
	int s,t;
	c >> s >> ch >> t;
	if (ch != '/') return c;
	f1.Assign(s,t);
	return c;
}


void Positive_Fraction :: Reduce()
{
	if (top == 0)
		bottom = 1;
	else
	{
		int min = (top < bottom ? top : bottom);
		int gcd;
		for (gcd = min; gcd > 1; --gcd)
		{
			if (top % gcd == 0 && bottom % gcd == 0)
				break;
		}
		top = top/gcd;
		bottom = bottom/gcd;
	}
}



// what's below are a few different angles of trying to write this code.  i know the decrement function makes little sense with how i was trying to call it...this is all just part of trial and error.



Positive_Fraction Positive_Fraction :: operator++()
{
	Positive_Fraction temp;
	
	top += bottom;
	return temp;
}

/*
Positive_Fraction operator++(top, bottom)
{

	temp = top += bottom;
}

*/


Positive_Fraction Positive_Fraction :: operator--()
{
	Positive_Fraction temp;
	
	temp.top = bottom - top;
	temp.bottom = bottom;
	return temp;
}

hope what i was doing makes sense. this is part of a larger program that works fine except for this portion. thanks in advance for any ideas.

Recommended Answers

All 5 Replies

I am not sure what you wanted this compiles and runs so here added comments for some info.

#include <iostream>//This is the proper header.
using namespace std;//Put everything in the std namespace
//not always the best idea but the easiest

class Positive_Fraction
{
public:
	Positive_Fraction();
	Positive_Fraction(int,int);
	void Print();  // added 9-19
	int Get();  // added 9-19
	int Assign(int s, int t);
	
	Positive_Fraction operator++();
	Positive_Fraction operator--();
	bool operator==(Positive_Fraction f2);
	friend ostream& operator<<(ostream& c, Positive_Fraction f1);
	friend istream& operator>>(istream& c, Positive_Fraction& f1);
private:
	int top;
	int bottom;
	void Reduce();
};

Positive_Fraction :: Positive_Fraction()
{
	top = 0;
	bottom = 1;
}


Positive_Fraction :: Positive_Fraction(int a, int b)
{
	Assign(a,b);
}

 
void Positive_Fraction :: Print() 
{
	if (top == 0) 
		cout << 0 << endl;
	else
	{
		if (top / bottom != 0)
		{
			cout << top / bottom;
			if (top % bottom != 0)
				cout << " and ";
		}
		if (top%bottom != 0)
			cout << top % bottom << '/' << bottom;
	}

}


int Positive_Fraction :: Get()
{
	char ch;
	int s,t;
	cin >> s >> ch >> t;
	if (ch != '/')
	{
		cout << "Positive_Fraction not assigned!\n";
		return 1;
	}
	return Assign(s,t);
}



int Positive_Fraction :: Assign(int s, int t)
{
	if (t == 0)
	{
		cout << "Positive_Fraction not assigned!\n";
		return 1;
	}
	else
	{
	top = s;
	bottom = t;
	Reduce();
	return 0;
	}
}



ostream& operator<<(ostream& co, Positive_Fraction f1)
{
	if (f1.top == 0) co << 0;
	if (f1.top >= f1.bottom)
	{
		co << f1.top / f1.bottom;
		if (f1.top % f1.bottom != 0)
			co << " and ";
	}
	if (f1.top % f1.bottom != 0)
		co << f1.top % f1.bottom << "/" << f1.bottom;
	return co;
}


istream& operator>>(istream& c, Positive_Fraction& f1)
{
	char ch;
	int s,t;
	c >> s >> ch >> t;
	if (ch != '/') return c;
	f1.Assign(s,t);
	return c;
}


void Positive_Fraction :: Reduce()
{
	if (top == 0)
		bottom = 1;
	else
	{
		int min = (top < bottom ? top : bottom);
		int gcd;
		for (gcd = min; gcd > 1; --gcd)
		{
			if (top % gcd == 0 && bottom % gcd == 0)
				break;
		}
		top = top/gcd;
		bottom = bottom/gcd;
	}
}

Positive_Fraction Positive_Fraction::operator++()
{
    top += bottom;
    return *this;//return the caller of the operator
}

Positive_Fraction Positive_Fraction::operator--()
{
    top = bottom - top;
    return *this;
}

int main()//main returns an int always has maybe always will :)
{
    Positive_Fraction a,b,c;
	int s,t;
	
	cout << "Enter two ints:";
	cin >> s >> t;
	b.Assign(s,t);

	++b;
	b.Print( );
	
	cout << endl;

	cout << "Enter two ints:";
	cin >> s >> t;
	b.Assign(s,t);
	
	--b;
	b.Print( );
	cout << endl;
	
	return 0;
}

operator ++ and -- with no arguments return a reference to an object and not an object itself. bman you should have known better.

operator ++ and -- with no arguments return a reference to an object and not an object itself. bman you should have known better.

here's a question I have. the manner in which the increment and decrement are written...are they still considered member functions?

btw: I changed the decrement to match the syntax of the increment. and thank you for the help :)

operator ++ and -- with no arguments return a reference to an object and not an object itself. bman you should have known better.

Not really since I have never overloaded the ++ and -- in my travels. Thanks for the information :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.