I had to write a fraction calculator program and my only problem (I think) is when I input the two fractions it gives me garbage - for instance if I put 1/2 + 1/2 it gives me 1374389536/687194768 - can anyone help me correct this please?
Thanks!

divisionByZero.h

#include <iostream>
#include <string>
 
using namespace std;

class divisionByZero                          
{                                             
public:                                       
    divisionByZero()                          
    {
        message = "Division by zero";         
    }                                         

    divisionByZero(string str)                
    {                                         
        message = str;                        
    }                                         

    string what()                             
    {                                         
        return message;                       
    }                                         

private:                                      
    string message;                           
};

main.ccp

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

using namespace std;

void doOperation(int& a, int& b, int& c,int& d) throw (divisionByZero);
void addFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void subtractFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void multiplyFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void divideFractions(int& a, int& b, int& c,int& d, int& num, int& den);
void exit();
void menu(char operationType);

int main()
{
	int a=0,b=0,c=0,d=0;

    try                                    
    {
		doOperation(a, b, c, d); 
    }
    catch (divisionByZero divByZeroObj)    
    {
        cout << divByZeroObj.what() << endl;
    }

    return 0;                               
}

void doOperation(int& a, int& b, int& c,int& d) throw (divisionByZero)
{

	char operationType = 'a';

    try                                               
    {
        cout << "Enter the numerator for the first fraction: ";
		cin >> a;

		cout << "Enter the denominator for the first fraction: ";
		cin >> b;

		cout << "Enter the numerator for the second fraction: ";
		cin >> c;

		cout << "Enter the denominator for the second fraction: ";
		cin >> d;

        if (b == 0 || d == 0)                            
            throw divisionByZero();                  

			menu(operationType);               
                                       
    }
    catch (divisionByZero)                            
    {
        throw 
          divisionByZero("Denominator can not be zero!");  
    }
}

void menu(char operationType) 
{
	int  a,b,c,d,num,den;

	cout << "Please choose the operation you would like to perform:"<< endl;
    cout << "		+ (addition)\n"
		 << "		- (subtraction)\n"
		 << "		* (multiplication)\n"
		 << "		/ (division)\n"
		 << "		x (exit)\n"
		 << "Operation to perform: ";
    cin >> operationType;
    cout << endl;

	switch (operationType)
	{
	case '+': 
		addFractions(a,b,c,d,num,den);

		cout << "\nThe answer is " << num << "/" << den << endl;
		
		break;
	case '-':
		subtractFractions(a,b,c,d,num,den);
		
		cout << "\nThe answer is " << num << "/" << den << endl;

		break;
	case '*':
		multiplyFractions(a,b,c,d,num,den);
		
		cout << "\nThe answer is " << num << "/" << den << endl;

		break;
	case '/':
		divideFractions(a,b,c,d,num,den);
		
		cout << "\nThe answer is " << num << "/" << den << endl;

		break;
	case 'x':
		exit();
		break;
	default:
		cout << "Your input was invalid.\n"<< endl;
	}
}

//function to add fractions
void addFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
	num = (a*d + b*c);
	den = (b*d);
}

//function to subtract fractions
void subtractFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
	num = ((a*d) - (b*c));
	den = (b*d);
}

//function to multiply fractions
void multiplyFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{	
    num = (a*c);
	den = (b*d);
}

//function to divide fractions
void divideFractions(int& a, int& b, int& c,int& d, int& num, int& den)
{
    num = (a*d);
	den = (b*c);
}

void exit()
{
	cout << "Thank you for using the Fraction Calculator." <<endl;
}

Perhaps a class like this might help.. Ive done abit for you.

#include<iostream>
using namespace std;

class fraction {
public:
	int Numerator;
	int Denominator;
	fraction(int n, int d) {
		Numerator = n;
		Denominator = d;
	}
	bool Shrink() {
		bool sucessful = 0;
		for (int i = min(Numerator, Denominator); i; i--) {
			if ((Numerator % i == 0) && (Denominator % i == 0)) {
				Numerator /= i;
				Denominator /= i;
				sucessful = 1;
			}
		}
		return sucessful;
	}
	fraction operator +(fraction &fraction2) {
		int n1 = Numerator;
		int d1 = Denominator;
		int n2 = fraction2.Numerator;
		int d2 = fraction2.Denominator;
		int MatchedDenominator = d1 * d2;
		n1 = MatchedDenominator / d1;
		n2 = MatchedDenominator / d2;
		fraction f(n1+n2,MatchedDenominator);
		f.Shrink();
		return f;
	}
};

