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;
}

You might have correct abit of that ^^, but its the right idea. My maths isn't that amazing.

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:

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>
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.