really need help figuring out this program, it crashes during the getdata() function in main

any tips or suggestions appreciated

thank you

#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); // program seems to crash right here

    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;


    }
}

Recommended Answers

All 3 Replies

One problem you could be having, is you're calling your sub routines without initializing firstRecord. As a result it contains random data, which could be causing your routine to crash. Try something simple like int firstRecord = 0;.

If you still have a problem submit some test data which will highlight the problem.

In function getdat(...) you have declared a local instance of infile which will will be a null value.
Try parsing infile declare in function main() as a parameter in getdat(...).

line 124: Tests is an uninitialized pointer, attempting to read something into that pointer is destined to fail. Same with TestNo on line 133 -- no memory has been allocated to that pointer either.

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.