Exception thrown at 0x798AEDBE (ucrtbased.dll) in eXcelTuitionCentre.exe: 0xC0000005: Access violation reading location 0xD21D9227

0

I have an error as what the title have said. This is my code.

This is my function, I want to call modifyTutor() function from my other function. The modifyTutor also called viewTutor() function to get all the tutor list into array of object. Then I want to modify the data from specific array of object and save it to textfile.

 TutorClass* TutorClass::viewTutor() {
    string line;
    ifstream file;
    FileClass count;
    int size = count.countRecord();
    TutorClass* tutorlist = new TutorClass[size];
    file.open("TutorRecord.txt");
    int index = 0;
    while (getline(file, line)) {
        istringstream ss(line);
        string data;
        string array[12];
        int x = 0;
        while (getline(ss, data, '|')) {
            array[x] = data;
            x = x + 1;
        }
        int rate = stoi(array[2]);
        tutorlist[index] = TutorClass(array[0], array[1], rate, array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11]);
        index++;
    }
    file.close();
    return tutorlist;

void TutorClass::modifyTutor(TutorClass Tutor,string option,string
value) {
        TutorClass tut = Tutor;
        FileClass record;
        int size = record.countRecord();
        TutorClass* tutorarray = viewTutor();
        ofstream txtfile("TutorRecord.txt");
        for (int i = 0; i < 5; i++) {
            if (tutorarray[i].tutorid == tut.tutorid) {
                if (option == "1") {
                    tut.rating = stoi(value);
                    tutorarray[i].rating = stoi(value);
                }
                else if (option == "2") {
                    tut.phonenum = value;
                    tutorarray[i].phonenum = value;
                }
                else if (option == "3") {
                    tut.address = value;
                    tutorarray[i].address = value;
                }
                else if (option == "4") {
                    string date = value.substr(0, 2);
                    string month = value.substr(2, 2);
                    string year = value.substr(4, 4);
                    tut.dateterminated = date+"-"+month+"-"+year;
                    tutorarray[i].dateterminated = date + "-" + month + "-" + year;
                }
            }
            txtfile << tutorarray[i].tutorid << "|" << tutorarray[i].tutorname << "|" << tutorarray[i].rating << "|" << tutorarray[i].phonenum << "|" << tutorarray[i].address << "|" << tutorarray[i].datejoin << "|" << tutorarray[i].dateterminated << "|" << tutorarray[i].subjectid << "|" << tutorarray[i].subjectname << "|" << tutorarray[i].salary << "|" << tutorarray[i].tuitionid << "|" << tutorarray[i].tuitionname << endl;

        }
        txtfile.close();
        cout << "Update Successfully"<<endl;
     }

And this is the code where i call the modifyTutor method.

void modifyTutor(TutorClass tutor) {
TutorClass tutorr = tutor;
bool flagmodify = false;
while (flagmodify == false) {
    cout << "==================================================================================================" << endl;
    cout << "                                      MODIFY TUTOR RECORD" << endl;
    cout << "==================================================================================================" << endl;
    cout << "\nTutor ID\t\t: " << tutor.tutorid << endl;
    cout << "Tutor Name\t\t: " << tutor.tutorname << endl;
    cout << "[1] Rating\t\t: " << tutor.rating << endl;
    cout << "[2] Phone Number\t: " << tutor.phonenum << endl;
    cout << "[3] Address\t\t: " << tutor.address << endl;
    cout << "[4] Date Terminated\t: " << tutor.dateterminated << endl;
    cout << "[0] Enter '0' to go back." << endl << endl;
    cout << "Enter keyword (1-4) which data to modify: ";
    string option;
    string value;
    TutorClass tut;
    getline(cin, option);
    if (option == "1" || option == "2" || option == "3" || option == "4" || option == "0") {
        flagmodify = true;
        if (option == "0") {
            system("CLS");
            searchTutorbyID();
        }
        else if(option=="1"){
            bool flagrating = false;
            while (flagrating == false) {
                cout << "\nEnter new Rating (1-5): ";
                getline(cin, value);
                if (value == "1" || value == "2" || value == "3" || value == "4" || value == "5") {
                    system("CLS");
                    tut.modifyTutor(tutorr, option, value);
                    flagrating = true;
                    tutorr.rating = stoi(value);
                    modifyTutor(tutorr);
                }
                else {
                    cout << "Invalid input (1-5)!" << endl;
                }
            }
        }
        else if (option == "2") {
            bool flagphone = false;
            while (flagphone == false) {
                cout << "Enter new phone number: ";
                getline(cin, value);
                for (int i = 0; i < value.length(); i++) {
                    if (isdigit(value[i])) {
                        flagphone = true;
                    }
                    else {
                        flagphone = false;
                        cout << "\nPlease enter a valid phone number!\n\n";
                        break;
                    }
                }
                if (flagphone == true) {
                    system("CLS");
                    tut.modifyTutor(tutorr, option, value);
                    tutorr.phonenum = value;
                    modifyTutor(tutorr);
                }
            }
        }
        else if (option == "3") {
            bool flagaddress = false;
            while (flagaddress == false) {
                cout << "Enter address: ";
                getline(cin, value);
                if (value != "") {
                    flagaddress = true;
                    system("CLS");
                    tut.modifyTutor(tutorr, option, value);
                    tutorr.address = value;
                    modifyTutor(tutorr);
                }
            }
        }

        else if (option == "4") {
            bool flagdate = false;
            while (flagdate == false) {
                cout << "\nEnter new Date Terminated (format: ddMMyyyy, .eg: 05122020): ";
                getline(cin, value);
                if (value.length() == 8) {
                    string date = value.substr(0, 2);
                    string month = value.substr(2, 2);
                    string year = value.substr(4, 4);
                    try {
                        int d = stoi(date);
                        int m = stoi(month);
                        int y = stoi(year);
                        if (d >= 1 && d <= 31 && m >= 1 && m <= 12 && y >= 2020) {
                            system("CLS");
                            tut.modifyTutor(tutorr, option, value);
                            flagdate = true;
                            tutorr.dateterminated = date+"-"+month+"-"+year;
                            modifyTutor(tutorr);
                        }
                        else {
                            cout << "Please enter the valid date (format: ddMMyyyy, .eg: 05122020)!"<<endl;
                        }
                    }
                    catch(...){
                        cout << "Please enter the correct date (format: ddMMyyyy, .eg: 05122020)!"<<endl;
                    }
                }
                else {
                    cout << "Please enter the correct date (format: ddMMyyyy, .eg: 05122020)!"<<endl;
                }
            }

        }
    }
    else {
        system("CLS");
        cout << "Invalid keyword (1-4)" << endl;
    }
}

}

The error is in this line

 txtfile << tutorarray[i].tutorid << "|" << tutorarray[i].tutorname << "|" << tutorarray[i].rating << "|" << tutorarray[i].phonenum << "|" << tutorarray[i].address << "|" << tutorarray[i].datejoin << "|" << tutorarray[i].dateterminated << "|" << tutorarray[i].subjectid << "|" << tutorarray[i].subjectname << "|" << tutorarray[i].salary << "|" << tutorarray[i].tuitionid << "|" << tutorarray[i].tuitionname << endl;

I have tried with everything i can but still could not solve this problem. Can you please check which code is wrong?

class
pointers
visual-c++
access-violation
asked on Stack Overflow Mar 26, 2020 by Richard Tiozard

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0