C++ do while loop Adds up grades, computes average

Description

C++ do while loop Adds up grades, computes average

#include <iostream>
using namespace std;
#include <iomanip.h>
int main()//from   w w w . j  a  va2s.  c  o m
{
   float total_grade=0.0;
   float grade_avg = 0.0;
   float grade;
   int grade_ctr = 0;
   do
   {
      cout << "What is your grade? (-1 to end) ";
      cin >> grade;
      if (grade >= 0.0)
      {
         total_grade += grade;              // Add to total.
         grade_ctr ++;
      }                    // Add to count.
   } while (grade >= 0.0);         // Quit when -1 entered. Control begins here if no more grades.
   grade_avg = (total_grade / grade_ctr);       // Compute average.
   cout << "\nYou made a total of " << setprecision(1) << total_grade << " points.\n";
   cout << "Your average was " << grade_avg << "\n";

   if (total_grade >= 450.0)
   {
      cout << "You made an A!!";
   }
   return 0;
}



PreviousNext

Related