Hi, I've been having trouble getting the average and letter grades to print and they are outputting as 0 .. I tried everything calling the average function etc. but I still get a zero. Any advice? I don't have any other problems just need help with the average function and how to get it to work and calculate. I posted the whole code just incase but what needs to be checked is the average function and how to call it to be placed for the student(also with lowest value dropped to calculate average)

input file sample:

Intro To Computer Science c++
SAL 343
JHG 344
John Adams
111223333 100 87 93 90 90 89 91 
Willy Smith
222114444 90 73 67 77 70 80 90 
Phil Jordan
777886666 100 80 70 -50 60 90 100



#include <iostream>
#include <string>
#include<fstream>
#include <iomanip>
#define CAP 25
using namespace std ;

struct Student{
    string  Course_Name,Course_Id, Course_Location;
    string  FirstName, LastName;
    float   Quiz, Test[6], Average;
    int Id;
    char Grade;
} ;

bool    Open_File(ofstream &fout);
bool    Open_File(ifstream &fin);
void    Read_Student(Student & Temp, ifstream & fin);
int     Read_List(Student & Temp,Student List[], int Max_Size);
void    Print_List(Student List[], int Size);
void    Print_Course(Student List[],ofstream&fout,int Size);
Student Read_Student(Student List[], Student & Temp);
void    Print_Student(Student & Temp);
float   calcAverage(Student & Temp);
void    Menu();
void    Process_Menu(Student List[], int Size, float Min);
int     Search(Student List[], int Size, string Target);
void    LookUp_Student(Student List[], int Size);
void    Print_Search(Student Temp);
char    Grade_it(Student & Temp);
float    Showmin();
void    SortChoice(Student List[],int Size);
void    SortByName(Student List[], int Size);
void    SortByAverage(Student List[], int Size);
void    Delete_Record(Student List[], int&Size);
void    Clean(Student List[], int Index, int Size);

int main(){
    float Min = 0;
    Student Stu[CAP], Temp;
    int Logical_Size = 0;
    Logical_Size = Read_List(Temp,Stu,CAP);
    if (Logical_Size > 0 )
    {

        Process_Menu(Stu,Logical_Size, Min);
    }
}

