title: Code involving counting prime numbers stops working once a composite number is input. C++
body: <p>Code involving counting prime numbers stops working once a composite number is input. C++.</p>
  <p>Code:</p>
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      int num;
      string var;
      int sum=0;
      int i;
      int largest = INT_MIN;
      int smallest = INT_MAX;
      int j = 0;
      int prime = 0;
      do {
          cout &lt;&lt; &quot;Please enter a series of numbers, press (Q or q) to process: &quot;;
          cin &gt;&gt; num;
          if (cin.fail())
          {
              cin.clear();
              cin &gt;&gt; var;
              if (var != &quot;Q&quot; &amp;&amp; var != &quot;q&quot;)
              {
                  cout &lt;&lt; &quot;Invalid input, try again&quot; &lt;&lt; endl;
              }
              else
              {
                  break;
              }
  
          }
          if (num &gt; largest)
          {
              largest = num;
          }
          if (num &lt; smallest)
          {
              smallest = num;
          }
          if (num == 0 || num == 1)
          {
              j == 1;
          }
          else
          {
              for (i = 2; i &lt;= num / 2; i++)
              {
                  if (num % i == 0)
                  {
                      j = 1;
                      break;
                  }
  
              }
              if (j == 0)
              {
                  prime++;
              }
          }
          sum += num;
          cout &lt;&lt; &quot;The corresponding element for the cumulative total sequence is: &quot; &lt;&lt; sum &lt;&lt; endl;
          cin.ignore(sum, '\n');
      } while (var != &quot;Q&quot; &amp;&amp; var != &quot;q&quot;);
      cout &lt;&lt; endl;
      cout &lt;&lt; &quot;Largest number: &quot; &lt;&lt; largest &lt;&lt; endl;
      cout &lt;&lt; &quot;Smallest number: &quot; &lt;&lt; smallest &lt;&lt; endl;
      cout &lt;&lt; &quot;How many prime numbers? &quot; &lt;&lt; prime &lt;&lt; endl;
      cout &lt;&lt; &quot;Have a great day!&quot; &lt;&lt; endl;
  }
  </code></pre>
  <p>Example of program's output:</p>
  <pre class="lang-none prettyprint-override"><code>Please enter a series of numbers, press (Q or q) to process: 2
  The corresponding element for the cumulative total sequence is: 2
  Please enter a series of numbers, press (Q or q) to process: 7
  The corresponding element for the cumulative total sequence is: 9
  Please enter a series of numbers, press (Q or q) to process: 4
  The corresponding element for the cumulative total sequence is: 13
  Please enter a series of numbers, press (Q or q) to process: 13
  The corresponding element for the cumulative total sequence is: 26
  Please enter a series of numbers, press (Q or q) to process: 17
  The corresponding element for the cumulative total sequence is: 43
  Please enter a series of numbers, press (Q or q) to process: q
  
  Largest number: 17
  Smallest number: 2
  How many prime numbers? 2
  Have a great day!
  </code></pre>
  <p>The first two inputs are prime numbers and work properly, but once I input a composite number (4) the rest of the prime numbers do not get counted as a prime number.</p>