Hello friends, I'm working on a program that takes a number input from the user and recursively sums the number 'n' to one. For example, inputting a 5 would sum 1/5 + 1/4 + 1/3+ 1/2 + 1/1. I have sucessfully gotten the fraction sequence to display but I am having trouble with the sum.
Here is my client program

#include "Fraction.cpp"
#include<iostream>
using namespace std;
Fraction SumOfSeries(int n);//adds fractions from num to 1
int main()
{
    int num;
    cout<<"Enter a number: ";
    cin>>num;
    cout<<"Sum of Series-> ";
    SumOfSeries(num);
    char wait;
    cout<<"\nEnter any key to end ";
    cin>>wait;
    return 0;
} 
Fraction SumOfSeries(int n)
{
    Fraction base(1,1);
    Fraction temp(1,n);
    if(n==1)
            {
                       base.Display();
                       return base;
            }
    else
        {
            for(int i=1; i<=n; i++)
            {
            base=base+temp;
            temp.Display();
            if(i!=n)
            cout<<" + ";
            return temp+SumOfSeries(n-1); 
            } 
        }      

}

Here is my fraction class (you probably don't need to spend too much time looking at this.

#include <iostream>
using namespace std;
class Fraction
{public:
        Fraction();
        Fraction(int n, int d);
        void Assign(int n, int d);
        Fraction Recip();
        void Display();
        int GetNum();
        int GetDen();
        double Convert();
        Fraction operator+(Fraction f);
        Fraction operator*(Fraction f);
        Fraction operator-(Fraction f);
        Fraction operator/(Fraction f);
        bool operator>(Fraction f);
        bool operator<(Fraction f);
        bool operator==(Fraction f);
        void Reduce();
        int GCF();
        friend ostream&operator<<(ostream&outs, const Fraction f);
        
 private:
        int num, den;
 };

Fraction::Fraction()
{}
                
void Fraction::Assign(int n,int d)
{
     num=n;
     den=d;
}
void Fraction::Display()
{
     cout<<num<<"/"<<den;
}
Fraction Fraction::Recip()
{
         Fraction temp(den,num);
         return temp;
}
int Fraction::GetNum()
{
    return num;
}
int Fraction::GetDen()
{
    return den;
}

Fraction::Fraction(int n, int d)
{
                       if (d!=0)
                       {num=n; den=d;}
                       else cout<<"error: fraction is undefined\n";
}         
Fraction Fraction::operator+(Fraction f)
{
           Fraction temp;
           temp.num=num*f.den+f.num*den;
           temp.den=den*f.den;
           temp.Reduce();
           return temp;
}
Fraction Fraction::operator*(Fraction f)
{
         Fraction temp;
         temp.num=num*f.num;
         temp.den=den*f.den;
         temp.Reduce();
         return temp;
}
Fraction Fraction::operator-(Fraction f)
{
         Fraction temp;
         temp.num=num*f.den-f.num*den ;
         temp.den=den*f.den;
         temp.Reduce();
         return temp;
}
Fraction Fraction::operator/(Fraction f)
{
         Fraction temp;
         temp.num=num*f.den;
         temp.den=den*f.num;
         temp.Reduce();
         return temp;
}
bool Fraction::operator>(Fraction f)
{
         bool temp;
         if(Convert()>f.Convert())
         {temp=true;}
         else
         {temp=false;}
         return temp;
}
bool Fraction::operator<(Fraction f)
{
         bool temp;
         if(Convert()<f.Convert())
         {temp=true;}
         else
         {temp=false;}
         return temp;
}
bool Fraction::operator==(Fraction f)
{
         bool temp;
         if(Convert()==f.Convert())
         {temp=true;}
         else
         {temp=false;}
         return temp;
}
void Fraction::Reduce()
{
     int gcf=GCF();
     num=num/gcf;
     den=den/gcf;
}
int Fraction::GCF()
{
     int gcf;
     bool done=false;
     if(num<=den)
     gcf=num;
     else gcf=den;
     while(gcf>1&&!done)
     {
                        if(num%gcf==0&&den%gcf==0)
                        done=true;
                        else gcf--;
                        }
                        return gcf;
}
double Fraction::Convert()
{
         double deci;
         deci=static_cast<double>(num)/den;
         return deci;
}

ostream&operator<<(ostream&outs, const Fraction f)
{
                                 outs<<f.num<<"/"<<f.den;
                                 return outs;
}

Thanks for any help.

Firstly you are returning a Fraction type but not stroring or displaying it.

Fraction frac=SumOfSeries(num);
frac.display;

Secondly in Function SumOfSeries Why are you introducing a for loop?

#include "Fraction.cpp"
#include<iostream>
using namespace std;

Fraction SumOfSeries(int n);//adds fractions from num to 1
int main()
{
    int num;
    cout<<"Enter a number: ";
    cin>>num;
    cout<<"Sum of Series-> ";
    Fraction frac=SumOfSeries(num);
    frac.Display();	
    char wait;
    cout<<"\nEnter any key to end ";
    cin>>wait;
    return 0;
} 
Fraction SumOfSeries(int n)
{
    Fraction base(1,1);
    Fraction temp(1,n);
    if(n==1)
            {
                       return base;
            }
    else
        {
            return temp+SumOfSeries(n-1); 
        }      

}

This code returns the correct output.
All you need to do is write a print function which writes down... all the numbers such as

writenumbers(int n)
{
for(int i=1; i<=n; i++)
            {
            Fraction temp(1,n);
            temp.Display();
            if(i!=n)
            cout<<" + "; 
            }
}

Thanks for the reply but I need to use recursion (i.e. the function has to call itself) to solve this problem.

Sorry, I followed your first piece of advice and got the sum to display. Thanks alot for the help.

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.