bool Open_File(ifstream &fin)
{
    string    File_Name;
    cout <<"Enter input file name: ";
    getline(cin, File_Name);
    fin.open(File_Name.c_str());
    if(fin.fail())
        return false ;
    else
        return true ;
}
bool Open_File(ofstream &fout)
{
    string    File_Name;
    cout <<"\nEnter output file name: ";
    getline(cin, File_Name);
    fout.open(File_Name.c_str());
    if(fout.fail())
        return false ;
    else
        return true ;
}
void Read_Student(Student & Temp , ifstream & fin){

    fin >> Temp.FirstName;
    fin >> Temp.LastName;
    fin >> Temp.Id;
    fin >> Temp.Quiz;
    for (int i = 0 ; i < 6 ; i++)
        fin >> Temp.Test[i];
    fin.ignore(10,'\n');
}
int Read_List(Student & Temp,Student List[], int Max_Size)
{
    ifstream fin;
    int i = 0;

    if (Open_File(fin))
    {
        getline(fin, List[i].Course_Name);
        getline(fin, List[i].Course_Id);
        getline(fin, List[i].Course_Location);
        Read_Student(List[i],fin);
        while(!fin.eof())
        {
            i++ ;
            if (i == Max_Size){
                cout << "\nArray is full.\n" ;
                break;
            }
            Read_Student(List[i],fin);
        }
    }
    else
    {
        cout <<"\nBad file. Did not open. Program terminated.\n";
        exit(0);
    }
    return (i);
}
void Print_List(Student List[], int Size)
{
    cout <<left << fixed << setprecision(2) <<showpoint <<endl ;
    cout << setw(19) << "Name" << setw(18) << "ID" << setw(18) << "Average" << setw(16)
    << "Grade" << endl;
    cout << "=============================================================" << endl;
    for(int i = 0; i < Size;i++)
        cout << setw(19) << List[i].LastName +", "+ List[i].FirstName
        << setw(18) << List[i].Id << setw(20) << List[i].Average
        << List[i].Grade << endl;
}
void Print_Course(Student List[],ofstream&fout,int Size){

    int i = 0;
    Open_File(fout);
    fout << "Course Name: " << List[i].Course_Name << endl;
    fout << "Course ID:  " << List[i].Course_Id << endl;
    fout << "Course Location: " << List[i].Course_Location << endl;
    fout <<left << fixed << setprecision(2) <<showpoint <<endl ;
    fout << setw(19) << "Name" << setw(18) << "ID" << setw(18)
    << "Average" << setw(16) << "Grade" << endl;
    fout << "=============================================================" << endl;
    for(int i = 0; i < Size;i++)
        fout << setw(19) << List[i].LastName +", "+ List[i].FirstName
        << setw(18) << List[i].Id << setw(20) << List[i].Average
        << List[i].Grade << endl;
    fout.close();
}
Student Read_Student(Student List[], Student & Temp){
    cout << "\nEnter the following student data:" << endl;
    cout <<"Student Name(e.g. Bob Smith): " ;
    cin >> Temp.FirstName >> Temp.LastName;
    cout <<"Student ID Number(without dashes): ";
    cin >> Temp.Id;
    cout << "Student Quiz Score: ";
    cin >> Temp.Quiz;
    if(Temp.Quiz < 0 || Temp.Quiz > 100)
    {
        cout << "Enter a valid quiz score between 0 to 100 for"
        << "\nQuiz Score: ";
        cin >> Temp.Quiz;
    }
    cout << setw(20) <<"Scores for Six Tests:" << endl ;
    for (int i = 0 ; i < 6 ; i++)
    {
        cout <<setw(7)<< " Test # " << i+1 << ": " ;
        cin >> Temp.Test[i];
        while (Temp.Test[i] < 0 || Temp.Test[i] > 100)
        {
            cout << "Enter a valid test score between 0 to 100 for"
            << "\nTest # " << i+1 << ": ";
            cin >> Temp.Test[i];
        }
    }
    float Total = 0;
    float Min;
    int Size = 0;
    Min = Temp.Test[0];
    for(int i = 0; i < 6; i++)
    {
        if (Temp.Test[i] < Min)
        {
            Min = Temp.Test[i];
        }
    }
    for(int i = 0; i < 6; i++)
    {
        Total+=Temp.Test[i];
    }

    Temp.Average = (Total-Min)/5*0.90 +(Temp.Quiz*0.10);
    Temp.Grade = Grade_it(Temp);
    ++Size;
    Print_Student(Temp);
    return Temp ;
}
void Print_Student(Student & Temp){
    cout << endl;
    cout << "Here is the Student Data you have entered: " << endl << endl;
    cout << "Name: " << Temp.FirstName << ", " << Temp.LastName << endl;
    cout << "ID Number: " << Temp.Id << endl;
    cout << "Grades for Quiz and Six Test scores " << endl;
    cout << "Quiz Score: " << Temp.Quiz << endl;
    for (int i=0; i < 6; i++)
    {
        cout << setw(7) << " Test Score # " << i+1 << ": " << Temp.Test[i] << " " << endl;
    }
    cout << "Average: " << Temp.Average << endl;
    cout << "Grade: "  << endl;
    cin.ignore(10,'\n');
}
float Showmin()
{
    Student Temp;
    float Min = Temp.Test[0];
    for(int i = 0; i < 6; i++)
    {
        if (Temp.Test[i] > Min)
        {
            Min = Temp.Test[i];
        }
    }
    return(Min);
}
void SortChoice(Student List[],int Size)
{
    Student Temp;
    char Choice;

    cout << "\nSelect the category in which you want to sort: ";
    cout <<"\nA. Alpabetical(Based on last name)\n";
    cout <<"B. Descending order(based on student average)\n";
    cout <<"Select your choice(A or B): ";
    cin >> Choice;
    if(Choice == 'A' || Choice == 'a')
    {
        SortByName(List,Size);
        Print_List(List,Size);
    }
    else if(Choice == 'B' || Choice == 'b')
    {
        SortByAverage(List,Size);
        Print_List(List,Size);
    }
}
void SortByName(Student List[], int Size)
{
    Student Temp;
    bool        Swap ;
    do{
        Swap = false ;
        Size -- ;
        for (int i = 0 ; i < Size ; i++)
            if (List[i].LastName > List[i+1].LastName){
                Temp = List[i] ;
                List[i] = List[i+1];
                List[i+1] = Temp ;
                Swap = true;
            }
    }while(Swap);
}
void SortByAverage(Student List[], int Size)
{
    Student Temp;
    bool        Swap ;
    do{
        Swap = false ;
        Size -- ;
        for (int i = 0 ; i < Size ; i++)
            if (List[i].Average > List[i+1].Average){
                Temp = List[i] ;
                List[i] = List[i+1];
                List[i+1] = Temp ;
                Swap = true;
            }
    }while(Swap);
}

float calcAverage(Student & Temp)
{
    float Total = 0;
    float Min;
    Min = Temp.Test[0];
    for(int i = 0; i < 6; i++)
    {
        if (Temp.Test[i] < Min)
        {
            Min = Temp.Test[i];
        }
    }
    for(int i = 0; i < 6; i++)
    {
        Total+=Temp.Test[i];
    }

    Temp.Average = (Total-Min)/5*0.90 +(Temp.Quiz*0.10);
    Temp.Grade = Grade_it(Temp);
   return (Temp.Average);
}
char Grade_it(Student & Temp)
{
    if (Temp.Average >= 0 && Temp.Average <= 59.9)
    {
        Temp.Grade = 'F';
    }
    else if (Temp.Average >= 60 && Temp.Average <= 69.9)
    {
        Temp.Grade = 'D';
    }
    else if (Temp.Average >= 70 && Temp.Average <= 79.9)
    {
        Temp.Grade = 'C';
    }
    else if (Temp.Average >= 80 && Temp.Average <= 89.9)
    {
        Temp.Grade = 'B';
    }
    else if (Temp.Average >= 90 && Temp.Average <= 100)
    {
        Temp.Grade = 'A';
    }
    return (Temp.Grade);
}

