C++ for statement Computes a grade average, an early exit with a break statement

Description

C++ for statement Computes a grade average, an early exit with a break statement

#include <iostream>
using namespace std;
#include <iomanip.h>
int main()/*from   w ww .j a v  a2 s.  c  o m*/
{
   float grade, avg;
   float total=0.0;
   int num, count=0;
   int loopvar;

   cout << "How many students are there? ";
   cin >> num;                // Get total number to enter.

   for (loopvar=1; loopvar<=num; loopvar++)
   {
      cout << "\nWhat is the next student's " << "grade? (-99 to quit) ";
      cin >> grade;
      if (grade < 0.0)              // A negative number triggers break.
      {
         break;
      }               // Leave the loop early.
      count++;
      total += grade;
   }
   avg = total / count;
   cout << "\n\nThe average of this class is "<< setprecision(1) << avg;
   return 0;
}



PreviousNext

Related