title: C++ overloading * for polynomial multiplication
body: <p>So I have been developing a polynomial class where a user inputs: 1x^0 + 2x^1 + 3x^2... and 1,2,3 (the coefficients) are stored in an int array  </p>
  
  <p>My overloaded + and - functions work, however, * doesnt work. No matter the input, it always shows -842150450<br>
  when is should be (5x^0 + x^1) * (-3x^0 + x^1) = <strong>-15x^0 + 2x^1 + 1x^2</strong><br>
   or (x+5)(x-3) = x^2 +2x - 15</p>
  
  <p>I'm using the overloaded * function like : <code>Polynomial multiply = one * two;</code><br>
  Im guessing the problem is strtol(p, &amp;endptr, 10) since it uses a long int, however, adding and subtracting works perfectly </p>
  
  <p>My constructor</p>
  
  <pre><code>Polynomial::Polynomial(char *s)
  {
      char *string;
      string = new char [strlen(s) + 1];
      int length = strlen(string);
      strcpy(string, s);
  
      char *copy;
      copy = new char [length];
      strcpy(copy, string);
  
      char *p = strtok(string, "  +-");
      counter = 0;
      while (p) 
      {
          p = strtok(NULL, "  +-");
          counter++;
      }
  
      coefficient = new int[counter];
  
      p = strtok(copy, "  +");
      int a = 0;
      while (p)
      {
          long int coeff;
          char *endptr;
          coeff = strtol(p, &amp;endptr, 10); //stops at first non number
          if (*p == 'x')
             coeff = 1;
  
          coefficient[a] = coeff;
          p = strtok(NULL, "  +");
          a++;
      }
  }
  </code></pre>
  
  <p>and the overloaded * function</p>
  
  <pre><code>Polynomial Polynomial::operator * (const Polynomial &amp;right)
  {
      Polynomial temp;
  
      //make coefficient array
      int count = (counter + right.counter) - 1;
      temp.counter = count;
      temp.coefficient = new int [count];
      for (int i = 0; i &lt; counter; i++)
      {
          for (int j = 0; j &lt; right.counter; j++)
              temp.coefficient[i+j] += coefficient[i] * right.coefficient[j];
      }
      return temp;
  }
  </code></pre>
  
  <p>And heres my entire code: <a href="http://pastie.org/721143" rel="nofollow noreferrer">http://pastie.org/721143</a></p>