This program will calculate the average(%) of exam grades.
It will also add extra credit points to the exam average given the course difficulty.
Enter all of the grades for one student. Type (-1) when finished with that student.
If you have additional students, you will be prompted to repeat the program at the end.
Enter an exam grade (type -1 to quit): user types: ten
Error: Grades must be an integer 0 or higher.
Enter an exam grade (type -1 to quit): user types: 100
Enter an exam grade (type -1 to quit): user types: 50
Enter an exam grade (type -1 to quit): user types: -1
Exam average, including extra credit, is: 78
The equivalent letter grade is: C
Would you like to enter grades for another student (Y or N)? user types: N
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char ans = 'Y';
while (ans == 'Y') {
int total = 0;
int n = 0;
int grade = 0;
while (grade >= 0) {
cout << "Enter an exam grade (type -1 to quit): ";
cin >> grade;
if (!cin.fail() && grade >= 0) {
n++;
total += grade;
}
else {
cout << "Error: Grades must be an integer 0 or higher." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
char letter;
int avg = total / n + 3;
if (avg >= 90) {
letter = 'A';
}
else if (avg >= 80) {
letter = 'B';
}
else if (avg >= 70) {
letter = 'C';
}
else if (avg >= 60) {
letter = 'C';
}
else {
letter = 'F';
}
cout << "Exam average, including extra credit, is: " << avg << endl;
cout << "The equivalent letter grade is: " << letter << endl;
cout << "Would you like to enter grades for another student (Y or N)? ";
cin >> ans;
ans = toupper(ans);
}
}
Comments
Leave a comment