Class Average w/Sentinel Controlled Reptition() {

// 2.9.cpp

#include <iostream.h>
#include <iomanip.h>
#include <math.h>

using namespace std;

int main() {

  int total, gradeCounter, grade;
  double average;

  total = gradeCounter = 0;

  cout << "Enter grade, -1 to end; ";
  cin >> grade;

  while(grade != -1) {
    total = total + grade;
    gradeCounter = gradeCounter + 1;

    cout << "Enter grade, -1 to end: ";
    cin >> grade;

  }

  if(gradeCounter != 0) {
    average = static_cast<double>(total)/gradeCounter;
    int fixed;
    cout << "Class average is " << setprecision(2) << fixed << average << endl;

  }
  else
    cout << "No grades were entered " << endl;

  return 0 ;

}

output:

[edeloss2@pegasus ch2]$ a.out
Enter grade, -1 to end; 90
Enter grade, -1 to end: 90
Enter grade, -1 to end: -1
Class average is 107396852890
edelossantosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
'fixed' seems to mess up the output somehow, but actually I don't know why this is happening here - the following works fine:

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main() {

 int total, gradeCounter, grade;
 double average;

 total = gradeCounter = 0;

 cout << "Enter grade, -1 to end; ";
 cin >> grade;

 while(grade != -1) {
   total = total + grade;
   gradeCounter = gradeCounter + 1;

   cout << "Enter grade, -1 to end: ";
   cin >> grade;

 }

  cout << "Total: " << total << endl;
  cout << "Grade count " << gradeCounter << endl;

 if(gradeCounter != 0) {
   average = static_cast<double>(total)/gradeCounter;
   int fixed;
   cout << "Class average is " << setprecision(2) << average << endl;

 }
 else
   cout << "No grades were entered " << endl;

 return 0 ;

}

BTW, you don't want to use the STL headers with the .h extension...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edelossantosAuthor Commented:
Thank you.  Del
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.