title: C++: Trouble with ending a loop based on a condition?
body: <p>This code is used to run a loop three times, that takes the numbers of eggs gathered and outputs the number in dozens and extra until the user enters a negative number. Then, it prints out the average amount of eggs gathered (entered), and outputs the total number of dozens and extra.</p>
  
  <p>The inputs we were assigned to use are:
  43,
  31,
  -1,
  24,
  8,
  14,
  -999,
  -5.</p>
  
  <p>Everything is fine up until we input -5. Our teacher doesn't want the average or total number of dozens and extras to print (you'll see what I mean in the output).</p>
  
  <p>The source code is as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  int main ()
  {
      int   eggNum;
      int   eggDozens;
      int   eggExtra;
      int   eggTotal;
      int   loopCount;
      int   forCount;
      float eggAvg;
      int   totalDozens;
      int   totalExtra;
  
      for(forCount = 1; forCount &lt;= 3; forCount=forCount + 1)
      {
          cout &lt;&lt; left &lt;&lt; "TEST #" &lt;&lt; forCount &lt;&lt; ":" &lt;&lt; endl;
          cout &lt;&lt; "Welcome to Aunt Ellen\'s eggs to dozens converter!";
          cout &lt;&lt; endl &lt;&lt; endl;
          cout &lt;&lt; "\tEnter the number of eggs gathered: ";
          cin  &gt;&gt; eggNum;
  
          eggTotal  = 0;
          loopCount = 0;
  
          while(eggNum &gt;= 0)
          {
              eggDozens = eggNum / 12;
              eggExtra  = eggNum % 12;
  
              if(eggDozens != 0)
              {
                  if(eggExtra != 0)
                  {
                      cout &lt;&lt; "\tYou have " &lt;&lt; eggDozens &lt;&lt; " dozen ";
                      cout &lt;&lt; eggExtra &lt;&lt; " eggs.";
                      cout &lt;&lt; endl &lt;&lt; endl;
                  }
                  else
                  {
                      cout &lt;&lt; "\tYou have " &lt;&lt; eggDozens &lt;&lt; " dozen eggs.";
                      cout &lt;&lt; endl &lt;&lt; endl;
                  }
              }
              else
              {
                  cout &lt;&lt; "\tYou have " &lt;&lt; eggExtra &lt;&lt; " eggs.";
                  cout &lt;&lt; endl &lt;&lt; endl;
              }
  
              loopCount = loopCount + 1;
              eggTotal  = eggTotal + eggNum;
  
              cout &lt;&lt; "\tEnter the number of eggs gathered: ";
              cin  &gt;&gt; eggNum;
          }
          cout &lt;&lt; endl &lt;&lt; "TOTALS:" &lt;&lt; endl;
          eggAvg = eggTotal / float(loopCount);
          cout &lt;&lt; "\tOn average " &lt;&lt; eggAvg &lt;&lt; " eggs have been";
          cout &lt;&lt; " gathered.";
  
          totalDozens = eggTotal / 12;
          totalExtra  = eggTotal % 12;
  
          cout &lt;&lt; endl &lt;&lt; "\tA total of " &lt;&lt; totalDozens &lt;&lt; " dozen ";
          cout &lt;&lt; totalExtra &lt;&lt; " and eggs have been gathered!" &lt;&lt; endl;
          cout &lt;&lt; endl &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>TEST #1:
  Welcome to Aunt Ellen's eggs to dozens converter!
  
      Enter the number of eggs gathered: 43
      You have 3 dozen 7 eggs.
  
      Enter the number of eggs gathered: 31
      You have 2 dozen 7 eggs.
  
      Enter the number of eggs gathered: -1
  
  TOTALS:
      On average 37 eggs have been gathered.
      A total of 6 dozen 2 and eggs have been gathered!
  
  
  TEST #2:
  Welcome to Aunt Ellen's eggs to dozens converter!
  
      Enter the number of eggs gathered: 24
      You have 2 dozen eggs.
  
      Enter the number of eggs gathered: 8
      You have 8 eggs.
  
      Enter the number of eggs gathered: 14
      You have 1 dozen 2 eggs.
  
      Enter the number of eggs gathered: -999
  
  TOTALS:
      On average 15.3333 eggs have been gathered.
      A total of 3 dozen 10 and eggs have been gathered!
  
  
  TEST #3:
  Welcome to Aunt Ellen's eggs to dozens converter!
  
      Enter the number of eggs gathered: -5
  
  TOTALS:
      On average -1.#IND eggs have been gathered.
      A total of 0 dozen 0 and eggs have been gathered!
  </code></pre>
  
  <p>I don't want the very last "TOTALS" and the lines following. I want the program to terminate after entering -5.</p>