title: How to output space between parts of a string
body: <p>Here is my code. I want to change decimal number into hexadecimal and binary. I think I did it but for binary parts I want it to have space between every 4 digits (like 1111 1111 1111 1111). I wonder how can I change it ("ans2" is string)</p>
  
  <hr>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  int main() {
      //output inital prompt
  
      cout &lt;&lt; "ConvInteger- Conversion to Hexadecimal and Binary               Representation\n";
  
      //read in input 
      int decimal;
      cout &lt;&lt; "Enter a Positive Decimal Integer(&lt;65535,-1 quits): ";
      cin &gt;&gt; decimal;
      if (decimal == -1) {
          cout &lt;&lt; "Program Terminated by user\n";
          return 0;
      }
      cout &lt;&lt; "Decimal:         " &lt;&lt; decimal &lt;&lt; endl;
  
      // calculates the decimal to Hexdecimal
      string ans = "";
      string hexdecimal = "0123456789ABCDEF";
      int i;
      for (i = 4; i &gt; 0; i--) {
          ans = hexdecimal[decimal % 16] + ans;
          decimal = decimal / 16;
      }
      cout &lt;&lt; "hexdecimal:      " &lt;&lt; ans &lt;&lt; endl;
  
      //calculates the decimal toBinary
      int ans1, i1;
      for (i1 = 16; i1 &gt; 0; i1--) {
          string ans1 = "";
          string binary = "01";
          ans1 = binary[decimal % 2] + ans1;
          decimal = decimal / 2;
      }
  
      cout &lt;&lt; "binary:      " &lt;&lt; ans1 &lt;&lt; endl;
  
      return 0;
      system("PAUSE");
  }
  </code></pre>