title: stoi "terminate called after throwing an instance of 'std::invalid argument'
body: <p>I am currently working on a dynamic programming problem, and I decided code it in C++ instead of my usual Java/C# to improve my skills.
  However, I am getting an error that I cannot figure out. EDIT: I will post my entire solution here:</p>
  
  <pre><code>int main() {
      /* Enter your code here. Read input from STDIN. Print output to STDOUT */
      int linecounter = 0;
      int value = -1;
      int differentCoins = -1;
      int uniqueCoinCount = -1;
      std::vector&lt;std::vector&lt;int&gt;&gt; array;
      int *uniqueCoins;
      int totalAmount = -1;
  
      for (std::string line; std::getline(std::cin, line); ) {
          if (linecounter % 2 == 0) {
              string inputs[2];
              int i = 0;
              stringstream ssin(line);
              while (ssin.good() &amp;&amp; i &lt; 2){
                  ssin &gt;&gt; inputs[i];
                  ++i;
              }
              cout &lt;&lt; inputs[0];
              cout &lt;&lt; inputs[1];
              totalAmount = std::stoi(inputs[0]);
              uniqueCoinCount = std::stoi(inputs[1]);
              uniqueCoins = new int[uniqueCoinCount + 1];
  
          }
          if (linecounter % 2 == 1) {
              array.resize(uniqueCoinCount + 1, std::vector&lt;int&gt;(totalAmount + 1));
              for (int i = 0; i &lt; totalAmount; i++) {
                  array[i][0] = 0;
              }
              for (int i = 0; i &lt;= uniqueCoinCount; i++) {
                  array[0][i] = 1;
              }
  
  
              stringstream ssin(line);
              int coinCounter = 0;
              uniqueCoins[coinCounter] = 0;
              coinCounter++;
              while (ssin.good() &amp;&amp; coinCounter &lt; uniqueCoinCount + 1){
  
                  ssin &gt;&gt; uniqueCoins[coinCounter];
                  coinCounter++;
              }
  
              for (int i = 1; i &lt; totalAmount; i++) {
                  for (int j = 1; j &lt;= uniqueCoinCount; j++) {
                      int totalExcludingCoin = array[i][j - 1];
                      if (uniqueCoins[j] &lt;= i) {
                          array[i][j] = totalExcludingCoin + array[i - uniqueCoins[j]][j];
                      }
                      else {
                          array[i][j] = totalExcludingCoin;
                      }
                  }
              }
          }
          linecounter++;
      }
  
      //cout &lt;&lt; array[totalAmount][uniqueCoinCount + 1];
  }
  </code></pre>
  
  <p>When I use <code>cout &lt;&lt; inputs[0]</code>, I can see that it prints out <code>4</code>. However, the hackerrank compiler gives me an error saying</p>
  
  <blockquote>
    <p>"terminate called after throwing an instance of '<code>std::invalid_argument</code>'<br>
    <code>what():  stoi</code>"</p>
  </blockquote>
  
  <p>What could be the problem? I've also tried <code>atoi()</code>, which returns me <code>40</code> instead of <code>4</code>, possibly due to <code>atoi</code> returning <code>0</code> when it errors? Is it because it reads the null terminator or something?</p>
  
  <p>Thanks</p>
  
  <p>Here is the output on hackerrank:</p>
  
  <pre>
  Nice try, but you did not pass this test case.
  
  Input (stdin)
  4 3
  1 2 3 
  
  Your Output (stdout)
  43
  
  Expected Output
  4
  
  Compiler Message
  Abort Called
  Error (stderr)
  terminate called after throwing an instance of 'std::invalid_argument'  
  what():  stoi
  </pre>