0

I have an if statement that whenever I implement it, it throws a "read access violation" exception, even though all my other if-statements look pretty similar and work. Everything else works perfectly, so it perplexes me as to why this is happening.

if (records[i].studentArray[j].id == records[i-1].studentArray[j].id) //these two
                records[i].retained++;

Full function

void readFile()
{
//Function reads the file and displays all results
int i = 0;
int j = 0;
int count;
Semester records[25]; //only holds up to 25 semesters
Student studentArray;
string semesterName;
ifstream roster;
roster.open ("records.txt");
if (roster.is_open())
{
    roster >> semesterName;
    while(!roster.eof())
    {

        roster >> count;
        records[i].semesterName = semesterName; 
        cout << semesterName;
        records[i].numStudents = count;
        records[i].studentArray = new Student[count];

        for (j = 0; j < count; j++)
        {
            roster >> records[i].studentArray[j];
            if (records[i].studentArray[j].year == "FY")
                records[i].fycount++;

            else if (records[i].studentArray[j].year == "SO")
                records[i].socount++;

            else if (records[i].studentArray[j].year == "JR")
                records[i].jrcount++;

            else if (records[i].studentArray[j].year == "SR")
                records[i].srcount++;

            if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)
                records[i].retained++;
        }
        cout << endl;
        cout << "FY: " << records[i].fycount << endl;
        cout << "SO: " << records[i].socount << endl;
        cout << "JR: " << records[i].jrcount << endl;
        cout << "SR: " << records[i].srcount << endl;

        cout << "FY gain/loss: " << records[i].fycount - records[i - 1].fycount << endl;
        cout << "SO gain/loss: " << records[i].socount - records[i - 1].socount << endl;
        cout << "JR gain/loss: " << records[i].jrcount - records[i - 1].jrcount << endl;
        cout << "SR gain/loss: " << records[i].srcount - records[i - 1].srcount << endl;

        cout << "Percentage gained/lost: " << static_cast<double>(records[i].numStudents - records[i - 1].numStudents) / records[i - 1].numStudents * MULTIPLIER << "%" << endl;

        cout << "Number of students retained: " << records[i].retained << endl;

        cout << endl;

        delete[] records[i].studentArray;
        roster >> semesterName;
        i++;
    }

    roster.close();
}
else
    cout << "Error: File not found" << endl;
}

Student and semester classes in the .h files

struct Semester
{
string semesterName;
int numStudents;
int fycount = 0;
int socount = 0;
int jrcount = 0;
int srcount = 0;
int retained = 0;
Student *studentArray;
};

class Student
{
public:
    string id;
    string year;
    string name;
    string lastName;
    friend istream & operator>> (istream &in, Student &s);
};

Thank you!

Deneb
  • 3
  • 2
  • 2
    when i == 0 you cannot access i-1 element – Artemy Vysotsky Sep 04 '17 at 04:10
  • ***while(!roster.eof())*** https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Sep 04 '17 at 04:42
  • Just in passing, the error message here is an **operating system "exception"**, not a **C++ "exception"**. Despite having the same name, these two have nothing to do with each other. The operating system does not throw exceptions. Only your C++ program can do that, and only when it says `throw`. (Well, several years ago Microsoft had a rather misguided attempt to integrate OS exceptions with C++ exceptions, but they seem to have abandoned that as unworkable) – Pete Becker Sep 04 '17 at 12:58

1 Answers1

0

Do a trace of your application. You will see that i is set to 0 initially and hence -1th location will be invalid in an array.

if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)

That's the source of the error. Better change it to:

if (i > 0 && records[i].studentArray[j].id == records[i-1].studentArray[j].id)

Hope this helps.

Sri Harsha Chilakapati
  • 10,962
  • 6
  • 46
  • 87