I have this program that calculates students grades. It asks for each students test scores individually then gives their total score and percent, then loops for the next student. But after the user is done inputing all the students scores, how do you create a function of some sort to add each student's total_scores up to find the class average.
#include <iostream>
using namespace std;
char ans;
struct student
{
char name_first[10], name_last[10], letter_grade;
double student_num, quiz_1, quiz_2, mid_exam, final_exam, total_points;
double percent_total;
};
void get_data(student& record);
void calc_grade(student& record);
void show_results(student& record);
int main()
{
char ans;
do
{
student record;
get_data(record);
calc_grade(record);
show_results(record);
cout << "Is there another student's scores that need to be processed?";
cout << endl;
cout << "Press 'y' or 'Y' for yes or 'n' to quit.";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
cout << "Bye";
return 0;
}
void get_data(student& record)
{
cout << "Please enter the following:\n";
cout << "Student ID number:";
cin >> record.student_num;
while (record.student_num < 1 || record.student_num > 99999)
{
cout << "Invalid Student ID number.\n";
cout << "Please enter again.\n";
cin >> record.student_num;
}
cout << "First name:";
cin >> record.name_first;
cout << "Last name:";
cin >> record.name_last;
cout << "Quiz #1 score:";
cin >> record.quiz_1;
while (record.quiz_1 < 0 || record.quiz_1 > 25)
{
cout << "Invalid quiz score.\n";
cout << "Please enter again.\n";
cin >> record.quiz_1;
}
cout << "Quiz #2 Score:";
cin >> record.quiz_2;
while (record.quiz_2 < 0 || record.quiz_2 > 25)
{
cout << "Invalid quiz score.\n";
cout << "Please enter again.\n";
cin >> record.quiz_2;
}
cout << "Midterm exam score:";
cin >> record.mid_exam;
while (record.mid_exam < 0 || record.mid_exam > 50)
{
cout << "Invalid exam score.\n";
cout << "Please enter again.\n";
cin >> record.mid_exam;
}
cout << "Final exam score:";
cin >> record.final_exam;
while (record.final_exam < 0 || record.final_exam > 100)
{
cout << "Invalid exam score.\n";
cout << "Please enter again.\n";
cin >> record.final_exam;
}
}
void calc_grade(student& record)
{
char letter_grade;
record.total_points = (record.quiz_1 + record.quiz_2 + record.mid_exam + record.final_exam);
record.percent_total = (100)*(record.total_points/200);
if (record.percent_total >= 90)
{
letter_grade = 'A';
letter_grade = record.letter_grade;
}
else if (record.percent_total >= 80 && record.percent_total < 90)
{
record.letter_grade = 'B';
}
else if (record.percent_total >= 70 && record.percent_total < 80)
{
record.letter_grade = 'C';
}
else if (record.percent_total >= 60 && record.percent_total < 70)
{
record.letter_grade = 'D';
}
else
{
record.letter_grade = 'F';
}
}
void show_results(student& record)
{
cout << "Summary:" << endl;
cout << "ID number:";
cout << record.student_num << endl;
cout << "Name:";
cout << record.name_first <<" "<< record.name_last << endl;
cout << "Quiz #1 score:";
cout << record.quiz_1 << endl;
cout << "Quiz #2 score:";
cout << record.quiz_2 << endl;
cout << "Midterm exam score:";
cout << record.mid_exam << endl;
cout << "Final exam score:";
cout << record.final_exam << endl;
cout << "Total points earned:";
cout << record.total_points << endl;
cout << "Percent Total:";
cout << record.percent_total << "%" << endl;
cout << "Grade:";
cout << record.letter_grade << endl;
}