I am attempting to create a sorted array for a class assignment but I am receiving a weird access violation error in my debug code. I have double checked all my values and nothing is accessing anything out of range. My code is below.
Thanks.
#include <iostream>
using namespace std;
int size=0;
class SortedArray
{
public:
void insert(int);
void print();
private:
int *sortedArray;
};
void SortedArray::insert(int inp)
{
size++;
int *temp=new int[size];
for (int i=0;i<(size-1);i++)
temp[i]=sortedArray[i];
delete[]sortedArray;
sortedArray=new int[size];
for(int i=0;i<size;i++)
sortedArray[i]=temp[i];
}
void SortedArray::print()
{
for(int i=0; i<sizeof(sortedArray);i++)
{
cout << sortedArray[i] << " ";
}
cout << endl;
}
int main(){
SortedArray x;
int inp;
do{
cin >> inp;
x.insert(inp);
x.print();
}while(inp>=0);
system ("Pause");
return 0;
}