Objective: Implement nested loops in a program to assign grades to students in a class at the end of a semester.
When entering the scores, if a student had an excused absence for a score it will be entered as -1. If a student has 2 or more excused absences, then do not compute the average and give the student a grade of I. If the student as less than 2 excused absences then compute the average and determine the grade. Of course for a student with 1 excused absence the average should not count the –1 score. For example, if the input scores for a student were 53, -1, 49 and 50, your program would ignore the -1, since this is the student’s only absence. or this student average will be (53+49+50)/3. The grade of S should be assigned if the student's average is 50.0 or above and U if it is below 50.0.
#include <iostream>
usingnamespace std;
int main()
{
int numTests;
char choice;
cout << "Enter the ID and scores for each student. If a student has \n""an excused absence then enter -1 for that score. " << endl << endl;
cout << "Enter the number of scores for this semester:" << endl << endl;
cin >> numTests;
cout << "=========================================================" << endl;
cout << "Add a student (Y to continue, any other character to end): ";
cin >> choice;
while (choice == 'Y')
{
int ID;
cout << "Enter a student's ID: ";
cin >> ID;
double score, sum = 0, avgScore;
int excused = 0, grade;
for (int i = 0; i<numTests; i++)
{
// input score
cout << "Enter a Score: ";
cin >> score;
// keep track of excuses
if (score == -1)
{
sum = sum - score;
}
else
{
sum = sum + score; // only if score is >=0
}
}
//avgScore = avgScore / (# of non-excused scores)
avgScore = sum / numTests;
if (avgScore > 50.0)
{
grade = 'S';
}
elseif (avgScore < 50.00)
{
grade = 'U';
}
else
{
grade = 'I';
}
cout << " ID=" << ID << " Avg=" << avgScore << " Grade=" << grade << endl;
cout << "=========================================================" << endl;
cout << "Add a student (Y to continue, any other character to end): ";
cin >> choice;
}
system("pause");
return 0;
}
What am I doing wrong that it won't take away the -1's that are entered or output the correct grade?