Problem with classes and pointers

I am trying to do this project. The basic directions are to make a program with a class that has two private variables, one a dynamic array of strings and the other is a variable that determines the length of the array. I am supposed to have a member function to add entries and another to delete entries. I have gotten the code to compile and am debugging it. This is the debug version of the main:

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
30
#include <iostream>
#include <string>
#include "namearray.h"
using namespace std;

int main()
{
	int size1;
	int option;
	string entry;

	cout << "Input array size: ";
	cin >> size1;

	namearray NameOf(size1);

	cout << NameOf.get_size() << endl;

	int j=0;
	for ( j = 0; j <= size1; j++);
	{
		cin >> entry;
		NameOf.set_name(entry,j);
	
	}
	NameOf.display_name();
	
	
	return 0;
}


This is the header:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef NAMEARRAY_H
#define NAMEARRAY_H


#include <iostream>
#include <string>
using namespace std;

class namearray
{
public:
	
	//default constuctor, sets to size = 1
	namearray();
	
	//constructor to initialize to a userdefined value
	namearray(int startsize);
	
	namearray(const namearray &arraycopy);
	
	namearray addEntry(namearray array1, string entryToAdd);

	namearray deleteEntry(namearray array1, string entryToDelete);

	~namearray();

	int get_size();

	void set_size(int num);

	string get_name(int index);

	void set_name(string word, int index);

	void display_name();

	void operator =(namearray array1);

private:

	string* name;

	int size;
};

#endif 


And this is the implementation file:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>
#include "namearray.h"
using namespace std;


namearray::namearray()
{
	size = 1;
	name = new string[size];
}

namearray::namearray(int startsize)
{
	size = startsize;
	name = new string[size];
}

namearray::namearray(const namearray &arraycopy)
{
	size = get_size();
	name = new string[size];

	for ( int i = 0 ; i < size; i++)
	{
		name[i] = get_name(i);
	}
}

namearray namearray::addEntry(namearray array1, string entryToAdd)
{
	int newsize = array1.get_size() + 1;
	namearray Array2(newsize);

	for ( int i = 0; i < size; i++)
	{
		Array2.set_name(array1.get_name(i), i);
	}
	Array2.set_name(entryToAdd, newsize-1);

	delete[] array1.name;

	return ( Array2);
}

namearray namearray::deleteEntry(namearray array1, string entryToDelete)
{
	int entry_to_be_zorched = -9999;

	for ( int i = 0; i < size; i++)
	{
		if ( array1.get_name(i) == entryToDelete)
		{
			entry_to_be_zorched = i;
		}
	}
	if ( entry_to_be_zorched == -9999 )
	{
		return ( array1);
	}

	int newsize = array1.get_size() - 1;
	namearray Array2( newsize);

	for (int i = 0; i <= entry_to_be_zorched; i++)
	{
		Array2.set_name(array1.get_name(i), i);
	}
	for (int i = entry_to_be_zorched+1; i < size; i ++)
	{
		Array2.set_name(array1.get_name(i), i-1);
	}
	

	delete[] array1.name;

	return ( Array2);

}

void namearray::display_name()
{
	int i;
	cout << get_size()<<endl; //debug
	for ( i = 0; i < get_size(); i++);
	{
		cout << "blech" << endl;  //debug
		cout << i << ". " << get_name(i) << endl;
	}
}

string namearray::get_name(int index)
{
	string blech = name[index-1];

	return (blech);
}

void namearray::set_name(string word, int index)
{
	name[index-1] = word; 
}

int namearray::get_size()
{
	int blech = size; //debug
	return (blech);
}

void namearray::set_size(int num)
{
	size = num;
}

namearray::~namearray()
{
	delete[] name;
}

void namearray::operator =(namearray array1)
{
	int newsize = array1.get_size();
	if ( newsize > get_size())
	{
		name = new string[newsize];
	}
	for ( int i = 0; i < newsize; i++)
	{
		set_name(array1.get_name(i), i);
	}
}



And this is the output for array size 3, a,b,c:

I am able to input the array length and then...

It comes up with one of those funky Windows XP dialogs that says something like:

This program has encountered a problem and "needs to close".

What is going on???
At first glance, I notice several problems.


1. Your default constructor creates an array of length 1. Why? Why not 0?

2. Your copy constructor calls get_name(i), which calls get_name on *this, not on
the object from which you are copying.

3. Why do addEntry and deleteEntry take namearrays as parameters and return a namearray?
You want to add or remove from *this, so there is no need for the extra parameter.

4. There are other problems with addEntry and deleteEntry, but I won't go into them until you
fix #3.

5. Your assignment operator leaks memory. In the expression a = b; where a and b are
namearrays, a may already contain names prior to the assignment. You need to ensure
that you free the memory used by a's internal array prior to "overwriting" it.

6. set_size() is a pointless method particularly in a public interface, unless you actually go and
create those elements. Think about what would happen if the array contained 5 elements,
then the user called set_size( 10 ). Where are indices 5-9 stored? You haven't even allocated
memory for them.
Thanks! Your suggestions really helped. My program runs fine now. I don't even know why I was doing that on those add and delete.
Topic archived. No new replies allowed.