title: C++ String Expression Solver Running Into Access Violation
body: <p>I am trying to create a program that will take in an expression (example: "10*2+1") and solve it. My code is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  void calculateString(std::string str);
  
  int main() {
  
      calculateString("10*2+2");
  
      system("pause");
  }
  
  void calculateString(std::string str) {
      int total = 0;
      std::string temp1 = "";
      std::string temp2 = "";
      std::string add = "";
      std::string *ray = new std::string[str.length()];
      std::vector&lt;int&gt; newRay;
  
      for (int i = 0; i &lt; str.length(); i++) {
          if (str.at(i) != '*' &amp;&amp; str.at(i) != '/' &amp;&amp; str.at(i) != '+' &amp;&amp; str.at(i) != '-') {
              add += str.at(i);
          }
          else {
              ray[i] = add;
              std::cout &lt;&lt; ray[i] &lt;&lt; "\n";
              add = "";
          }
      }
      for (int i = 0; i &lt; str.length(); i++) {
          if (ray[i].compare("*")) {
              total = atoi(ray[i - 1].c_str()) * atoi(ray[i + 1].c_str());
              newRay.push_back(total);
          }
      }
      for (int i = 0; i &lt; str.length(); i++) {
          if (ray[i] == "+") {
              newRay.push_back(atoi(ray[i - 1].c_str()) + atoi(ray[i + 1].c_str()));
          }
      }
      for (int i = 0; i &lt; newRay.size(); i++) {
          std::cout &lt;&lt; newRay[i] &lt;&lt; "\n";
          total += newRay[i];
      }
      std::cout &lt;&lt; str &lt;&lt; "=" &lt;&lt; total &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>However, whenever I run this I keep running into an access violation error that reads: </p>
  
  <blockquote>
    <p>Exception thrown at 0x0F1CD4A0 (ucrtbased.dll) in CalcString.exe:
    0xC0000005: Access violation reading location 0x01BE0FEE.</p>
  </blockquote>
  
  <p>And it points to line 34 which is this: 
  <code>total = atoi(ray[i - 1].c_str()) * atoi(ray[i + 1].c_str());</code>
  which is basically calculating the multiplication part of the expression then storing the asnwer in one variable. I have tried everything from changing my array to a vector to trying to rewrite all the methods and nothing seems to work. Please help</p>