簡體   English   中英

C ++:引發異常:按數組讀取訪問沖突

[英]C++: Exception thrown: Read access violation by array

我有一個if語句,即使我執行的所有其他if語句看起來都非常相似且可以正常工作,只要執行它,它都會引發“讀取訪問沖突”異常。 其他所有東西都運行良好,所以讓我困惑為什么會這樣。

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

功能齊全

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

.h文件中的學生和學期班

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

謝謝!

跟蹤您的應用程序。 您將看到i最初設置為0,因此數組中的第-1個位置將無效。

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

這就是錯誤的根源。 最好將其更改為:

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM