title: Issues with polynomial class c++
body: <p>I'm working on a polynomial class that basically does +,-,*,/, and evaluates polynomials. I keep running into bugs (specifically output is incorrect) and I think it's because of one of my operation methods (maybe addition??).</p>
  
  <p><em><strong>EDIT</em></strong>: Narrowed down problem to +() operator. It cannot add polynomials and a double. </p>
  
  <p>Any help would be appreciated, please! </p>
  
  <p>Polynomial Class CPP:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "polynomial.h"
  
  using namespace std;
  
  /*
     =======================
         Constructors
     =======================
  */
  
  Polynomial::Polynomial() //default constructor
     {
        for ( int i = 0; i &lt; 20; i++ )
        {
           coefs[i] = 0;
        }
     }
  
  
  Polynomial::~Polynomial() {}
  void Polynomial::set(int coef, int pwr){
      coefs[pwr] = coef;
      pwrs = degree();
  }
  
  int Polynomial::degree()
     {
        int d = 0;
        for ( int i = 0; i &lt; 20; i++ )
           if ( coefs[i] != 0 ) d = i;
        return d;
     }
  
  /* 
     =======================
          operator=
     =======================
  */
  
  Polynomial&amp; Polynomial::operator= ( const Polynomial&amp; poly )
  {
        if ( this == &amp;poly ) return ( *this ) ;
        else{
            for (int i = 0; i &lt; 20; i++)
                coefs[i] = poly.coefs[i];
        }
        return ( *this );
  }
  
  /* 
     =======================
          operator+
     =======================
  */
  
  Polynomial operator+(const Polynomial&amp; a, const Polynomial&amp; b )
  {
      Polynomial c;
      for ( int i = 0; i &lt;= a.pwrs; i++ ) c.coefs[i] += a.coefs[i];
      for ( int i = 0; i &lt;= b.pwrs; i++ ) c.coefs[i] += b.coefs[i];
      c.pwrs = c.degree();
      return c;
  }
  
  Polynomial operator+(const Polynomial&amp; a, const double&amp; d)
  {
      Polynomial c;
      for ( int i = 0; i &lt;= a.pwrs; i++ ){
          if(i == a.pwrs) {
              i=i+1;
              c.coefs[i] = d;
          }
          c.coefs[i] += a.coefs[i];
      }
      c.pwrs = c.degree();
      return c;
  }
  
  /*
     =======================
         Operator-
     =======================
  */
  
  Polynomial operator- (const Polynomial&amp; a,const Polynomial&amp; b )
  {
      //Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;
      for ( int i = 0; i &lt;= a.pwrs; i++ ) c.coefs[i] += a.coefs[i];
      for ( int i = 0; i &lt;= b.pwrs; i++ ) c.coefs[i] -= b.coefs[i];
      c.pwrs = c.degree();
      return c;
  }
  
  /*
     =======================
         Operator*
     =======================
  */
  
  Polynomial operator* (const Polynomial&amp; a, const Polynomial&amp; b)
  {
      //Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;
      for ( int i = 0; i &lt;= a.pwrs; i++ )
          for ( int j = 0; j &lt;= b.pwrs; j++ )
              c.coefs[i+j] += ( a.coefs[i] * b.coefs[j] );
      c.pwrs = c.degree();
      return c;
     }
  
  Polynomial operator*(const Polynomial&amp; poly1, const double&amp; d)
  {
      Polynomial poly;
      for(int i = 0; i &lt; 20; i++)
          poly.coefs[i] = poly1.coefs[i] * d;
      poly.pwrs = poly1.pwrs;
      return poly;
  }
  
  Polynomial operator*(const double&amp; d, const Polynomial&amp; poly1)
  {
      Polynomial poly;
      for(int i = 0; i &lt; 20; i++)
          poly.coefs[i] = d * poly1.coefs[i];
      poly.pwrs = poly1.pwrs;
      return poly;
  }
  
  /*
     =======================
         Operator/
     =======================
  */
  
  Polynomial operator/ (const Polynomial&amp; a, const Polynomial&amp; b)
  {
      Polynomial c;
      for ( int i = 0; i &lt;= a.pwrs; i++ )
          for ( int j = 0; j &lt;= b.pwrs; j++ )
              c.coefs[i+j] += ( a.coefs[i] / b.coefs[j] );
      c.pwrs = c.degree();
      return c;
     }
  
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const Polynomial&amp; p) {
      for ( int i = 19; i &gt;= 0; i-- ) {
          if(p.pwrs &gt; 1){
              if ( p.coefs[i] != 0 ) {
                  cout &lt;&lt; p.coefs[i] &lt;&lt; "x^" &lt;&lt; i &lt;&lt; " ";
                  if(p.coefs[i-1] &gt; 0)
                      cout &lt;&lt; "+";
                  else if(p.coefs[i-1] &lt; 0)
                      cout &lt;&lt; "";
              }
          }
          else if (p.pwrs == 1)
              cout &lt;&lt; p.coefs[i] &lt;&lt; "x ";
          else if(p.pwrs == 0)
              cout &lt;&lt; p.coefs[i];
      }
      cout &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "polynomial.h"
  #include "monomial.h"
  
  using namespace std;
  
  int main(){
  Polynomial a, b, c, d, e,result;
      a.set(1,3); //x^3
      b.set(1,2); //x^2
      c.set(6,1); //6x
      d.set(0.01,10); //(1/100)x^10
      e.set(2,5); //2x^5 
      result = 4*(a+b)*(c+1)*(d-e); // 4 * (x^3+x^2) * (6x+1) * ((1/100)x^10 - 2x^5)
  }
  </code></pre>