Copying one dynamic array into another and using "delete"

Here's what I have so far.

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
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
	int * listDyn;
	int max =2;
	int n=0;
	int i=0;
	listDyn = new int[max];
	

	cout << "Enter a list of numbers to be stored in an array. After each                                            entry press ENTER. Enter '-1' when you are finished." << endl; 


	while( n != -1)
	{
		cin >> listDyn[i] ;

		if( i >= max)
		{
		max = max*2;            
        int* TempListDyn = new int[max];
		
        for (int j=0; j<i; j++) 
		{
           TempListDyn[j] = listDyn[j];       
        }
        delete [] listDyn;     

        int *listDyn = new int[max];

	    for (int p=0; p<i; p++) 
		listDyn[p] = TempListDyn[p];

		delete [] TempListDyn;

		}
		n= listDyn[i];
		i++;
	}
		cout << " These are the values you entered" << endl;

		for(int k=0; k < i-1; k++)
		{
			cout << listDyn[k] << " " ;
		}
	system("pause");
	}


The main problem is line 30( commented that line and everything worked perfectly. How do I go about fixing this?
Last edited on
the problem is not line 30. it's line 32. You define a new local variable listDyn and the old is not changed.

line 32: int *listDyn = new int[max];

tip: make proper indentations. This way it's hard to see what line belongs to what block
I knew it would something so simple. Thanks!

Sorry about the indentation. It looked fine in Visual C++ so I thought it would look fine here. Note to self: Preview before posting is very helpful for everyone involved.
Topic archived. No new replies allowed.