?
Solved

Class Average w/Sentinel Controlled Reptition() {

Posted on 2004-11-22
2
Medium Priority
?
205 Views
Last Modified: 2010-04-01
// 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
0
Comment
Question by:edelossantos
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12648774
'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
 

Author Comment

by:edelossantos
ID: 12648819
Thank you.  Del
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question