title: Converting IP address to binary
body: <p>I'm currently working on converting each piece of an ip address to binary, and I would be glad if u can tell me why my code doesn't work. The main problem is with function ip_bin(); cause it's converting the first number correctly to binary but somehow later then it is adding the first number to the second one... same situation in 2 last answers.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  string ip, mask;
  string ip_string[4];
  int ip_int[4], length[4];
  
  void enter();
  void get_ip();
  void ip_bin();
  
  int main(){
  
      enter();
      get_ip();
      ip_bin();
  
  return 0;}
  
  void enter(){
  
      cout&lt;&lt;&quot;Enter ip adress: &quot;;
      cin&gt;&gt;ip;
      cout&lt;&lt;&quot;Enter mask adress: &quot;;
      cin&gt;&gt;mask;
  }
  
  void get_ip(){
  //splitting to sections each number between the ip dots 
      for(int i = 0; i&lt;4; i++){
  
          ip_int[i] = stoi(ip.substr(0, '.'));
          cout&lt;&lt;ip_int[i]&lt;&lt;endl;
          string z = to_string(ip_int[i]);
          int ln = z.length();
          ip.erase(0, ln+1);
      }
  }
  
  void ip_bin(){
      int i=0, tab[31];
      for(int k = 0; k&lt;4; k++){
          while(ip_int[k]){
              tab[i++] = ip_int[k]%2;
  
              ip_int[k] = ip_int[k]/2;
          }
          for(int j = i-1; j&gt;=0; j--){
              ip_string[k] = ip_string[k] + to_string(tab[j]);
          }
  
          length[k] = ip_string[k].length();
          if(length[k]&lt;8){
              for(int g = 0; g&lt;8 - length[k]; g++){
                  ip_string[k] = '0' + ip_string[k];
              }
          }
  
          cout&lt;&lt;ip_string[k]&lt;&lt;endl;
      }
  
  
  }
  </code></pre>