title: C++: Mathematical error when adding two polynomials
body: <p>I'm in the process of writing a Polynomial Class and have overloaded the + operator. I asked a question a few days ago regarding the actual overloading process, and received a great answer. However, my question now is in regards to the actual function of my program. </p>
  
  <p>I need to add two user entered polynomials together. When one adds polynomials one must only add like terms, even if the two polynomials differ in size. My program does ok if the first entered polynomial is larger than the second, but reaches an error if the first polynomial is smaller than the second. To be more specific... I've noticed that it seems to leave one term off. On top of that it seems to add two like terms together and then print it with a different exponent. For example :
  2x^3 + 3x^3 = 5x^2.</p>
  
  <p>For reference, this is my previous question regarding this program: <a href="https://stackoverflow.com/questions/15395924/overloading-operator-with-classes-containing-array-pointers-c/15395971?noredirect=1#comment21764261_15395971">Overloading + operator with classes containing array pointers (C++)</a></p>
  
  <p>Some help would be greatly appreciated.</p>
  
  <p>My Code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;stdexcept&gt;
  using namespace std;
  
  #ifndef POLYNOMIAL_H
  #define POLYNOMIAL_H
  
  class Polynomial 
  {
        friend istream&amp; operator&gt;&gt;(istream&amp; in, Polynomial&amp; p);
        friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Polynomial&amp; p);
        public:
               Polynomial(int = 10);
               ~Polynomial();
               void assignExponent();
               Polynomial operator+(const Polynomial&amp; other);
  
        private:
                int SIZE;
                int *exponents;
                int *polyPtr; 
  };
  
  #endif
  
  //CONSTRUCTOR
  Polynomial::Polynomial(int arraySize)
  {
       if(arraySize &gt; 0)
                    SIZE = arraySize;
       else
       {
           cout &lt;&lt; "Array size must be greater than 0. Program will now "
                &lt;&lt; "Terminate..." &lt;&lt; endl;
  
           system("pause");
           exit(0);
       }
  
       polyPtr = new int[SIZE]; 
       exponents = new int[SIZE];
  
       for(int i = 0; i&lt;SIZE; i++)
               polyPtr[i] = 0;
  
       assignExponent();
  };
  
  //DESTRUCTOR
  Polynomial::~Polynomial() 
  {
       delete [] polyPtr;                         
  };
  
  //STREAM INSERTION
  istream&amp; operator&gt;&gt;(istream&amp; in, Polynomial&amp; p)
  {
           for(int i = 0; i&lt;p.SIZE; i++)
           {
                 in &gt;&gt; p.polyPtr[i];         
           }
  
           return in;
  };
  
  //STREAM EXTRACTION
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const Polynomial&amp; p)
  {
           int exponent;
           for(int i = 0; i&lt;p.SIZE; i++)
           {
                 exponent = (p.SIZE - 1) - i;
  
                 if(p.polyPtr[i] != 1)
                 {  
                    if(exponent &gt; 0 &amp;&amp; exponent != 1)
                             out &lt;&lt; p.polyPtr[i] &lt;&lt; "x^" &lt;&lt; exponent &lt;&lt; " + ";  
  
                    if(exponent == 1)
                             out &lt;&lt; p.polyPtr[i] &lt;&lt; "x" &lt;&lt; " + ";
  
                    if(exponent == 0)
                             out &lt;&lt; p.polyPtr[i];  
                 }
  
                 //In order to not display coefficient if = 1
                 else
                 {
                     if(exponent &gt; 0 &amp;&amp; exponent != 1)
                             out &lt;&lt; "x^" &lt;&lt; exponent &lt;&lt; " + ";  
  
                     if(exponent == 1)
                             out &lt;&lt; "x" &lt;&lt; " + ";
  
                     if(exponent == 0)
                             out &lt;&lt; p.polyPtr[i];
                 }    
           }       
  
           return out;
  }; 
  
  //Assigns a value for exponent
  void Polynomial::assignExponent()
  {
       for(int i = 0; i&lt;SIZE; i++)
       {
               exponents[i] = (SIZE - 1) - i;        
       }
  };
  
  //OVERLOAD OF + OPERATOR
  Polynomial Polynomial::operator+(const Polynomial&amp; other)
  {
           Polynomial sum(SIZE);
           int difference;
  
           //If the first polynomial is larger
           if (SIZE &gt; other.SIZE)
           {
              difference = SIZE - other.SIZE;
  
              for(int i = 0; i&lt;SIZE; i++)
              {
                   if(i - difference &lt; 0)
                        sum.polyPtr[i] = polyPtr[i];
  
                   else
                   {
                       sum.polyPtr[i] = polyPtr[i] + 
                                    other.polyPtr[i - difference];  
                   }                            
              }         
           }
  
           //If the second polynomial is larger       **PROBLEM***************************************  
           if(other.SIZE &gt; SIZE)
           {
              difference = other.SIZE - SIZE;
  
              for(int i = 0; i&lt;other.SIZE; i++)
              {
                   if(i - difference &lt; 0)
                        sum.polyPtr[i] = other.polyPtr[i];
  
                   else
                   {
                       sum.polyPtr[i] = other.polyPtr[i] + 
                                    polyPtr[i - difference];  
                   }                            
              }         
           }
  
           //If the polynomials are equal
           if(SIZE == other.SIZE)     
           {
                    for(int i = SIZE-1; i &gt;= 0; i--)
                    {
                            sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];        
                    }   
           }
  
           return sum;
  };
  
  int main()
  {
      int polySize;
  
      //User enters a size for the first &amp; second polynomial
      cout &lt;&lt; "Enter a size for the first polynomial: ";
      cin &gt;&gt; polySize;
      Polynomial pOne(polySize);
  
      cout &lt;&lt; "\nEnter a size for the second polynomial: ";
      cin &gt;&gt; polySize;
      Polynomial pTwo(polySize);
  
      //User enters in values (Overload of &gt;&gt; operator
      cout &lt;&lt; "\n\nEnter in values for the first polynomial, "
           &lt;&lt; "in the format - (x x x x): " &lt;&lt; endl;
      cin &gt;&gt; pOne;
      cout &lt;&lt; "\nEnter in values for the second polynomial, "
           &lt;&lt; "in the format - (x x x x): " &lt;&lt; endl;
      cin &gt;&gt; pTwo;
  
      //Overload &lt;&lt; operator for output
      cout &lt;&lt; "\nPolynomial 1 is: " &lt;&lt; pOne &lt;&lt; endl
           &lt;&lt; "Polynomial 2 is: " &lt;&lt; pTwo &lt;&lt; endl;
  
      Polynomial pThree = pOne + pTwo;
  
      cout &lt;&lt; "\nAfter being added together, the new polynomial is: "
           &lt;&lt; pThree &lt;&lt; endl;
  
   system("pause");   
  }
  </code></pre>
  
  <p>Output Window:
  <img src="https://i.stack.imgur.com/Qdqpq.png" alt="enter image description here"></p>