I am working on an assignment which is a container class that uses an array to hold strings. We must use dynamic memory, and can only use cstring class functions, no objects can be strings. So we must use char arrays. Anyways my problem is that whenever i run my program it instantly crashes and I can't seem to find why, any help would be greatly appreciated.
/*
* stringArray.h
*
* Created on: Mar 2, 2010
* Author: Jacob
*/
//Constructor for the stringArray class
// stringArray(const size_t max_size = DEFAULT_SIZE);
//
//
//Copy constructor stringArray(const stringArray& source);
//
//Destructor ~stringArray();
//
//bool addString(const char new_string[]);
// adds a new string in the bag.
// POSTCONDITION: returns true if string was successfully added to the bag, false otherwise
//
//bool removeString)const char old_string[]);
// removes one occurrence of old_string from the bag.
//POSTCONDITION: returns trus if successful; false otherwise
//
//size_t removeAll(const char old_string[]);
//removes all occurrences of old_string from the bag.
//POSTCONDITION: removes the strings and then returns the number of strings removed
//
//bool substitute(const char old_string[], const char new_strin[]);
//Replaces one occurrence of old_string with new_string
// POSTCONDITION: one is replaced and true is returned, if there are no occurences of old_string
// or the function failed somehow, false is returned instead
//
//size_t substituteALL(const char old_string[], const char new_stringn[]);
// Replaces all the occurrences of old_string with new_string
//POSTCONDITION: Returns the number of strings replaces.
//
//size_t size()
// returns the total number of strings in the bag
//
// bool is_string(const char cur_string[]) const;
// returns true if cur_string is present in the bag, false otherwise
//
// size_t occurrences(const char cur_string[]) const;
// returns the number of occurrences of cur_string in the bag
//
// void printStringArray() const;
// Prints out all strings in the stringArray object, one string per line
//
// void pringSortedStringArray() const;
// prints out all strings in the stringArray object, one string per line, ina sorted order from least to greatest
//
// overloaded operators
//
// stringArray operator+ (stringArray& x, stringArray& y)
//
// stringArray operator- (stringArray
#include <iostream> // Provides ostream and istream
#include <cstdlib> // Provides ostream and istream
using namespace std;
namespace csci2270_assignmentTwo
{
class stringArray
{
public:
stringArray(const size_t max_size = 10);
stringArray();
stringArray(const stringArray& source);
~stringArray();
bool addString(const char new_string[]);
bool removeString(const char old_string[]);
size_t removeAll(const char old_string[]);
bool substitute(const char old_string[], const char new_strin[]);
size_t substituteAll(const char old_string[], const char new_string[]);
size_t size();
bool is_string(const char cur_string[]);
size_t occurrences(const char cur_string[]);
void printStringArray();
void pringSortedStringArray() const;
bool stringArray::operator == (const stringArray &right);
bool stringArray::operator!=(const stringArray &right) const;
stringArray stringArray::operator +=(const stringArray &right);
friend stringArray operator+(const stringArray& left, const stringArray& left);
private:
size_t count;
size_t max_size;
char** storage;
};
}
/*
* stringArray.cxx
*
* Created on: Mar 2, 2010
* Author: Jacob
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "stringArray.h"
using namespace std;
namespace csci2270_assignmentTwo
{
stringArray::stringArray(const size_t max_size)
//creates a dynamic array that is the size of the max size
//sets count equal to zero
{
char** storage = new char* [max_size];
for(int i = 0; i < max_size; i++)
{
storage[i] = "\0";
}
count = 0;
}
stringArray::stringArray()
{
char** storage = new char* [10];
for(int i = 0; i < 10; i++)
{
storage[i] = "\0";
}
count = 0;
}
stringArray::~stringArray()
{
for(int i = 0; i < max_size; i++)
{
delete storage[i];
}
delete storage;
}
stringArray::stringArray(const stringArray& source)
{
count = source.count;
for(int i = 0; i < max_size; i++)
{
strcpy(storage[i], source.storage[i]);
}
}
bool stringArray::addString(const char new_string[])
{
int length = strlen(new_string);
for(int i = 0; i < max_size; i++)
{
if(storage[i] != "\0")
{
storage[i] = new char[length];
strcpy(storage[i], new_string);
count++;
return true;
}
}
}
bool stringArray::removeString(const char old_string[])
{
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], old_string) == 0)
{
delete storage[i];
count--;
for(i; i < (max_size - 1); i++)
{
strcpy(storage[i], storage[i+1]);
}
}
}
}
size_t stringArray::removeAll(const char old_string[])
{
for(int i = 0; i < max_size; i++ )
{
removeString(old_string);
}
}
bool stringArray::substitute(const char old_string[], const char new_string[])
{
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], new_string) == 0)
{
delete storage[i];
int length = strlen(new_string);
storage[i] = new char[length];
strcpy(storage[i], new_string);
return true;
}
}
return false;
}
size_t stringArray::substituteAll(const char old_string[], const char new_string[])
{
size_t ammount = 0;
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], new_string) == 0)
{
delete storage[i];
int length = strlen(new_string);
storage[i] = new char[length];
strcpy(storage[i], new_string);
ammount++;
}
}
return ammount;
}
size_t stringArray::size()
{
size_t size;
for(int i = 0; i < max_size; i++)
{
if(storage[i] != "\0")
{
size++;
}
}
return size;
}
bool stringArray::is_string(const char cur_string[])
{
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], cur_string) == 0)
{
return true;
}
}
}
size_t stringArray::occurrences(const char cur_string[])
{
size_t occur;
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], cur_string) == 0)
{
occur++;
}
}
return occur;
}
void stringArray::printStringArray()
{
for(int i = 0; i < count; i++)
{
cout << storage[i];
cout << endl;
}
}
void printSortedStringArray()
{
}
stringArray stringArray::operator +=(const stringArray &right)
{
int i = 0;
stringArray new_bag(max_size + right.max_size);
for(int i = 0; i < max_size; i++)
{
strcpy(new_bag.storage[i], storage[i]);
}
for(i; i < (max_size + right.max_size); i++)
{
strcpy(new_bag.storage[i], right.storage[i]);
}
return new_bag;
}
bool stringArray::operator == (const stringArray &right)
{
if(max_size != right.max_size)
{
return false;
}
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], right.storage[i]) != 0 )
{
return false;
}
}
return true;
}
bool stringArray::operator!=(const stringArray &right) const
{
if(max_size != right.max_size)
{
return true;
}
for(int i = 0; i < max_size; i++)
{
if(strcmp(storage[i], right.storage[i]) != 0 )
{
return true;
}
}
return false;
}
stringArray operator+(const stringArray& left, const stringArray& right)
{
stringArray temp = left;
int i = 0;
for(i; i < temp.max_size; i++ )
{
strcpy(temp.storage[i], right.storage[i]);
}
return temp;
}
}