Skip to content

Instantly share code, notes, and snippets.

@dag10
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dag10/9980803 to your computer and use it in GitHub Desktop.
Save dag10/9980803 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
const int ENDGRADE = 999;
int main() {
cout << "This program accepts user-entered grades and calculates the average." << endl;
double grade_entered = 0;
double grade_total;
int num_grades = 0;
do {
cout << "Please enter a grade: ";
cin >> grade_entered;
if (grade_entered >= 0 && grade_entered <= 100) {
grade_total += grade_entered;
num_grades++;
} else if (grade_entered != ENDGRADE) {
cerr << grade_entered << " is an invalid grade." << endl;
}
} while (grade_entered != ENDGRADE);
if (num_grades > 0) {
double average = grade_total / num_grades;
cout << "The average of all grades is " << average << " percent" << endl;
} else {
cout << "No grades were entered." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment