title: Bigint class operator
body: <p>Please check out this code and tell me why is the operator += only works if the numbers have the same size when i tried <code>x += z;</code> <code>i</code> got <code>345678</code>.</p>
  
  <p>I think the problem is <code>int for (i = 0; i &lt;= len - 1; i++)</code> loop. I tried to fix it but I wasn't successful. Any solutions?</p>
  
  <p>Header file.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class Mint {
  public:
  
      Mint();
      Mint(int);
      Mint (const char  *s);
      string afficher();
      Mint operator+=(const Mint &amp;rhs); //returns mint + rhs
      Mint operator+(const Mint &amp;rhs);
  private:
      vector&lt;char&gt; num;
  };
  </code></pre>
  
  <p>Source file.</p>
  
  <pre><code>#include "Mint.h"
  #include &lt;string.h&gt;
  
  Mint::Mint()
  {
      num.push_back(0);
  }
  Mint::Mint(int n)
  {
      while(n!=0)
      {
          num.push_back(n%10);
          n = n/10;
      }
  }
  Mint::Mint(const char* s)
  {
      int i = 0 ;
      for(i=strlen(s)-1;i&gt;=0;i--)
      num.push_back(s[i] - '0');
  }
  string Mint::afficher(){
      string s="";
      int i;
      for(i=num.size()-1;i&gt;=0;i--)
          s += char('0'+num[i]);
      return s;
  }
  Mint Mint::operator+=(const Mint &amp;rhs) {
      unsigned int len = num.size();
      char carry = 0;
  
      if (len &gt; rhs.num.size())
          len = rhs.num.size();
  
      unsigned int i;
      for (i = 0; i &lt;= len - 1; i++)
      {
          char result = num[i] + rhs.num[i] + carry;
          num[i] = result % 10;
          carry = result / 10;
      }
  
      while (carry)
      {
          if (i &lt; num.size())
          {
              num[i] += carry;
              if (num[i] &gt;= 10)
              {
                  num[i] -= 10;
                  i++;
              }
              else
                  carry = 0;
          }
          else {
              num.push_back(carry);
              carry = 0;
          }
      }
      return *this;
  }
  Mint Mint::operator+(const Mint &amp;rhs){
      Mint result;
      result = *this;
      return result+=rhs;
  }
  </code></pre>
  
  <p><code>main</code> file.</p>
  
  <pre><code>#include "Mint.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;  
  
  int main()
  {
      Mint x="123456",z="22222222222222222222",f;
      Mint a = "655478461469974272005572";
      Mint b = 8;
      x += z;
      f = x + z;
      cout &lt;&lt; x.afficher()&lt;&lt;endl;
  
  }
  </code></pre>