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;
        
      
      
      }

Hello mcap61,

Forgive me in advance as I do not have a compiler on this laptop; I cannot compile your code thus far, but will attempt to give you the best help that I can... take it for what it's worth.


One thing I notice, is you only create one 'student' object... my suggestion would be to create an array of 'students' where you could populate each struct to represent a single student. I would make this 'student' array global (declare it outside of int main()) so all functions would have access to it.

The way you have it now, you create a new 'student' object with each iteration of your while() loop..... this is fine, but you lose the ability to keep a running record of students. So, I would just make the following simple adjustment(s):

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;}ArrayOfStudents[42];

Then when you want to populate your array, do so like this:

//Populate ArrayOfStudents[ ]
loop(some type of loop, i++)
{
        //handle 'c-string' char arrays with <cstring> functions
        strcpy(ArrayOfStudents[i].name_first, firstname);
        strcpy(ArrayOfStudents[i].name_last, lastname);

         //single 'char' to 'char' assignment (no functions necessary)
        ArrayOfStudents[i].lettergrade = grade;

         //standard numercal assignments
          ArrayOfStudents[i].student_num = number;
          ArrayOfStudents[i].quiz1 = quizscore1;
          ArrayOfStudents[i].quiz2 = quizscore2;
          .....
          .....
          ....
          etc. etc. etc.

So, now that you have an array of 'student' objects.. you now have the ability to keep a running record of all the students and their attributes.. so whenever we want, we can go back to perform any calculations we need to:

//calculate a class average

//global variable
int class_ave = 0;

loooop(whatever loop you want to create; i++)
{
     ArrayOfStudents[i].total_points += class_ave;
}

class_ave /= number_of_students;

cout << "the class average is: " << class_ave;

So, the technique that I would like you to consider, is instead of making a new 'student record' with each loop iteration... try just making a array of 'student' objects to begin with. This way you can populate an array of 'student's and be able to keep a running record of them.

If anyone can explain this any better please do so.. or correct any errors in my logic, or if there is just a better way to do this in general, I am always open to suggestions.

thanks for your help, i really appreciate it, but we havent done arrays in class yet, is there a way to do it without arrays.

Your name_first and name_last variables are char arrays.
And I find it funny how your teacher would show you how to make structures before arrays but I guess I learned in a different order.
The best thing to use would be a vector container but using an array would work too.

Please any help would be appreciated.

I would do what Clinton Portis suggested. I wouldn't try doing this without some form of container otherwise you are really restricted with the amount of student records you have and the code becomes extremely bulky.

If you don't want to use an array, your code will be much less ellegant... so instead of creating a nice 'array of structs' in which ye' could populate.. you will probably end up having to created a bunch of 'student' objects individually

So.. you'll probably have to do something like this:

student      student_record_1, student_record_2, student_record_3,
               student_record_4, student_record_5, student_record_6,
               student_record_7, student_record_8, student_record_9,
               student_record_10, student_record_11, 
               student_record_12, student_record_13, 
               student_record_14, student_record_15,
               student_record_16, student_record_17, 
               student_record_18, student_record_19, 
               student_record_20, student_record_21,           
               student_record_22, student_record_23,  
               student_record_24, student_record_25, 
               student_record_26, student_record_27,
               student_record_28, student_record_29, 
               student_record_30;

Of course, you can create the 30 student objects much more easily by doing this:

student student_records[30];

So, when you want to populate your student records.. ye' could do something like this:

//function definition
void populate(student&);

int student_no = 0;

cout << "Enter student no. to populate: " ;
cin >> student_no;

     switch(student_no)
     {
          case 1:  populate(student_record_1);
                        break;
          case 2:  populate(student_record_2);
                       break;
          case 3:  populate(student_record_3);
                       break;
          case 4:  populate(student_record_4);
                       break;
          case 5:  populate(student_record_5);
                       break;
          case 6:  populate(student_record_6);
                       break;
          case 7:  populate(student_record_7);
                      break;
         case 8: populate(student_record_8);
                     break;
         case 9:  populate(student_record_9);
                      break;
         case 10:  populate(student_record_10);
                      break;
         case 11:  populate(student_record_11);
                      break;
          case 12:  populate(student_record_12);
                      break;
         case 13:  populate(student_record_13);
                      break;
         case 14:  populate(student_record_14);
                      break;
         case 15:  populate(student_record_15);
                      break;
         case 16:  populate(student_record_16);
                        break;
          case 17: populate(student_record_17);
                       break;
          case 18:  populate(student_record_18);
                       break;
          case 19:  populate(student_record_19);
                       break;
          case 20:  populate(student_record_20);
                       break;
          case 21:  populate(student_record_21);
                       break;
          case 22:  populate(student_record_22);
                      break;
         case 23: populate(student_record_23);
                     break;
         case 24:  populate(student_record_24);
                      break;
         case 25:  populate(student_record_25);
                      break;
         case 26:  populate(student_record_26);
                      break;
          case 27:  populate(student_record_27);
                      break;
         case 28:  populate(student_record_28);
                      break;
         case 29:  populate(student_record_29);
                      break;
         case 30:  populate(student_record_30);
                      break;

          default:  display_error_message();
     }

Now that you have your 30 student containers populated.. you now have the ability to go back and perform any calculations you want. I think you wanted to know how to calculate a "class average", you could probably do something like this:

int class_average = 0;

class_average += student_record_1.total_points;
class_average += student_record_2.total_points;
class_average += student_record_3.total_points;
class_average += student_record_4.total_points;
class_average += student_record_5.total_ponts;
class_average += student_record_6.total_points;
class_average += student_record_7.total_points;
class_average += student_record_8.total_points;
class_average += student_record_9.total_points;
class_average += student_record_10.total_points;
class_average += student_record_11.total_points;
class_average += student_record_12.total_points;
class_average += student_record_13.total_points;
class_average += student_record_14.total_points;
class_average += student_record_15.total_points;
class_average += student_record_16.total_points;
class_average += student_record_17.total_points;
class_average += student_record_18.total_points;
class_average += student_record_19.total_points;
class_average += student_record_20.total_points;
class_average += student_record_21.total_points;
class_average += student_record_22.total_points;
class_average += student_record_23.total_points;
class_average += student_record_24.total_points;
class_average += student_record_25.total_points;
class_average += student_record_26.total_points;
class_average += student_record_27.total_points;
class_average += student_record_28.total_points;
class_average += student_record_29.total_points;
class_average += student_record_30.total_points;

class_average /= number_of_students;

cout << "The class average is:  " << class_average;

I realize that you are in a precarious situation in not knowing how to use an array type container, but I hope you realize the inefficiency of not using an array vs. having to write out every single variable.

I felt so dirty writing the above code. I will have to go take a shower.

wow! thank you for explaining that.... that actually makes sense. After reading the chapter on arrays, i see how arrays are waayyyyy better and easier. I really don't like my professor right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.