Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

KEEP the ReadHeader Function!! #include #include #include #include #include usin

ID: 3556368 • Letter: K

Question

KEEP the ReadHeader Function!!

#include
#include
#include
#include
#include


using namespace std;

struct StudentData
{
   string Name;

   int Idnum;

   int* Tests;

   double Average;

   char Grade;
};

void ReadHeader(int&, ifstream&);

StudentData DynamicallyAllocate(int);

void GetStudentData(StudentData[], int, int);

void calculateGrade(StudentData[], int, int);

void displayResults(StudentData[], int);

int main()


{

   int firstRecord;

   ifstream infile;

   ReadHeader(firstRecord,infile);

   int numberOfTests,
       numberOfstudents;


   //StudentData *StudentInfo = new StudentData[firstRecord];// Dynamically allocate a new array


   cout <<"Enter number of tests: ";
   cin >> numberOfTests;

   cout <<"Enter number of students: ";
   cin >> numberOfstudents;

   StudentData *dataOfStudents = new StudentData[numberOfstudents];

   int student = 0;

   GetStudentData(dataOfStudents, numberOfstudents, numberOfTests);

   calculateGrade(dataOfStudents,numberOfstudents,numberOfTests);

   displayResults(dataOfStudents, numberOfstudents);

   return 0;


}

void ReadHeader(int& firstRecord, ifstream& infile)
{
   string filename;

   cout <<"Enter the .txt file you wish to open: ";
   cin >> filename;

   infile.open(filename.c_str());

   while(!infile)
   {
       cout <<"Please re-enter a valid .txt file to open: ";
       cin >> filename;

       infile.open(filename.c_str());
   }


   if(infile)
   {
       infile >> firstRecord;
       if (firstRecord < 5)
       {
           cout <<"Error! program is now ending" << endl;
           exit(1);
       }

   }


}

StudentData DynamicallyAllocate(int firstRecord)
{
   StudentData *StudentInfo = new StudentData[firstRecord];

   return *StudentInfo;
}

void GetStudentData(StudentData StudentInfo[], int NumOfStudents, int NumOfTests)
{

   int testScore;

   for(int student = 0; student < NumOfStudents; student++)
   {
       cout << endl;

       cout << "Enter the name of the student " << (student+1) << ":";
       cin >> StudentInfo[student].Name;

       cout <<"Enter the id number of the student " << (student +1) << ":";
       cin >> StudentInfo[student].Idnum;


       for (int test = 0; test < NumOfTests; test++)
       {

           cout << " Enter the score for test " << (test+1) << ":";
           cin >> testScore;

           while(testScore < 0)
           {
               cout <<"Enter a positive test score value: ";
               cin >> testScore;
           }

           StudentInfo[student].Tests[test] = testScore;


       }

   }

}

void calculateGrade(StudentData StudentInfo[], int NumOfStudents, int NumOfTests)
{
   double TotalScore;

   for( int student = 0; student < NumOfStudents; student++)
   {
       TotalScore = 0;
       for(int test = 0; test < NumOfTests; test++)
           TotalScore += StudentInfo[student].Tests[test];

       StudentInfo[student].Average = TotalScore / NumOfTests;

       if(StudentInfo[student].Average > 90)
           StudentInfo[student].Grade = 'A';

       else if(StudentInfo[student].Average > 80)
           StudentInfo[student].Grade = 'B';

       else if(StudentInfo[student].Average > 70)
           StudentInfo[student].Grade = 'C';

       else if(StudentInfo[student].Average > 60)
           StudentInfo[student].Grade = 'D';

       else
           StudentInfo[student].Grade = 'F';

   }

}

void displayResults(StudentData StudentInfo[], int NumOfStudents)
{

   cout <<"Course Grades" << endl;

   cout << setw(20) << "Name: " << setw(12) << "ID Number" << endl;
   cout << setw(12) << setprecision(4)
       << "Average" << setw(6) << "Grade" << endl;

   for(int student = 0; student < NumOfStudents; student++)
   {
       cout << setw(20) << StudentInfo[student].Name << setw(12) << StudentInfo[student].Idnum << endl;
       cout << setw(12) << StudentInfo[student].Average << setw(6) << StudentInfo[student].Grade << endl;


   }
}

here is the INPUT file that the program reads

10

Mark 2345A78 6 78 67 98 34 78 69
Emily 3246B59 5 89 56 74 59 98
Yao 8942A85 7 98 75 69 89 85 78 95
Claudette 6587C69 5 56 45 63 75 60
Niklos 5874B58 6 69 68 52 69 75 74
Channah 4587B32 7 98 95 100 96 89 96 94
Parissa 8524C84 5 98 96 85 91 90
Rodrick 9856C65 5 56 75 84 65 75
Chance 5648A67 6 100 89 96 89 82 86
Melissa 8786C78 5 80 63 75 77 72
Niels 4854A78 6 48 56 85 65 42 75

Explanation / Answer

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

struct student
{
   string name;
   string id;
   int num;
   int test[10];
   double avg;
   char grade;
};
ifstream infile;
void ReadHeader(int& firstRecord, ifstream& infile)
{
string filename;
cout <<"Enter the .txt file you wish to open: ";
cin >> filename;
infile.open(filename.c_str());
while(!infile)
{
cout <<"Please re-enter a valid .txt file to open: ";
cin >> filename;
infile.open(filename.c_str());
}


infile >> firstRecord;
if (firstRecord < 5)
{
cout <<"Error! program is now ending" << endl;
exit(1);
}

}

student* allo_dyn(int n)
{
   student * info = new student[n];
   return info;
}

int getdata(student info[], int n)
{
   int i = 0;
   while(n--)
   {
       if(infile.eof()) return 0;
       infile>>info[i].name;
       infile>>info[i].id;
       infile>>info[i].num;
       int sum = 0;
       for(int j =0 ; j<info[i].num ;j++)
       {
           infile >> info[i].test[j];
           sum += info[i].test[j];
       }
       info[i].avg = double(sum)/(double)info[i].num;
       i++;
   }
   return 1;
}

void cal_grade(student info[],int n)
{
   for( int student = 0; student < n; student++)
{
if(info[student].avg > 90)
info[student].grade = 'A';
else if(info[student].avg > 80)
info[student].grade = 'B';
else if(info[student].avg > 70)
info[student].grade = 'C';
else if(info[student].avg > 60)
info[student].grade = 'D';
else
info[student].grade = 'F';
}
}

void display(student StudentInfo[], int NumOfStudents)
{
cout <<"Course Grades" << endl << endl;
cout << setw(20) << "Name: " << setw(12) << "ID Number";
cout << setw(12) << setprecision(4)
<< "Average" << setw(6) << "Grade" << endl;
for(int student = 0; student < NumOfStudents; student++)
{
cout << setw(20) << StudentInfo[student].name << setw(12) << StudentInfo[student].id ;
cout << setw(12) << StudentInfo[student].avg << setw(6) << StudentInfo[student].grade << endl;

}
}
void dealloc(student info[],int n)
{
delete [] info;
}
int main()
{
int firstRecord;
ReadHeader(firstRecord,infile);

student *info = allo_dyn(firstRecord);
int ch =getdata(info,firstRecord);
if(ch == 0)
{
    cout <<"Error! program is now ending" << endl;
exit(1);
}

cal_grade(info,firstRecord);
display(info,firstRecord);
dealloc(info,firstRecord);
return 0;
}