int main() {
	fraction num1(1, 2);
	fraction num2(1, 3);
	fraction num3 = num1 + num2;
	cout << num3.Numerator << '/' << num3.Denominator;
	cin.ignore();
	return 0;
}

Heres the corrected code:

#include<iostream>
using namespace std;

class fraction {
public:
	int Numerator;
	int Denominator;
	fraction(int n, int d) {
		Numerator = n;
		Denominator = d;
	}
	bool Shrink() {
		bool sucessful = 0;
		for (int i = min(Numerator, Denominator); i; i--) {
			if ((Numerator % i == 0) && (Denominator % i == 0)) {
				Numerator /= i;
				Denominator /= i;
				sucessful = 1;
			}
		}
		return sucessful;
	}
	fraction operator +(fraction &fraction2) {
		int n1 = Numerator;
		int d1 = Denominator;
		int n2 = fraction2.Numerator;
		int d2 = fraction2.Denominator;
		int MatchedDenominator = d1 * d2;
		fraction f(
			((MatchedDenominator / d1) * n1) + 
			((MatchedDenominator / d2) * n2)
			,MatchedDenominator);
		f.Shrink();
		return f;
	}
	void Display() {
		cout << Numerator << '/' << Denominator;
	}
};

int main() {
	fraction f1(235, 1000);
	fraction f2(2, 10);
	fraction f3 = f1 + f2;
	f3.Display();
	cin.ignore();
	return 0;
}

Should work perfectly :)

And the multiplication is faily easy:

fraction operator *(fraction &fraction2) {
	int n1 = Numerator;
	int d1 = Denominator;
	int n2 = fraction2.Numerator;
	int d2 = fraction2.Denominator;
	fraction f(n1 * n2, d1 * d2);
	f.Shrink();
	return f;
}

Dont know why.., but I made the entire class for you (and me), including a parser and just about every operator available :)

Some examples on how it works:

  • fraction(33,99) automaticly turns into 1/3
  • Parser will work with spaces e.g
    fraction f = "1 /2 /    3";

    is the same as 1/2/3 (1.66666)

  • fraction f = 12.2; // f will cout "12/1/5"

Code is getting abit big so I have attached the code.
If you find any problems with it, let me know :icon_wink:

