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

I really need help making this program work. it stops working in main at the get

ID: 3556449 • Letter: I

Question

I really need help making this program work. it stops working in main at the getData function

it stops working in main

#include<iostream>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<fstream>


using namespace std;

struct StudentData
{
   string Name;

   int Idnum;

   int* Tests;
  
   int* TestNo;

   double Average;

   char Grade;
};

void ReadHeader(int&, ifstream&);

StudentData* DynamicallyAllocate(int);

int getdata(StudentData[], int);

void calculateGrade(StudentData[], int);

void displayResults(StudentData[], int);

int main()


{

   int firstRecord;
  

   ifstream infile;

   ReadHeader(firstRecord,infile);


   StudentData *StudentInfo = DynamicallyAllocate(firstRecord);// Dyanmically allocate memory


   int student = 0;


   getdata(StudentInfo,firstRecord);

   calculateGrade(StudentInfo,firstRecord);

   displayResults(StudentInfo, firstRecord);

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

int getdata(StudentData StudentInfo[], int n)
{
   ifstream infile;


   int i = 0;

   int numberOfTests;

   while(n--)
   {
       if(infile.eof()) return 0;
       infile >> StudentInfo[i].Name;
           infile >> StudentInfo[i].Idnum;
           infile >> *StudentInfo[i].Tests;

           int sum = 0;

       numberOfTests = *StudentInfo[i].Tests;

           for (int j = 0; j < numberOfTests;j++)
           {

               infile >> StudentInfo[i].TestNo[j];
               sum += StudentInfo[i].TestNo[j];
           }

       StudentInfo[i].Average = double(sum)/(double)*StudentInfo[i].Tests;
       i++;
   }

   return 1;


  
}


void calculateGrade(StudentData StudentInfo[], int NumOfTests)
{
   for( int student = 0; student < NumOfTests; student++)
   {
       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;


   }
}

Explanation / Answer

#include<iostream>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<fstream>


using namespace std;

struct StudentData
{
   string Name;

   int Idnum;

   int Tests;

   int* TestNo;

   double Average;

   char Grade;
};

void ReadHeader(int&, ifstream&);

StudentData* DynamicallyAllocate(int);

int getdata(ifstream&, StudentData[], int);

void calculateGrade(StudentData[], int);

void displayResults(StudentData[], int);

int main()


{

   int firstRecord;

   ifstream infile;

   ReadHeader(firstRecord,infile);


   StudentData *StudentInfo = DynamicallyAllocate(firstRecord);// Dyanmically allocate memory


   int student = 0;


   getdata(infile,StudentInfo,firstRecord);

   calculateGrade(StudentInfo,firstRecord);

   displayResults(StudentInfo, firstRecord);

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

int getdata(ifstream& infile,StudentData StudentInfo[], int n) // u have to pass valid ifstream handle here.

// this is the bug because of which code is not working...
{
   int i = 0;
   int numberOfTests;
   while(n--)
   {
       if(infile.eof()) return 0;
       infile >> StudentInfo[i].Name;
           infile >> StudentInfo[i].Idnum;
           infile >> StudentInfo[i].Tests;

           int sum = 0;
           int numberOfTests = StudentInfo[i].Tests;
       StudentInfo[i].TestNo = new int[numberOfTests] ;

           for (int j = 0; j < numberOfTests;j++)
           {

               infile >> StudentInfo[i].TestNo[j];
               sum += StudentInfo[i].TestNo[j];
           }

       StudentInfo[i].Average = double(sum)/(double)numberOfTests;
       i++;
   }

   return 1;

}


void calculateGrade(StudentData StudentInfo[], int NumOfTests)
{
   for( int student = 0; student < NumOfTests; student++)
   {
       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;


   }
}