Hi adrea-
I don't see the problem you are describing. The logic appears to be okay, except that as soon as you enter a zero the loop stops and the average is computed; you aren't giving it the option to consider zero grades. One way to fix this is to use a negative grade as the flag to stop counting:
Code:
int getAvgGrade()
{
//gets and accumulates the scores, then returns the average
int total_n_grade = 0;
int total_grades = 0;
int zero_grades = 0; // Added this
float n_grade = 0.0;
char letterGrade = ' ';
char grade = ' ' ;
cout << "Pleae enter the first numeric grade: ";
cin >> n_grade;
while (n_grade >= 0) // Changed this here
{
if (n_grade > 0) { // Added this if-else
total_grades++;
total_n_grade += n_grade;
} else
zero_grades++;
cout << "please enter the next numeric grade: ";
cin >> n_grade;
}//end while
if (total_grades >=0)
n_grade = total_n_grade/ total_grades;
cout << "Total number of all grades entered: "
<< total_grades + zero_grades << endl; // Changed this
cout << "Total number of valid grades entered: "
<< total_grades << endl; // Added this
return n_grade;
} //end of getAvGrade function
Also, you may want to have something else that deals with the special case where the average ends up being zero (in other words the student didn't take any tests at all. The program with the revisions to the getAvGrade function produced the following run:
Code:
akua% a.out
Pleae enter the first numeric grade: 100
please enter the next numeric grade: 100
please enter the next numeric grade: 100
please enter the next numeric grade: 0
please enter the next numeric grade: 100
please enter the next numeric grade: 100
please enter the next numeric grade: 0
please enter the next numeric grade: 0
please enter the next numeric grade: 100
please enter the next numeric grade: -1
Total number of all grades entered: 9
Total number of valid grades entered: 6
Average Grade: A
Hope this isn't too late, good luck on your final.