int Search(Student List[], int Size, string Target)
{
    for(int i = 0 ; i < Size ; i++)
        if (Target == List[i].LastName)
            return i;
    return -1;
}
void LookUp_Student(Student List[], int Size)
{
    string Target;
    int Index;
    cout << "\nEnter a name to search for (Last Name e.g. Smith): ";
    getline(cin,Target);
    Index = Search(List,Size,Target);
    if (Index == -1)
        cout << Target <<" is not on the list.\n" ;
    else
        Print_Search(List[Index]);
}
void Print_Search(Student Temp){
    cout << left << fixed << setprecision(2) << showpoint;
    cout << "Student Data for " << Temp.LastName +", "+Temp.FirstName << endl;
    cout << setw(20) << "Student Name:  "  << Temp.FirstName << " " << Temp.LastName   << endl;
    cout << setw(20) << "Student ID:  "      << Temp.Id           << endl;
    cout << setw(20) << "Student Average:  " << Temp.Average      << endl;
    cout << setw(20) << "Student Grade:  "   << Temp.Grade << endl;
}
void Clean(Student List[], int Index, int Size)
{
    List[Index].Id=0;
    List[Index].Quiz=0;
    for (int i = 0 ; i < Size ; i ++)
    List[Index].Test[i]=0;
    List[Index].Average=0;
    List[Index].Grade=0;
}
void Delete_Record(Student List[], int&Size)
{
    Student Temp;
    int Index;
    string Target;
    cout << "Enter student Last Name: ";
    cin >> Temp.LastName;
    Index = Search(List,Size,Target);
    if(Index!=-1)
    {
        if(Index == (Size-1))
           {
               Clean(List,Index,Size);
               --Size;
               cout << "The record was deleted.\n";
           }
    }
    else
        cout<<"No record to delete\n";

}
void Menu(){
    cout << endl;
    cout<<"____________________________________________________"<<"\n";
    cout<<"                         MENU                                  "<<"\n";
    cout<<"____________________________________________________"<<"\n";
    cout <<" \n 1) Read student data and update list\n";
    cout <<" 2) Print course content to file\n";
    cout <<" 3) Sort and print list: Alphabetical or Average\n";
    cout <<" 4) Print list of students registered in course\n";
    cout <<" 5) Look up a student and print all their details.\n";
    cout <<" 6) Add new student to the list\n";
    cout <<" 7) Delete student from the list\n";
    cout <<" 8) Exit";
    cout<<"\n____________________________________________________"<<"\n";
}
void Process_Menu(Student List[], int Size, float Min){
    Student Temp;
    ofstream fout;
    int  Choice ;
    do{
        Menu();
        cout <<"Please make a selection: ";
        cin >> Choice ;
        cin.ignore(10,'\n');

        switch(Choice){
            case 1: Read_List(Temp,List,Size);
                break ;
            case 2: Print_Course(List,fout,Size);
                break ;
            case 3: SortChoice(List,Size);
                    break;
            case 4: calcAverage(Temp);
                Print_List(List,Size);
                break;
            case 5: LookUp_Student(List,Size);
                break;
            case 6: Read_Student(List,Temp);
                break;
            case 7: Delete_Record(List,Size);
                break;
            case 8 : cout << "\n\n\tGood-Bye!" ;
                break;
            default: cout <<"\n\n***** ERROR! Invalid selection, must be between 1 and 4.  Try again.\n\n";
                break ;
        }
        cout <<"\n\n\n";
    }while(Choice !=8);
}

Looking at your code I find that you have to make a decision. Do you want to calculate the average once and store the value in the structure Student or do you want to calculate the student average each time you need it.

void Print_List(Student List[], int Size)
{
    cout <<left << fixed << setprecision(2) <<showpoint <<endl ;
    cout << setw(19) << "Name" 
    << setw(18) << "ID" 
    << setw(18) << "Average" 
    << setw(16) << "Grade" << endl;

    cout << "=============================================================" << endl;

    for(int i = 0; i < Size;i++)
        cout << setw(19) << List[i].LastName +", "+ List[i].FirstName
        << setw(18) << List[i].Id 
        << setw(20) << calcAverage(List[i])//I use the return value here but it also stores the result in student
        << List[i].Grade << endl;
}

Edited 3 Years Ago by gerard4143

This question has already been answered. Start a new discussion instead.