title: Octal conversion using loops in C++
body: <p>I am currently working on a basic program which converts a binary number to an octal. Its task is to print a table with all the numbers between 0-256, with their binary, octal and hexadecimal equivalent. The task requires me only to use my own code (i.e. using loops etc and not in-built functions). The code I have made (it is quite messy at the moment) is as following (this is only a snippit):</p>
  
  <pre><code>        int counter = ceil(log10(fabs(binaryValue)+1));
          int iter;
          if (counter%3 == 0)
          {
              iter = counter/3;
          }
          else if (counter%3 != 0)
          {
              iter = ceil((counter/3)); 
          }
          c = binaryValue;
          for (int h = 0; h &lt; iter; h++)
          {
              tempOctal = c%1000;
              c /= 1000;
              int count = ceil(log10(fabs(tempOctal)+1));
              for (int counter = 0; counter &lt; count; counter++)
              {
                  if (tempOctal%10 != 0)
                  {
                     e = pow(2.0, counter);
                     tempDecimal += e;
                  }
                  tempOctal /= 10;
              }
              octalValue += (tempDecimal * pow(10.0, h));
          }
  </code></pre>
  
  <p>The output is completely wrong. When for example the binary code is 1111 (decimal value 15), it outputs 7. I can understand why this happens (the last three digits in the binary number, 111, is 7 in decimal format), but can't be able to identify the problem in the code. Any ideas?</p>
  
  <p>Edit: After some debugging and testing I figured the answer.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main()
  {
      while (true)
  {
      int binaryValue, c, tempOctal, tempDecimal, octalValue = 0, e;
      cout &lt;&lt; "Enter a binary number to convert to octal: ";
      cin &gt;&gt; binaryValue;
      int counter = ceil(log10(binaryValue+1));
      cout &lt;&lt; "Counter " &lt;&lt; counter &lt;&lt; endl;
      int iter;
      if (counter%3 == 0)
      {
         iter = counter/3;
      }
      else if (counter%3 != 0)
      {
         iter = (counter/3)+1; 
      }
      cout &lt;&lt; "Iterations " &lt;&lt; iter &lt;&lt; endl;
      c = binaryValue;
      cout &lt;&lt; "C " &lt;&lt; c &lt;&lt; endl;
      for (int h = 0; h &lt; iter; h++)
      {
          tempOctal = c%1000;
          cout &lt;&lt; "3 digit binary part " &lt;&lt; tempOctal &lt;&lt; endl;
          int count = ceil(log10(tempOctal+1));
          cout &lt;&lt; "Digits " &lt;&lt; count &lt;&lt; endl;
          tempDecimal = 0;
          for (int counterr = 0; counterr &lt; count; counterr++)
          {
              if (tempOctal%10 != 0)
              {
                   e = pow(2.0, counterr);
                   tempDecimal += e;
                   cout &lt;&lt; "Temp Decimal value 0-7 " &lt;&lt; tempDecimal &lt;&lt; endl;
              }
              tempOctal /= 10;
          }
          octalValue += (tempDecimal * pow(10.0, h));
          cout &lt;&lt; "Octal Value " &lt;&lt; octalValue &lt;&lt; endl;
          c /= 1000;
      }
  cout &lt;&lt; "Final Octal Value: " &lt;&lt; octalValue &lt;&lt; endl;
  }
  system("pause");
  return 0;
  </code></pre>
  
  <p>}</p>