Attachments
#include<iostream>
#include<cmath>
class fraction {
	static
	inline bool TextContains(char *txt, char ch) {
		for (int i = 0; txt[i]; i++)
			if (txt[i] == ch) return 1;
		return 0;
	}
	static
	inline char *SubStr(char *text, int beg, int end) {
		register int len = end - beg;
		char *cut = new char[len];
		memcpy_s(cut,(rsize_t)len,&text[beg],(size_t)len);
		cut[len] = '\0';
		return cut;
	}
	static
	char *GetWord(char *text, char *gaps, int bzIndex) {
		int i=0,sc=0,ec=0,g=0;
		for(;text[i]&&TextContains(gaps,text[i]);i++);
		for (sc=i;text[i];i++) {
			if (TextContains(gaps,text[i])) {
				while(text[i]&&TextContains(gaps,text[i+1]))i++;
				if (++g==bzIndex)sc=i+1;
			} else if (g==bzIndex) {
				while(text[i]&&!TextContains(gaps,text[i]))i++;
				ec = i;
				break;
			}
		}
		return SubStr(text, sc, ec);
	};
public:
	int Numerator;
	int Denominator;
	int count; // Whole numbers
	fraction() {
		count = 0;
		Numerator = 0;
		Denominator = 1;
	}
	fraction(int v) {
		count = v;
		Numerator = 0;
		Denominator = 1;
	}
	fraction(int v, int n, int d) {
		Numerator = n;
		Denominator = d;
		if (Denominator == 0) {
			Numerator = 0;
			Denominator = 1;
		}
		count =  (Numerator > Denominator ? 
				  Numerator / Denominator :
				  0) + v;
		if (count != 0) {
			Numerator -= (Numerator / Denominator) * Denominator;
		}
		Shrink();
	}
	fraction(int n, int d) {
		Numerator = n;
		Denominator = d;
		if (Denominator == 0) {
			Numerator = 0;
			Denominator = 1;
		}
		count =  Numerator > Denominator ? 
				 Numerator / Denominator :
			0;
		if (count != 0) {
			Numerator -= (Numerator / Denominator) * Denominator;
		}
		Shrink();
	}
	fraction(double fraction2) {
		register int count = (int) fraction2;
		register unsigned char decimalCount = 0;
		register int den = 1;
		while (ceil(fraction2)!=fraction2) {
			decimalCount++;
			fraction2 *= 10;
			den *= 10;
		}
		fraction2 -= (count * den);
		if (den==0)den=1;
		*this = fraction(count, (int) fraction2, den);
		Shrink();
	}
	fraction(char *str) {
		Parse(str);
	}
	bool Shrink() {
		bool sucessful = 0;
		for (int i = std::min(Numerator, Denominator); i; i--) {
			if ((Numerator % i == 0) && (Denominator % i == 0)) {
				Numerator /= i;
				Denominator /= i;
				sucessful = 1;
			}
		}
		return sucessful;
	}
	fraction operator =(fraction &fraction2) {
		count = fraction2.count;
		Denominator = fraction2.Denominator;
		Numerator = fraction2.Numerator;
		return *this;
	}
	fraction operator =(int fraction2) {
		count = fraction2;
		Denominator = 1;
		Numerator = 0;
		return *this;
	}
	fraction operator =(double fraction2) {
		*this = fraction(fraction2);
	}
	fraction operator =(char *str) {
		Parse(str);
	}
	fraction operator +(fraction &fraction2) {
		int n1 = Numerator + (count * Denominator);
		int d1 = Denominator;
		int n2 = fraction2.Numerator + (fraction2.count * fraction2.Denominator);
		int d2 = fraction2.Denominator;
		int MatchedDenominator = d1 * d2;
		fraction f(
			((MatchedDenominator / d1) * n1) + 
			((MatchedDenominator / d2) * n2)
			,MatchedDenominator);
		f.Shrink();
		return f;
	}
	fraction operator -(fraction &fraction2) {
		int n1 = Numerator + (count * Denominator);
		int d1 = Denominator;
		int n2 = fraction2.Numerator;
		int d2 = fraction2.Denominator + (fraction2.count * fraction2.Denominator);
		int MatchedDenominator = d1 * d2;
		fraction f(
			((MatchedDenominator / d1) * n1) - 
			((MatchedDenominator / d2) * n2)
			,MatchedDenominator);
		f.Shrink();
		return f;
	}
	fraction operator *(fraction &fraction2) {
		int n1 = Numerator + (count * Denominator);
		int d1 = Denominator;
		int n2 = fraction2.Numerator + (fraction2.count * fraction2.Denominator);
		int d2 = fraction2.Denominator;
		fraction f(n1 * n2, d1 * d2);
		f.Shrink();
		return f;
	}
	fraction operator /(fraction &fraction2) {
		int n1 = Numerator + (count * Denominator);
		int d1 = Denominator;
		int n2 = fraction2.Numerator + (fraction2.count * fraction2.Denominator);
		int d2 = fraction2.Denominator;
		fraction f(n1 * d2, d1 * n2);
		f.Shrink();
		return f;
	}
	/* Overload operators with double */
	fraction operator +(double fraction2) {
		return (*this + fraction(fraction2));
	}
	fraction operator -(double fraction2) {
		return (*this - fraction(fraction2));
	}
	fraction operator *(double fraction2) {
		return (*this * fraction(fraction2));
	}
	fraction operator /(double fraction2) {
		return (*this / fraction(fraction2));
	}
	/*   +=  -=  /=  *=    */
	fraction operator +=(double fraction2) {
		*this = *this + fraction(fraction2);
		return *this;
	}
	fraction operator -=(double fraction2) {
		*this = *this - fraction(fraction2);
		return *this;
	}
	fraction operator *=(double fraction2) {
		*this = *this * fraction(fraction2);
		return *this;
	}
	fraction operator /=(double fraction2) {
		*this = *this / fraction(fraction2);
		return *this;
	}
	double Value() {
		return (((double)Numerator+((double)count*(double)Denominator)) / (double)Denominator);
	}
	void Parse(char *str) {
		register char c = 0; // '/' count
		for (register int i = 0; str[i]; i++, c+=(char)(str[i]=='/'));
		if (c == 1) {
			fraction fract(
				atoi(GetWord(str, " /", 0)),
				atoi(GetWord(str, " /", 1))
			);
			*this = fract;
		} // 1
		else if (c == 2) {
			fraction fract(
				atoi(GetWord(str, " /", 0)),
				atoi(GetWord(str, " /", 1)),
				atoi(GetWord(str, " /", 2))
			);
			*this = fract;
		} // 2
		else *this = 0;
	}
	#ifdef _CONSOLE
	void Display() {
		if (Numerator != 0) {
			if (count != 0) {
				std::cout << count << '/' << Numerator << '/' << Denominator;
			} else {
				std::cout << Numerator << '/' << Denominator;
			}
		} else {
			std::cout << count;
		}
	}
	friend std::ostream& operator <<(std::ostream& out, fraction &f) {
		f.Display();
		return out;
	}
	friend std::istream& operator >>(std::istream& in, fraction &f) {
		char *str = new char[25];
		std::cin.getline(str,25);
		register char c = 0; // '/' count
		for (register int i = 0; str[i]; i++, c+=(char)(str[i]=='/'));
		if (c == 1) {
			fraction fract(
				atoi(GetWord(str, " /", 0)),
				atoi(GetWord(str, " /", 1))
			);
			f = fract;
		} // 1
		else if (c == 2) {
			fraction fract(
				atoi(GetWord(str, " /", 0)),
				atoi(GetWord(str, " /", 1)),
				atoi(GetWord(str, " /", 2))
			);
			f = fract;
		} // 2
		else f = 0;
		return in;
	}
	#endif
}; // (UNSIGNED)
int main() {
	fraction f = "3/33/99";
	f *= 3.1;
	f /= 2.3;
	std::cout << f;
	std::cin.ignore();
	return 0;
}

