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;


	}




}

friend stringArray operator+(const stringArray& left, const stringArray& left);

1) Does not need to be a friend.

2) Need to implement the assignment operator.

stringArray::stringArray(const stringArray& source)
{
    count = source.count;

    for(int i = 0; i < max_size; i++)
    {
        strcpy(storage[i], source.storage[i]);
    }

}

3) Is not implemented correctly.
4) Give more info, where does it crash? Where is your main?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

// FILE: stringArrayExam.cxx
// Written by Shivakant Mishra
// Last Update: March 07, 2010

#include <iostream>
#include <cstdlib>
#include "stringArray.h"

using namespace std;
using namespace csci2270_assignmentTwo;

	int main( )
	{
	    stringArray names;

	    cout << "Number of strings in names are " << names.size( ) << endl;
	    names.addString("Tyson Henry");
	    names.addString("Mike Coffin Jr.");
	    names.addString("Nick J. Bucholtz");
	    names.addString("Shamim Mohamed");
	    names.addString("Curtis Dyerson");
	    names.addString("Richard D. Schlichting");
	    cout << "Number of strings in names are " << names.size( ) << endl;
	    names.printStringArray( );
	    cout << endl;
	    if (names.is_string("Shamim Mohamed"))
		cout << "Shamim Mohamed is in names" << endl;
	    else
		cout << "Shamim Mohamed is not in names" << endl;
	    names.addString("Vicraj Thomas");
	    names.addString("Nina Bhatti");
	    names.addString("Herman Rao");
	    names.addString("Patrick Homer");
	    cout << "Number of strings in names are " << names.size( ) << endl;

	    cout << endl;

	    
	}

That is some of the main file that tests our program, and my program stops working the second it starts up. Windows brings up the troubleshooter right when i try to run. Even when i enter a test

cout << "test";

it doesn't even do this, so i can't even try to test different parts of my code. Thank you for the reply

also, it's a requirement for the + operator to be friend

Edited 6 Years Ago by icelantic: n/a

First off: Fix your compiler warnings. They are there for a reason.

e.g. storage[i]="\0"; should be storage[i]='\0'; .

Your memory problem is because in both constructors [that are ambiguous by the way] you allocate storage but do not allocate or even set to zero the memory for each location in storage.

You are going to either:

(a) have to allocate a space for the strings, AND keep a record of how long that is, since the null terminator will tell you when the actual string ends but not the memory space

(b) Write a short string class to do that for you, and make stringArray a storage for for a number of string objects.

I recommend (b). Note that you have not broken the terms of the assignment because you have written your own. i.e. not use the STL.
however, you might like to clarify that point.

This article has been dead for over six months. Start a new discussion instead.