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]

//GOYO
#include <iostream>
 using namespace std;
  
 class fraction
 {
 private:
     int numerator;
     int denominator;
 public:
     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]

[Fraction.ccp]

#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;
  }///end
  
  
 ostream& operator<<(ostream& out, fraction& f)
 {
     out << f.numerator << "/" << f.denominator;
     return out;
 }///end
  
 istream& operator >> (istream& s, fraction& f)
 {
     cout << "\nPlease type the numerator: ";
     cin >> f.numerator;
     cout << "Please type the denominator: ";
     cin >> f.denominator;
     return s;
 }///en

[end Fraction.cpp]

[Main.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')
             exit(0);
  
      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;
          break;
  
    
  
    return 0;
	}
 }

[Main.cpp]

where do you call simplify?

Member Avatar for iamthwee

And use Euclid's GCD.

And what for friend arith operators?

fraction operator+(const fraction& f) const;
...

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

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.