System Crashed at delete[] obj2

-1
/*
Name: Godfrey Lo, PRG355, Lab Project
Purpose: is to enter the number of months for the next test date of device 
with input of serial number,
Device Description, Test Description, recent date, and the number of months 
of two tests. At the end the
program must be searched by having the user to input the serial number and 
the next date, if these two are
valid everything in the device is listed out.
*/
#include<iostream>
#include<cmath>
#include <fstream>
#include<cstdlib>
#include<string>
using namespace std;
class Device {//Input and store Device Description and Serial Numbers
protected:
    string  serial_number;
    string device_description;
public:
    Device() {
        serial_number = ("6DCMQ32");
        device_description = ("TheDell");
    }
};
class Test {//Input and store Test Description, recent day, and month; 
Calculate the next day
protected:
    string Test_Description;
    static int recent_month, recent_day, recent_year, new_month;
    static int nmonth, next_month, next_day, next_year, max_day;
public:
    Test() {
        Test_Description = ("Virtual");
    }
    static void getMonth() {//Calculates the next/new month
        next_month = recent_month + nmonth;
        new_month = next_month % 12;
        if (next_month >= 12) {
            cout << "The next Date: " << new_month << " / ";
        }
        else {
            cout << "The next Date: " << next_month << " / ";
        }
    }
    static void getDay() {  //Calculates day of next month
        if (new_month == 4 || new_month == 6 || new_month == 9 || new_month == 11) {
            max_day = 30;
        }
        else if (new_month == 2) {
            max_day = 29;
        }
        else {
            max_day = 31;
        }
        if (recent_day > max_day) {
            cout << max_day << " / ";
        }
        else {
            cout << recent_day << " / ";
        }
    }
    static void getYear() {// Calculate the year of next month
        next_year = recent_year + next_month;
        if (next_year >= 12) {
            cout << recent_year + (next_month / 12) << endl;
        }
        else {
            cout << next_year << endl;
        }
    }
    static void getDate() {// Collects the output of each element of next date
        getMonth(), getDay(), getYear();
    }
};
int Test::recent_month, Test::recent_day, Test::recent_year, Test::new_month;
int Test::nmonth, Test::next_month, Test::next_day, Test::next_year, Test::max_day;
class Lab : public Device, public Test {
protected:
    static int n;
public:
    friend ostream& operator<<(ostream& out, Lab & lab);
    friend istream& operator>>(istream & in, Lab & lab);
    static void getN() {
        cout << "Enter the number of devices: ";
        cin >> n;
    }
    void getWrite() {
        getN();
        Lab *obj = new Lab[n];
        if (obj == 0) {
            cout << "Memory Error";
            exit(1);
        }
        for (int i = 0; i < n; i++) {
            cin >> obj[i];
        }
        cout << endl;
        ofstream myfile("Device.dat", ios::binary);
        myfile.write((char*)obj, n * sizeof(Lab));
        delete[] obj;
    }
    void getRead() {
        ifstream file2("Device.dat", ios::binary);
        string a;
        Lab *obj2 = new Lab[n];
        if (obj2 == 0) {
            cout << "Memory Error";
            exit(1);
        }
        file2.read((char*)obj2, n * sizeof(Lab));
        for (int i = 0; i < n; i++) {
            cout << obj2[i];
        }
        cout << endl;
        delete[] obj2;
    }
    int search(string SerialN) {
        int flag = 0;
        ifstream fin("Device.dat", ios::binary);
        Lab *obj2 = new Lab[n];
        fin.read((char *)obj2, n * sizeof(Lab));
        cout << "Enter Serial Number to search for: ";
        cin >> SerialN;
        for (int i = 0; i < n; i++) {
            flag = obj2[i].search(SerialN);
            if (flag == 0) {
                cout << obj2[i];
                break;
            }
        }
        delete[] obj2;
        return 0;
    }
};
ostream& operator<<(ostream& out, Lab & lab) {//Outputs everything in Device Class
    out << lab.device_description << endl;
    out << lab.serial_number << endl;
    out << lab.Test_Description << endl;
    lab.getDate();
    return out;
}
istream& operator>>(istream & in, Lab & lab) {// Inputs 
    cout << "Enter Device Desciption and Serial Number: ";
    getline(in, lab.device_description);
    getline(in, lab.serial_number);
    cout << "Enter Test Desciption: ";
    getline(in, lab.Test_Description);
    cout << "Enter the Number of months: ";
    in >> lab.nmonth;
    cout << "Enter the Most Recent Date(mm/dd/yyyy): ";
    in >> lab.recent_month >> lab.recent_day >> lab.recent_year;
    return in;
}
int Lab::n;
int main() {
    Lab L;
    string a;
    L.getWrite();
    L.getRead();
    L.search(a);
    getchar();
    getchar();
    return 0;
}

The output is correct, but the System Crashes after the program closes. Program says Unhandled exception at 0x00C1A958 in Lab Project.exe: 0xC0000005: Access violation writing location 0x00000001.

I know where the crash is at delete[] obj2 and I think does not like where it is placed because when I tried it after Lab *obj2=new Lab[n] it was fine but when it was after the second for loop is crashed. But I do not know how to fix this error and I need delete[] or else the value in obj2 will become my address.

I am using Microsoft Visual Studios 2017.

class
oop
search
file-io
asked on Stack Overflow Nov 20, 2018 by Goodzone Lo • edited Nov 22, 2018 by Goodzone Lo

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0