Write, compilc, and run a C++ program that continuously requests a grade to be e
ID: 440073 • Letter: W
Question
Write, compilc, and run a C++ program that continuously requests a grade to be entered. If the grade is less than 0 or greater than 100, your program should print a message informing the user that an invalid grade has been entered; else, the grade should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute and display the average of the valid grades entered. Run the program written in Exercise 2a on a computer and verify the program by using appropriate test data.Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int grade, counter = 0, total = 0;
double avg;
while(1){
cout <<"Enter a grade to be averaged: ";
cin >> grade;
if(grade == 999){
break;
}
else if (grade < 0 || grade > 100){
cout << "Invalid grade entered! Grade range (0-100)" << endl;
}
else{
total += grade;
counter++;
}
}
avg = (double)total/counter;
cout.precision(2);
cout << fixed << " Average of all grades enter: "<< avg << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.