Hey there, well I'm working on this miniregister program that allows the user to add classes and insert students into those classes and whatnot, and I'm trying to get the average age of a class but I keep getting garbage for some reason, so if you could help me out I'd appreciate it.

Here's the code:

int DisplayClass (){
    //This function displays the summary of the class
     
     char name[30];  // Variable of the first name
     char last[30]; // Variable that contains the last name
     int age; // Variable of the age
     char gender;  // Variable of the gender
     int grade[5]; // Variable of the 5 grades
     int i = 0;
     
     int gradetotal[5]={0,0,0,0,0};
     int AgeTotal;    
          
     float gradeavg[5]; // Variable used to store the average of each grade
     float AgeAvg = 0;   // Variable used to store the average age
     int male = 0;  
     int female = 0; 
    
     ifstream Class;
     string ClassName;
    
     cout << "Enter a class: " << endl;
     cin >> ClassName;
    
     ClassName.append(".dat");
     Class.open(ClassName.c_str(),fstream::in);
    
    if(!Class){
         cout << "Opening file " << ClassName << " for reading \n\n";
         cout << "The file could not be opened! " << endl;
         cout << "Possible errors: " << endl;
         cout << "The class does not exist. " << endl;
         exit(1);   // Exits program.
     }
     
         Class >> name >> last >> age >> gender >> grade[0] >> grade[1] 
         >> grade[2] >> grade[3] >> grade[4];        
         while(!Class.eof()){
            
            gradetotal[0] += grade[0];
            gradetotal[1] += grade[1];
            gradetotal[2] += grade[2];
            gradetotal[3] += grade[3];
            gradetotal[4] += grade[4]; 
                        
            AgeTotal += age;
            
            if (gender == 'M' || gender == 'm'){
               male++;
            }
            else if (gender == 'F' || gender == 'f'){
               female++;
            }
            i++; 
            
            Class >> name >> last >> age >> gender >> grade[0] >> grade[1] 
            >> grade[2] >> grade[3] >> grade[4];                  
            }
         
            gradeavg[0] = gradetotal[0]/i;
            gradeavg[1] = gradetotal[1]/i;
            gradeavg[2] = gradetotal[2]/i;
            gradeavg[3] = gradetotal[3]/i;
            gradeavg[4] = gradetotal[4]/i; 
                        
            AgeAvg = AgeTotal/i;
         
         // Displays the actual class summary
         cout << "Exams Summary " << endl;                  
         cout << "Exam 1 Average:" << setprecision(2) << gradeavg[0] << endl;
         cout << "Exam 2 Average:" << setprecision(2) << gradeavg[1] << endl;
         cout << "Exam 3 Average:" << setprecision(2) << gradeavg[2] << endl;
         cout << "Exam 4 Average:" << setprecision(2) << gradeavg[3] << endl;
         cout << "Exam 5 Average:" << setprecision(2) << gradeavg[4] << endl << endl;
         
         cout << "Gender Summary " << endl;
         cout << "Females:" << female << endl;
         cout << "Males:" << male << endl << endl;
         
         cout << "Age Average:" << AgeAvg << endl;

         return 0;
}

If you need any more info, let me know. Thanks

> gradeavg[0] = gradetotal[0]/i;
I see here you're doing integer division, so you will get truncated answers.

If your grade scores are not integers to begin with, then your data types for reading the file are wrong.

An example file format would help

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.