Having some huge issues with my list program

Howdy. For my CSC 265 course, I'm making a list program. The rules are as follows: The program has a List class, which has a dynamic array (starts at size 2). The user can enter a value, print the values, or test the copy constructor (I'll come back to this).

Once they've entered two values, when they try to enter a third, the array needs to automatically add two to the size.

Printing the values is extremely simple, so I'm having no issues with this.

To test the copy constructor, it needs to run the copy constructor, set all of the values of the array to zero, and then print the zeroes.

After all of this works, I need to derive a Set class from List, and allow the user to choose which class to use at runtime.


Here are my problems:
1. When my array gets past size 2, it screws up my first entry. At size 4, it sets the first entry to 0, no matter what, and at size 6, it sets it to a huge number.
2. When I test the copy constructor, it doesn't seem to erase the numbers. It just outputs them.
3. I can't figure out how to get the user to choose classes. I tried an if statement, keeping the object name the same regardless of which class they used, but the compiler told me the object was undefined.

Here's a link to my code. Thanks so much for the help! http://min.us/mlistprogram
closed account (DSLq5Di1)
Some thoughts after going over your List class:-

Constructor
- Use constants where appropriate, ie. the size variable. [1]
- array is a reserved keyword, you should not use it to name your variables!
- Use an initialization list for your constructors [2]
- You can zero initialize your array with the call to new, new int[size]();.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void List::insert(const int& entry)
{
        ...

	else
	{
		int* ptr = new int[size];
		ptr = array; // remember that these are pointers..
                             // the memory just allocated for ptr is lost, ptr is now a copy of array,
                             // and as such points to the same memory allocated for array.
		
		size += 2;
		delete[]array; // now array and ptr are both invalid.

		int* array = new int[size]; // you are creating a local variable here,
                                            // that hides the member variable of the same name.

		array = ptr; // another memory leak, the local variable array, now points to the invalid pointer ptr.
		array[count]=entry; // bigbadaboom..
		count++;
	}
}

1
2
3
4
5
void List::erase() const // an erase operation is not exactly a candidate for constantness..
{
    for (int i = 0; i < size; i++)
        array[size]=0; // a silly mistake?
}

Your third problem would be solved with polymorphism:-
http://www.cplusplus.com/doc/tutorial/polymorphism/
http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/

[1] http://www.parashift.com/c++-faq-lite/const-correctness.html
[2] http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
Last edited on
Thanks so much for the reply. You commented on what's wrong with my insert function, but I don't think I understand pointers exactly, so how do I go about fixing it?
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	{
                size += 2;
		int* new_array = new int[size]; // allocate memory for a new array

		for (int i = 0; i < count; i++)
                {
			new_array[i] = array[i]; // copy the values from your old array into the new array
                }
                new_array[count] = entry; // assign the new entry

                delete[] array; // delete the memory allocated for your old array
                array = new_array; // assign the new_array pointer to your old array pointer
               
                count++;
	}

ps. I think it would make more sense if you renamed 'size' to 'capacity' and 'count' to 'size'.
Topic archived. No new replies allowed.