title: Binary math programming
body: <p>I have been asked to create a program that takes 2 binary numbers then outputs the result of adding, subtracting and multiplying them.  My code works for addition but when I try to do subtraction or multiplication, the output is not as expected.  The cout statement is displayed with each digit and several of the digits are incorrect.  Could anyone point to flaws in my code?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main() {
  
      long num1, num2;
      int choice;
      int i = 0, r = 0, sum[20];
  
      cout &lt;&lt; "Enter the first binary number: ";   //prompts user to input binary values to perform arithmetic on
      cin &gt;&gt; num1;
  
      cout &lt;&lt; "Enter the second binary number: ";
      cin &gt;&gt; num2;
  
      cout &lt;&lt; "To add the numbers press 1, to subtract the numbers press 2," &lt;&lt; endl;  //prompts user to choose which operation to perform
      cout &lt;&lt; "to multiply the numbers press 3" &lt;&lt; endl;
      cin &gt;&gt; choice;
  
      if (choice != 1 &amp; choice != 2 &amp; choice != 3)  //if statement to test for valid input
      {
          cout &lt;&lt; "Please enter a valid choice: ";
          cin &gt;&gt; choice;
      }
  
      if (choice == 1) 
      {
  
          while (num1 != 0 || num2 != 0)     //if user chooses 1, perform binary arithmetic
          {
              sum[i++] = (num1 % 10 + num2 % 10 + r) % 2;
              r = (num1 % 10 + num2 % 10 + r) / 2;
              num1 = num1 / 10;
              num2 = num2 / 10;
          }
          if (r != 0)
              sum[i++] = r;
          --i;
          cout &lt;&lt; "The sum of the two numbers is: ";
  
  
          while (i &gt;= 0)
              cout &lt;&lt; sum[i--];
          cout &lt;&lt; ". ";
      }
  
      if (choice == 2)
      {
  
          while (num1 != 0 || num2 != 0)     //if user chooses 2, perform binary subtraction
          {
              int i = 0, r = 0, diff[20];
              diff[i++] = (num1 % 10 - num2 % 10 + r) % 2;
              r = (num1 % 10 - num2 % 10 + r) / 2;
              num1 = num1 / 10;
              num2 = num2 / 10;
              if (r != 0)
                  diff[i++] = r;
              --i;
              cout &lt;&lt; "The difference of the two numbers is: ";
              while (i &gt;= 0)
                  cout &lt;&lt; diff[i--];
              cout &lt;&lt; ". ";
          }
      }
      if (choice == 3)   //if user chooses 3, perform binary multiplication
      {
  
          while (num1 != 0 || num2 != 0)
          {
              int i = 0, r = 0, product[20];
              product[i++] = (num1 % 10 * num2 % 10 + r) % 2;
              r = (num1 % 10 * num2 % 10 + r) / 2;
              num1 = num1 / 10;
              num2 = num2 / 10;
              if (r != 0)
                  product[i++] = r;
              --i;
              cout &lt;&lt; "The product of the two numbers is: ";
              while (i &gt;= 0)
                  cout &lt;&lt; product[i--];
              cout &lt;&lt; ". ";
          }
      }
  
  
      system("pause");
      return 0;
  }
  </code></pre>