C++ HW2.exe has triggered a breakpoint

  breakpoints, c++, oop

I try to set integerSet set3 = set1.unionOfSets(set2);

I get the error "HW.exe" has triggered a breakpoint. The same thing happens when I try to

set integerSet set4 = set1.intersectionOfSets(set2); I do not get what the problem is. This is my code:

class integerSet {
private:
    int* set;
public:
    integerSet();
    ~integerSet();
    integerSet unionOfSets(const integerSet&);
    integerSet intersectionOfSets(const integerSet&);
    void emptySet();
    void insertElement(int);
    void deleteElement(int);
    void inputSet();
    void printSet() const;
    bool isEqual(const integerSet&) const;
    int validInput(int);
    void setSize(int);
    int getSize();
};

integerSet::integerSet()  
{
    set = new int[1000];
    emptySet();
}
integerSet::~integerSet()
{
    delete[]set;
}
void integerSet::emptySet()
{
    for (int i = 0; i < 1000; i++) {
        set[i] = 0;
    }
}
integerSet integerSet::unionOfSets(const integerSet& r)
{
    integerSet unionSet;
    for (int i = 0; i < 1000; i++) {
        if (set[i] == 1 || r.set[i] == 1) {
        unionSet.set[i] = 1;
        }
    }
    return unionSet;
}
integerSet integerSet::intersectionOfSets(const integerSet& r)
{
    integerSet intersect;
    for (int i = 0; i < 1000; i++) {
        if (set[i] == 1 && r.set[i] == 1) {
            intersect.set[i] = 1;
        }
    }
    return intersect;
}
void integerSet::insertElement(int k)
{
    if (validInput(k)) {
        set[k] = 1;
    }
    else {
        cout << "invalid number" << endl;
    }
}
void integerSet::deleteElement(int m) 
{
    if (validInput(m)) {
        set[m] = 0;
}
else {
    cout << "invalid number" << endl;
}
}
void integerSet::printSet() const
{
    bool empty = true;

    for (int i = 0; i < 1000; ++i) {
        if (set[i]) {
            cout << setw(3) << i;
            empty = false;
        }
    }
    if (empty) {
        cout << "---";
    }
}
bool integerSet::isEqual(const integerSet& r) const
{
    for (int i = 0; i < 1000; i++) {
        if (set[i] != r.set[i]) {
            return false;
        }
    }
    return true;
}

int integerSet::validInput(int n)
{
    return n >= 0 && n <= 1000;
}
void integerSet::inputSet()
{
    int num;

    cout << "Enter elements. Enter -1 to stop entering" << endl;
    do {
        cin >> num;

        if (validInput(num))
            set[num] = 1;
        else if (num != -1)
            cerr << "n Invalid Element";
    } while (num != -1);
}

int main() {
    integerSet set1;
    cout << "Set 1 :" << endl;
    set1.inputSet();

    integerSet set2;
    cout << "Set 2 :" << endl;
    set2.inputSet();

    integerSet set3 = set1.unionOfSets(set2);
    cout << "Union of set 1 and 2 :" << endl;
    set3.printSet();

    integerSet set4 = set1.intersectionOfSets(set2);
    cout << "Intersecting set 1 and 2 :" << endl;
    set4.printSet();

    if (set1.isEqual(set2)) {
        cout << "set 1 and 2 are equal" << endl;
    }
    else {
        cout << "set 1 and 2 are not equal" << endl;
    }

    cout << "adding 50 to set 1: " << endl;
    set1.insertElement(50);
    cout << "Set 1 :";
    set1.printSet();

    cout << "nRemoving 50 from set 1: " << endl;
    set1.deleteElement(50);
    cout << "Set 1 :";
    set1.printSet();

    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT