title: C++ Long Division
body: <p>Whilst working on a personal project of mine, I came across a need to divide two very large arbitrary numbers (each number having roughly 100 digits). 
  So i wrote out the very basic code for division (i.e., answer = a/b, where a and b are imputed by the user)and quickly discovered that it only has a precision of 16 digits! It may be obvious at this point that Im not a coder! </p>
  
  <p>So i searched the internet and found a code that, as far as i can tell, uses the traditional method of long division by making a string(but too be honest im not sure as im quite confused by it). But upon running the code it gives out some incorrect answers and wont work at all if a>b.
  Im not even sure if there's a better way to solve this problem than the method in the code below!? Maybe there's a simpler code??</p>
  
  <p>So basically i need help to write a code, in C++, to divide two very large numbers. 
  Any help or suggestions are greatly appreciated! </p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  
  using namespace std;  //avoids having to use std:: with cout/cin
  
  
  
  int main (int argc, char **argv)
  {
      string dividend, divisor, difference, a, b, s, tempstring = ""; // a and b used to store dividend and divisor.
      int quotient, inta, intb, diff, tempint = 0;
      char d;
  
      quotient = 0;
  
      cout &lt;&lt; "Enter the dividend?  ";      //larger number (on top)
      cin  &gt;&gt; a;
      cout &lt;&lt; "Enter the divisor?  ";           //smaller number (on bottom)
      cin  &gt;&gt; b;
  
      //making the strings the same length by adding 0's to the beggining of string.
      while (a.length() &lt; b.length())  a = '0'+a;          // a has less digits than b add 0's
       while (b.length() &lt; a.length())  b = '0'+b;          // b has less digits than a add 0's
  
      inta = a[0]-'0';                        // getting first digit in both strings
      intb = b[0]-'0';
  
      //if a&lt;b print remainder out (a) and return 0
      if (inta &lt; intb)
      {
          cout &lt;&lt; "Quotient: 0 " &lt;&lt; endl &lt;&lt; "Remainder: " &lt;&lt; a &lt;&lt; endl;
      }
  
  
      else
      {
          a = '0'+a;
          b = '0'+b;
          diff = intb;
          //s = b;
         // while ( s &gt;= b )
  
          do
          {
              for (int i = a.length()-1; i&gt;=0; i--)            // do subtraction until end of string
              {
              inta = a[i]-'0';                    // converting ascii to int, used for munipulation
              intb = b[i]-'0';   
              if (inta &lt; intb)                 // borrow if needed
              {
                a[i-1]--;                         //borrow from next digit
                a[i] += 10;
              }
              diff = a[i] - b[i];
            char d = diff+'0';
             s = d + s;                           //this + is appending two strings, not performing addition.
  
              }
           quotient++;
           a = s;
          // strcpy (a, s);
          } 
  
          while (s &gt;= b); // fails after dividing 3 x's
  
      cout &lt;&lt; "s string: " &lt;&lt; s &lt;&lt; endl;
       cout &lt;&lt; "a string: " &lt;&lt; a &lt;&lt; endl;
       cout &lt;&lt; "Quotient:  " &lt;&lt; quotient &lt;&lt; endl;
       //cout &lt;&lt; "Remainder: " &lt;&lt; s &lt;&lt; endl;
  
      }
  
      system ("pause");
      return 0;
      cin.get(); // allows the user to enter variable without instantly ending the program
      cin.get(); // allows the user to enter variable without instantly ending the program
  </code></pre>
  
  <p>}</p>