Start w/o debugging outputting different results than debugger

Ok, so I'm new to this posting for help on a forum deal. I've searched and searched, but can't seem to find why I'm running into this issue.

I'm working on an IntegerSet class for school, that uses a dynamic array. Per the directions for the assignment "A set is represented internally as a fixed array of ones and zeros or true and false. Array element a[ i ] is 1 or true if integer i is in the set. Array element a[ j ] is 0 or false if integer j is not in the set."

Here is my code:
Header:
// IntegerSetEC.h -- IntegerSet class specification file

#ifndef INTEGERSETEC_H
#define INTEGERSETEC_H

using namespace std;

const int MIN_SIZE = 128;
const int MAX_SIZE = 10000;

class IntegerSet
{
public:
IntegerSet operator+(const IntegerSet &s) const;
IntegerSet operator*(const IntegerSet &s) const;
bool find(int m) const;
IntegerSet &insert(int m);
IntegerSet &remove(int m);
friend istream &operator>>(istream &in, IntegerSet &s);
friend ostream &operator<<(ostream &out, const IntegerSet &s);
friend bool operator==(const IntegerSet &s1, const IntegerSet &s2);
friend bool operator!=(const IntegerSet &s1, const IntegerSet &s2);
friend bool operator<=(const IntegerSet &s1, const IntegerSet &s2);
friend bool operator>=(const IntegerSet &s1, const IntegerSet &s2);
// extra credit
IntegerSet & operator=(const IntegerSet &s);
IntegerSet(const IntegerSet &s);
IntegerSet();
IntegerSet(int size);
IntegerSet(int arr[], int n);
~IntegerSet() {if (arraySize > 0) delete [] aPtr;}

private:
int *aPtr;
int arraySize;
};
#endif

Implementation file:
// IntegerSetEC.cpp -- IntegerSet class function implementation file
#include <iostream>
#include <cstdlib>
#include "IntegerSetEC.h"

using namespace std;

IntegerSet::IntegerSet() // default
{
arraySize = MIN_SIZE;
aPtr = new int[arraySize];
for(int i = 0; i < arraySize; i++)
aPtr[i] = 0;
}

IntegerSet::IntegerSet(const IntegerSet &s) // copy constructor
{
arraySize = s.arraySize;
aPtr = new int[arraySize];
for(int i = 0; i < arraySize; i++)
aPtr[i] = s.aPtr[i];
}

IntegerSet::IntegerSet(int size) // constructor
{
arraySize = size + 1;
aPtr = new int[arraySize];
for (int i = 0; i < arraySize; i++)
aPtr[i] = 0;
}

IntegerSet::IntegerSet(int arr[], int n) // constructor
{
arraySize = n;
aPtr = new int[n];
for (int i = 0; i < n; i++)
aPtr[i] = 0;
for (int j = 0; j < n; j++)
if (arr[j] <= MAX_SIZE && arr[j] >= 0)
aPtr[arr[j]] = 1;
}

istream &operator>>(istream &in, IntegerSet &s)
{
int j = 0, inputValue = 0;

while (inputValue != -1 && j <= MAX_SIZE)
{
in >> inputValue;
if (inputValue < MAX_SIZE && inputValue >= 0)
{
if (inputValue >= s.arraySize)
{
int *tempPtr = nullptr;
tempPtr = new int[inputValue + 1];
for (int i = 0; i < inputValue + 1; i++)
{
tempPtr[i] = 0;
if (s.aPtr[i] == 1)
tempPtr[i] = 1;
}
delete [] s.aPtr;
s.aPtr = 0;
s.arraySize = inputValue + 1;
s.aPtr = new int[s.arraySize];
for (int i = 0; i < inputValue + 1; i++)
{
s.aPtr[i] = 0;
if (tempPtr[i] == 1)
s.aPtr[i] = 1;
}
delete [] tempPtr;
tempPtr = 0;
}
s.aPtr[inputValue] = 1;
}
j++;
}
return in;
}

ostream &operator<<(ostream &out, const IntegerSet &s)
{
int tally = 0;
out << "{";
for (int i = 0; i <= s.arraySize; i++)
{
if (s.aPtr[i] == 1)
{
if (tally == 0)
{
out << i;
tally++;
}
else
{
out << ", " << i;
tally++;
}
}
}
out << "}";

return out;
}

IntegerSet & IntegerSet::operator=(const IntegerSet &s)
{
if (arraySize > 0)
delete [] aPtr;
arraySize = s.arraySize;
aPtr = new int[arraySize];
for (int i = 0; i < arraySize; i++)
aPtr[i] = s.aPtr[i];
return *this;
}


Note: I'm overloading some operators (=, <<, >>, etc)

The issue I am having when I input (>>) data,
example: IntegerSet s;
cin >> s;

with my driver program works just fine for elements that are less than the initial array size (128). However, when I try to input values 138 or higher into the set, my program will output my those values plus others.

For example when I enter in the values: 138 -1
I believe the set should display {138}
BUT the dang thing keeps throwing in a 137 to make it {137, 138}
so frustrating! This is when I'm starting the program without debugging.

I got this feeling there's a memory leak somewhere haunting my code, but when I try and start the program with debugging this time and step through the loops and coding, the program will run exactly as I intend it to.

I know many of these forums frown upon people scheming for others to do their work, but if you could just point me in the right direction (ie there is/isnt a memory leak, something is up with my compiler, ???) I would greatly appreciate it.
It works fine here: http://cpp.sh/3d2z
input 138 -1
{138}


(note: you don't need to copy each element of temp to s.aPtr, you can just let s.aPtr point to where temp points to)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
istream &operator>>(istream &in, IntegerSet &s)
{
    int j = 0, inputValue = 0;
    
    while (inputValue != -1 && j <= MAX_SIZE)
    {
	in >> inputValue;
	if (inputValue < MAX_SIZE && inputValue >= 0)
	{
	    if (inputValue >= s.arraySize)
	    {
		int *tempPtr = nullptr;
		tempPtr = new int[inputValue + 1];
		for (int i = 0; i < inputValue + 1; i++)
		{
		    tempPtr[i] = 0;
		    if (s.aPtr[i] == 1)
			tempPtr[i] = 1;
		}
		delete [] s.aPtr;
		s.aPtr = tempPtr;
		s.arraySize = inputValue + 1;
	    }
	    s.aPtr[inputValue] = 1;
	}
	j++;
    }
    return in;
}
Last edited on
Thanks Gamer2015, such an obvious answer, yet for whatever reason I was stuck in a box. I fixed the s.aPtr to point to where temp pointed to and everything finaaaallllly works!
Topic archived. No new replies allowed.