i had to write a program to solve a fraction e.g
if i input 21/6 theen it will give output 7/2.
can anyone help me.?

you should check this out:

// Fractional Calculator in C++

#include<iostream>
using namespace std;

struct fraction
{
    int n,d;
    int n1,d1;

};

int main()
{
    char p;
    fraction opr;

    cout<<"enter nomerator of first fraction : ";
    cin>>opr.n;
    cout<<endl;
    cout<<"enter demonerator of first fraction";
    cin>>opr.d;
    cout<<endl;
    cout<<"enter operation : ";
    cin>>p;
    cout<<endl;

    cout<<"enter nomerator of second fraction : ";
    cin>>opr.n1;
    cout<<endl;
    cout<<"enter denomerator of second fraction : ";
    cin>>opr.d1;
    cout<<endl;

    if(p=='+')
    {
        int z = (opr.n * opr.d1 + opr.d * opr.n1 );
        int y = (opr.d * opr.d1);
        cout<<z<<"/"<<y<<endl;

    }

    if(p=='-')
    {
        int a = (opr.n * opr.d1 - opr.d * opr.n1 );
        int b = (opr.d * opr.d1);
        cout<<a<<"/"<<b<<endl;

    }
    return 0;

}

- See more at: http://www.programmingtunes.com/fractional-calculator-using-c/

check this out:

<blockquote>[cpp]

// Fractional Calculator in C++

#include<iostream>
using namespace std;

struct fraction
{
    int n,d;
    int n1,d1;

};

int main()
{
    char p;
    fraction opr;

    cout<<"enter nomerator of first fraction : ";
    cin>>opr.n;
    cout<<endl;
    cout<<"enter demonerator of first fraction";
    cin>>opr.d;
    cout<<endl;
    cout<<"enter operation : ";
    cin>>p;
    cout<<endl;

    cout<<"enter nomerator of second fraction : ";
    cin>>opr.n1;
    cout<<endl;
    cout<<"enter denomerator of second fraction : ";
    cin>>opr.d1;
    cout<<endl;

    if(p=='+')
    {
        int z = (opr.n * opr.d1 + opr.d * opr.n1 );
        int y = (opr.d * opr.d1);
        cout<<z<<"/"<<y<<endl;

    }

    if(p=='-')
    {
        int a = (opr.n * opr.d1 - opr.d * opr.n1 );
        int b = (opr.d * opr.d1);
        cout<<a<<"/"<<b<<endl;

    }
    return 0;

}
[/cpp]</blockquote>
This question has already been answered. Start a new discussion instead.