hey I am currently working on a Fraction program and the fraction needs to be in lowest terms i have been working on this for a while and I can't figure out what is wrong with it i think i have been looking at it for too long, but anyways maybe someone else can see my mistake or any other advice on what should be changed
Thanks in Advance.

[Header file]

#include <iostream>
 using namespace std;
 class fraction
     int numerator;
     int denominator;
     fraction() : numerator(0), denominator(0)  //Constructor (no args) 
     { }
     fraction(int num, int den=1); //(Fraction F2) : numerator(num), denominator(den) //Constructor (two args)
     friend fraction operator +(fraction f1, fraction f2);
     //friend fraction operator-(fraction f1, fraction f2);
     //friend fraction operator*(fraction f1, fraction f2);
     //friend fraction operator/(fraction f1, fraction f2);
     friend istream& operator >> (istream& s, fraction& e);
     friend ostream& operator<< (ostream& out, fraction& f);
     int GreatestCommonDivisor(int n, int d);
     void Simplify()
     int gcd, absNum = abs(numerator);
     if(numerator != 0 && absNum != 1 && denominator != 1) {
         gcd = GreatestCommonDivisor(absNum, denominator);
         if(gcd > 1) {
             numerator = numerator / gcd;
             denominator = denominator / gcd;

[end header]


#include <iostream>
 #include <string>
 #include "Fraction.h"
 using namespace std;
 fraction operator+(fraction f1, fraction f2)
     fraction temp;
     temp.numerator = f1.numerator * f2.denominator + f1.denominator * f2.numerator;
     temp.denominator = f1.denominator * f2.denominator;
     return temp;
 ostream& operator<<(ostream& out, fraction& f)
     out << f.numerator << "/" << f.denominator;
     return out;
 istream& operator >> (istream& s, fraction& f)
     cout << "\nPlease type the numerator: ";
     cin >> f.numerator;
     cout << "Please type the denominator: ";
     cin >> f.denominator;
     return s;

[end Fraction.cpp]


#include <iostream>
 #include <string>
 #include "fraction.h"                                                                                                             
 using namespace std;
 int main() 
     fraction f1;
     fraction f2;
     fraction answer;
     fraction f3;
     char symbol = ' ';
     string nome;
    while (true)
     cout << "A\tADD\n";
     cout << "S\tSUBTRACT\n";
     cout << "M\tMULTIPLY\n";
     cout << "D\tDIVIDE\n";
     cout << "E\tEXIT\n";
     cout << "\nWhat operation do you want to use? ";
         cin >> symbol;
         if (symbol == 'e' || symbol == 'E')
      switch (symbol) 
      case 'A':
          cout << "\nPlease type values for fraction 1 and fraction 2\n";
          cin >> f1 >> f2;
          cout << "\nFraction 1 = " << f1 << "\nFraction 2 = " << f2;
          f3 = f1 + f2;
          cout << "\n\bThe sum of F1 and F2 is: " << f3 << "\n" << endl;
    return 0;


8 Years
Discussion Span
Last Post by goyofoyo

Line 11: Do you expect, for a fraction, denominator to be zero?

As per for the simplify(), It is simple:
1.Write a function that calculates the gcd of two number
2. Divide numerator and denominator by that GCD

For finding GCD use the Euclidean Algorithm


alright i will try the Euclidean algorithm
we will see how it goes

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.