title: Subtract large numbers using arrays c++
body: <p>I tried to subtract two large positive integers with the help of arrays. But when running the code below I get a segmentation fault (core dumped) when run on g++ (Ubuntu).</p>
  
  <p>I followed <a href="https://stackoverflow.com/questions/15006080/subtract-numbers-using-arrays-c">Subtract numbers using arrays - C++</a> and <a href="https://stackoverflow.com/questions/14875445/subtracting-two-long-positive-integers-in-an-array-c?rq=1">Subtracting two long positive integers in an array c++</a> but couldn't find much of help from these.</p>
  
  <pre><code>int main() {
  vector &lt;int&gt; N;
  vector &lt;int&gt; a;
  vector &lt;int&gt; y;
  char digit1;
  int l;
  
  do {
      cin.get(digit1);
      if (digit1 != (int)
          '\n')
          N.push_back(int(digit1) - int('0'));
  } while (digit1 != (int)
      '\n');
  
  char digit2;
  do {
      cin.get(digit2);
      if (digit2 != (int)
          '\n')
          a.push_back(int(digit2) - int('0'));
  } while (digit2 != (int)
      '\n');
  
  int i = N.size() - 1;
  int j = a.size() - 1;
  int k = 0;
  do {
      if (j &gt;= 0) {
          if (N[i] &lt; a[j]) {
              y[k] = N[i] + 10 - a[j];
              l = i - 1;
              while (N[l] == 0) {
                  N[l] = 9;
                  l--;
              }
              N[l] = N[l] - 1;
          } else {
              y[k] = N[i] - a[j];
          }
          i--;
          j--;
          k++;
      } else {
          y[k] = N[i];
          i++;
          k++;
      }
  } while (i &gt;= 0);
  
  for (int m = y.size() - 1; m &gt;= 0; m--) {
      cout &lt;&lt; y[m];
  }
  return 0;
  </code></pre>