title: C++ Equations with Polynomials
body: <p>I am having a problem dealing with a project I made for myself. I am rather inexperienced with C++ and programming in general so my code may not be the most elegant or efficient. The objective is to add, subtract, or multiply two polynomials together that are being read in from a file. There is an unknown amount of these equations, but for now I am dealing with one. </p>
  
  <ul>
  <li>The equations are in the form of (2*x^2+3*x^4+5*x+6)+(1*x^5+4*x^2+7*x).</li>
  <li>This is the equation I am using for testing.</li>
  <li>The program is outputting 6x^2+6x+2x^2+3x.</li>
  </ul>
  
  <p>I have been working on this for a while now today, but I cannot seem to find where the problem is. My main problem is the program won't output the correct answer, even though it gets the first term correct. I know you guys don't like code-dumping, but I'm not sure how else we'll be able to find the problem. Thanks for any help. </p>
  
  <pre><code>    string multiplication(double polyArray0[MAX_SIZE][2], double polyArray1[MAX_SIZE][2], int counter = 0, int iteration = 0)
      {
          int updated = (counter + 1);
          string line;
  
          for(int i = 0; i &lt; counter; ++i)
          {
              for(int n = 0; n &lt; iteration; ++n)
              {
                  polyArray0[i][0] *= polyArray1[n][0];
                  polyArray0[i][1] += polyArray1[n][1];
              }
          }
  
          line = counter;
  
          for(int i = 0; i &lt; counter; ++i)
          {
              line += polyArray0[updated][0];
              line += polyArray0[updated][1];
          }
  
          return line;
      }
  
      string addition(double polyArray0[MAX_SIZE][2], double polyArray1[MAX_SIZE][2], int counter = 0, int iteration = 0)
      {
          double polyArray2[MAX_SIZE][2];
          int updated = 0;
          bool additionTo;
          string line;
  
          for(int i = 0; i &lt; counter; ++i)
          {
              additionTo = false;
  
              for(int n = 0; n &lt; iteration; ++n)
              {
                  if(polyArray0[i][1] == polyArray1[n][1])
                  {
                      polyArray0[i][0] += polyArray1[n][0];
                      additionTo = true;
                  }
              }
  
              if(!additionTo)
              {
                  polyArray2[updated][0] = polyArray0[i][0];
                  polyArray2[updated][1] = polyArray0[i][1];
                  ++updated;
              }
          }
  
          line = static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; (updated + counter)))-&gt;str();
  
          for(int i = 0; i &lt; counter; ++i)
          {
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray0[i][0]))-&gt;str();
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray0[i][1]))-&gt;str();
          }
  
          for(int i = 0; i &lt; updated; ++i)
          {
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray2[i][0]))-&gt;str();
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray2[i][1]))-&gt;str();
          }
  
          return line;
      }
  
      string subtraction(double polyArray0[MAX_SIZE][2], double polyArray1[MAX_SIZE][2], int counter = 0, int iteration = 0)
      {
          double polyArray2[MAX_SIZE][2];
          int updated = counter;
          string line;
          bool additionTo;
  
          for(int i = 0; i &lt; iteration; ++i)
          {
              polyArray1[i][0] *= -1;
          }
  
          for(int i = 0; i &lt; counter; ++i)
          {
              additionTo = false;
  
              for(int n = 0; n &lt; iteration; ++n)
              {
                  if(polyArray0[i][1] == polyArray1[n][1])
                  {
                      polyArray0[i][0] += polyArray1[n][0];
                      additionTo = true;
                  }
              }
  
              if(!additionTo)
              {
                  polyArray2[updated][0] = polyArray0[i][0];
                  polyArray2[updated][1] = polyArray1[i][1];
                  ++updated;
              }
          }
  
          line = static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; (updated + counter)))-&gt;str();
  
          for(int i = 0; i &lt; counter; ++i)
          {
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray0[i][0]))-&gt;str();
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray0[i][1]))-&gt;str();
          }
  
          for(int i = 0; i &lt; updated; ++i)
          {
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray2[i][0]))-&gt;str();
              line += static_cast&lt;ostringstream*&gt;(&amp;(ostringstream() &lt;&lt; polyArray2[i][1]))-&gt;str();
          }
  
          return line;
      }
  int main()
  {
      //
      // Variables :
      //  polyArray0 : used to store the left side of the equation
      //  polyArray1 : used to store the right side of the equation
      //  updated    : a counter used to ensure that the bounds of the arrays are not being overstepped
      //  counter    : a counter to find the length of a polynomial
      //  iteration  : see updated
      //  startPosition/endPosition : used to find the length of each polynomial inside of the parenthesis
      //  number     : number being converted into the array
      //  newPolynomial : will store the answer in a string
      //  polyInfo   : the string contained within the file
      //  add        : check to see if the program will be using the addition function
      //  subtract   : check to see if the program will be using the subtraction function
      //  multiply   : check to see if the program will be using the multiplication function
      //
      double polyArray0[MAX_SIZE][2];
      double polyArray1[MAX_SIZE][2];
      double updated;
      int counter = 0;
      int iteration = 0;
      int startPosition;
      int endPosition;
      string number;
      string newPolynomial;
      string polyInfo;
      string polynomial0;
      string polynomial1;
      string polynomial2;
      bool polynomial;
      bool adding;
      bool subtracting;
      bool multiplying;
  
      //
      // This will read in file. The file must be located in the same folder as the .cpp file.
      //
      ifstream polyFile;
      polyFile.open("functions.txt");
  
      //
      // Use an if statement to make sure the file is opened and then retrieve the information and store it
      // in a string called "polyFile."
      //
      if(polyFile.good())
      {
          getline(polyFile, polyInfo);
      }
  
      else
      {
          cout &lt;&lt; "The file could not be open." &lt;&lt; endl;
      }
  
      polynomial = true;
  
      //
      // Use a for loop to search through the string and find the math operator located
      // between ")" and "(."
      //
      for(int i = 0; i &lt; polyInfo.length(); ++i)
      {
          if(polyInfo.substr(i, 1) == "(")
          {
              startPosition = (i + 1);
          }
  
          else if(polyInfo.substr(i, 1) == ")")
          {
              if(polynomial)
              {
                  polynomial0 = polyInfo.substr(startPosition, (i - 1));
                  polynomial = false;
  
                  //
                  // To determine whether the program will be adding, subtracting, or multiplying, we
                  // set one of the booleans to true depending on which character was found.
                  //
                  if(polyInfo.substr(i + 1, 1) == "+")
                  {
                      adding = true;
                  }
  
                  else if(polyInfo.substr(i + 1, 1) == "-")
                  {
                      subtracting = true;
                  }
  
                  else if(polyInfo.substr(i + 1, 1) == "*")
                  {
                      multiplying = true;
                  }
              }
  
              else
              {
                  polynomial1 = polyInfo.substr(startPosition, i - startPosition);
              }
          }
      }
  
      polynomial2 = polynomial0;
  
      //
      // For loops and if statements are used to parse the string into an array so we will be able to 
      // easily handle the coefficients and powers later on for the operations.
      //
      for(int i = 0; i &lt; polynomial2.length(); ++i)
      {
          if(i == 0)
          {
              if(polynomial2.substr(0, 1) == "x")
              {
                  polyArray0[counter][0] = 1;
  
                  if(polynomial2.substr(1, 1) != "^")
                  {
                      polyArray0[counter][1] = 1;
                      ++counter;
                  }
              }
  
              else
              {
                  number = polynomial2.substr(0, 1);
                  polyArray0[counter][0] = atoi(number.c_str());
  
                  if(polynomial2.substr(i, 1) == "-")
                  {
                      polyArray0[counter][0] *= -1;
                  }
  
                  if(polynomial2.substr(1, 1) != "*")
                  {
                      polyArray0[counter][1] = 0;
                      ++counter;
                  }
  
                  else if((polynomial2.substr(i + 2, 1) == "*") &amp;&amp; (polynomial2.substr(i + 5, 1) != "^"))
                  {
                      polyArray0[counter][1] = 1;
                      ++counter;
                  }
              }
          }
  
          else if((polynomial2.substr(i, 1) == "-") || (polynomial2.substr(i, 1) == "+"))
          {
              if(polynomial2.substr(i + 1, 1) == "x")
              {
                  polyArray0[counter][0] = 1;
  
                  if(polynomial2.substr(i, 1) == "-")
                  {
                      polyArray0[counter][0] *= -1;
                  }
  
                  if(polynomial2.substr(i + 2, 1) != "^")
                  {
                      polyArray0[counter][1] = 1;
                      ++counter;
                  }
              }
  
              else
              {
                  number = polynomial2.substr(i + 1, 1);
                  polyArray0[counter][0] = atoi(number.c_str());
  
                  if(polynomial2.substr(i, 1) == "-")
                  {
                      polyArray0[counter][0] *= -1;
                  }
  
                  if(polynomial2.substr(i + 2, 1) != "*")
                  {
                      polyArray0[counter][1] = 0;
                  }
  
                  else if((polynomial2.substr(i + 2, 1) == "*") &amp;&amp; (polynomial2.substr(i + 4, 1) != "^"))
                  {
                      polyArray0[counter][1] = 1;
                      ++counter;
                  }
              }
          }
  
          else if(polynomial2.substr(i, 1) == "^")
          {
              number = polynomial2.substr(i + 1, 1);
              polyArray0[counter][1] = atoi(number.c_str());
              ++counter;
          }
      }
  
          polynomial2 = polynomial1;
  
          for(int i  = 0; i &lt; polynomial2.length(); ++i)
          {
              if(i == 0)
              {
                  if(polynomial2.substr(0, 1) == "x")
                  {
                      polyArray0[iteration][0] = 1;
  
                      if(polynomial2.substr(1, 1) != "^")
                      {
                          polyArray1[iteration][1] = 1;
                          ++iteration;
                      }
                  }
  
                  else
                  {
                      number = polynomial2.substr(0, 1);
                      polyArray1[iteration][0] = atoi(number.c_str());
  
                      if(polynomial2.substr(i, 1) == "-")
                      {
                          polyArray0[counter][0] *= -1;
                      }
  
                      if(polynomial2.substr(1, 1) != "*")
                      {
                          polyArray1[iteration][1] = 0;
                          ++iteration;
                      }
  
                      else if((polynomial2.substr(i + 1, 1) == "*") &amp;&amp; (polynomial2.substr(i + 3, 1) != "^"))
                      {
                          polyArray1[iteration][1] = 1;
                          ++iteration;
                      }
                  }
              }
  
              else if((polynomial2.substr(i, 1) == "+") || (polynomial2.substr(i, 1) == "-"))
              {
                  if(polynomial2.substr(i + 1, 1) == "x")
                  {
                      polyArray1[iteration][0] = 1;
  
                      if(polynomial2.substr(i, 1) == "-")
                      {
                          polyArray0[counter][0] *= -1;
                      }
  
                      if(polynomial2.substr(i + 2, 1) != "^")
                      {
                          polyArray1[iteration][1] = 1;
                          ++iteration;
                      }
                  }
  
                  else
                  {
                      number = polynomial2.substr(i + 1, 1);
                      polyArray1[iteration][0] = atoi(number.c_str());
  
                      if(polynomial2.substr(i, 1) == "-")
                      {
                          polyArray0[counter][0] *= -1;
                      }
  
                      if(polynomial2.substr(i + 2, 1) != "*")
                      {
                          polyArray1[iteration][1] = 0;
                          ++iteration;
                      }
  
                      else if((polynomial2.substr(i + 2, 1) == "*") &amp;&amp; (polynomial2.substr(i + 4, 1) != "^"))
                      {
                          polyArray1[iteration][1] = 1;
                          ++iteration;
                      }
                  }
              }
  
              else if(polynomial2.substr(i, 1) == "^")
              {
                  number = polynomial2.substr(i + 1, 1);
                  polyArray1[iteration][1] = atoi(number.c_str());
                  ++iteration;
              }
          }
  
          //
          // If the program found a "+" it will call the addition function and store the answer in "newPolynomial."
          // Similar process with subtracting and multiplying.
          //
          if(adding)
          {
              newPolynomial = addition(polyArray0, polyArray1, counter, iteration);
          }
  
          else if(subtracting)
          {
              newPolynomial = subtraction(polyArray0, polyArray1, counter, iteration);
          }
  
          else
          {
              newPolynomial = multiplication(polyArray0, polyArray1, counter, iteration);
          }
  
          number = newPolynomial.substr(0, 1);
          updated = atoi(number.c_str());
  
          for(int i = 1; i &lt; (updated + 1); ++i)
          {
              number = newPolynomial.substr(i, 1);
              polyArray0[i][0] = atoi(number.c_str());
              number = newPolynomial.substr((i + updated), 1);
              polyArray0[i][1] = atoi(number.c_str());
          }
  
          for(int i = 0; i &lt; updated; ++i)
          {
              cout &lt;&lt; polyArray0[i][0] &lt;&lt; "x^" &lt;&lt; polyArray0[i][1];
  
              if(polyArray0[i][0] &gt; 0 &amp;&amp; i != updated)
              {
                  cout &lt;&lt; "+";
              }
          }
  
      cout &lt;&lt; endl;
      system("PAUSE");
      return 0;
  }
  </code></pre>