I am working on a program and it is not averaging properly. I cannot figure out how to fix this.
In short, the program is set up to add in grades from 0 to 100; any grades over 100 are invalid, and a grade entry of 999 should stop the loop and average the grades.
The problem I am having is that the program is counting the invalid grades, which is making the average calculation incorrect.
Here is the program:
Code:
#include <iostream>
using namespace std;
int main()
{
int x;
double grade, totgrade, count;
totgrade = 0;
count = 0;
cout << "Please enter grade: ";
cin >> grade;
do
{
if (grade > 100)
cout << "\nInvalid grade; ";
if (grade <= 100)
totgrade = totgrade + grade;
cout << "\nPlease enter next grade: ";
cin >> grade;
count++;
}
while (grade != 999);
do
{
if (grade == 999)
cout << "The average of these grades is: ";
cout << totgrade / count;
{
break;
}
}
while (grade == 999);
cin >> x;
return 0;
return EXIT_SUCCESS;
}
// TEST
//Please enter grade: 1
//
//Please enter next grade: 99
//
//Please enter next grade: 0
//
//Please enter next grade: 100
//
//Please enter next grade: 111
//
//Invalid grade;
//Please enter next grade: 999
//The average of these grades is: 40
Thanks in advance for any help you might be able to give me.