Hi, I have made an IntArray class with overloaded operators. I have been desperately trying to fix the program. There is a driver with 20 tests. Before I was able to go through all twenty tests but the contents of the array were almost always -842150451. After many hours of messing around with it and messing around with the operators I can no longer get through all twenty tests because a runtime error pops up on the sixth test that says:
TestProgram.exe has encountered a problem.
and now the numbers are almost all -8. I have managed to make my program worse than before. I have been coding for many hours and I am making frequent mistakes. I have to get this done soon and I do not know what is wrong. Right now I think there is a problem with some of the constructors, the [] operators and the ostream/istream operators. Please somebody help me! I am desperate. Below is my code and I will post a second post with the driver code. Can somebody please help me so that all the tests execute and so the output is not random numbers. I need to fix the many flaws in my code. Please help! Thank you.
Code:
#include <iostream>
#include "intarray.h"
#include "iadrv.h"
using namespace std;
using std::strcpy;
using std::strlen;
#include <iomanip>
using std::setw;
IntArray::IntArray(void): start(0), finish(9)
{
CreateArray(finish);
parray[10];
cout<< "constructor begin" << endl;
}
IntArray::IntArray(IntArray &rhs): parray(0), start(rhs.start), finish(rhs.finish)
{
CreateArray(rhs.size);
for ( int i = rhs.low(); i <= rhs.high(); i++ )
parray[i] = rhs.parray[i];
cout<< "&rhs initiate" << endl;
}
IntArray::IntArray(int theStart, int theFinish): parray(0), start(theStart), finish(theFinish)
{
if(start > finish)
{
cout<< "There is an error with the array" << endl;
start = theStart;
finish = theFinish;
}
else
{
CreateArray(finish - start + 1);
cout<< "constructor called"<< endl;
}
}
int IntArray::translateIndexToZeroBased(int index)
{
index = index - low();
return index;
}
int& IntArray::operator [](int index)
{
static int sink;
int zerobasedindex = translateIndexToZeroBased(index);
if(zerobasedindex < 0 || zerobasedindex >= theSize)
{
cout<< "Problem with index (less than 0)" << endl;
return sink;
}
else
{
CreateArray(index);
}
return parray[zerobasedindex];
}
const int& IntArray::operator [](int index) const
{
static int sink;
int zerobasedindex = 10;
if(zerobasedindex < 0 || zerobasedindex >= theSize)
{
cout<< "Problem with index (l)" << endl;
return sink;
}
return parray[zerobasedindex];
}
IntArray::IntArray(int anarray[], int amount): parray(0), size(amount), start(0), finish(amount - 1)
{
CreateArray(amount);
for(int i = start; i <= finish; i++)
parray[i] = anarray[i];
}
IntArray::IntArray(int theSize): parray(0), size(theSize), start(0), finish(theSize -1)
{
CreateArray(size);
for(int i = start; i <= finish; i++)
parray[i] = i * 10;
}
IntArray::~IntArray()
{
delete []parray;
}
void IntArray::CreateArray(int theSize)
{
parray = NULL;
size = theSize;
parray = new int[size];
}
int IntArray::low()
{
return theStart;
}
int IntArray::high()
{
return theFinish;
}
void IntArray::setName(const char* aname)
{
name = new char[strlen(aname + 1)];
strcpy(name, aname);
}
bool IntArray::operator ==(IntArray &other)
{
if(*this != other)
return false;
for(int k = 0; k < size; k++)
if( parray[k] != other.parray[k])
return false;
return true;
}
bool IntArray::operator !=(IntArray &other)
{
return !(this == &other);
}
IntArray &IntArray::operator =(const IntArray &rhs)
{
if(this == &rhs)
return *this;
else
{
delete []parray;
size = rhs.size;
parray = new int[size];
for(int i = start; i<size ; i++)
parray[i] = rhs.parray[i];
}
return *this;
}
IntArray operator+ (IntArray& start, IntArray& finish)
{
int *array;
array = new int[start.size + finish.size];
for(int i = 0; i < start.size; i++)
array [i] = start.parray [i];
for(int k = start.size; k < start.size + finish.size; k++)
array [k] = finish.parray [k - start.size];
IntArray temp(array, start.size + finish.size);
delete []array;
return temp;
}
IntArray& IntArray::operator+= (IntArray& add)
{
delete []parray;
size = add.size;
parray = new int[size];
for(int k = 0; k< size ; k++)
parray[k] += add.parray[k];
cout<< "Success!" << endl;
return *this;
}
istream& operator>>(istream& input, IntArray& a)
{
input >> a.size;
delete []a.parray;
a.parray = new int [a.size];
for (int n = a.low(); n <= a.high(); n++)
input >> a[n];
return input;
}
ostream& operator<<(ostream& output, IntArray& a )
{
for (int n = a.low(); n <= a.high(); n++)
output << a[n] << endl;
return output;
}
#ifndef _IntArray_H
#define _IntArray_H
#include <iostream>
#include "iadrv.h"
using std::ostream;
using std::istream;
class IntArray
{
friend ostream &operator<<(ostream &output, IntArray &a);
friend istream &operator>>(istream &input, IntArray &a);
public:
IntArray();
IntArray(IntArray &);
IntArray(int start, int finish);
IntArray(int anarray[], int amount);
IntArray(int theSize);
~IntArray();
int& operator[](int index);
const int& operator[] (int index) const;
int high();
int low();
void setName(const char* name);
int translateIndexToZeroBased(int index);
IntArray& operator= (const IntArray &rhs);
IntArray& operator+= (IntArray& add);
friend IntArray operator+ (IntArray& start, IntArray& finish);
bool operator!= (IntArray &other);
bool operator== (IntArray &other);
protected:
void CreateArray(int theSize);
private:
int start;
int finish;
int size;
int *parray;
int index;
int anarray;
int amount;
char* name;
int theSize;
int theStart;
int theFinish;
};
#endif