Dynamic Array Memory Not allocating properly

This code is supposed to iterate through a dynamically allocated array of 5 elements in size and store the user's input into the array in the next slot. But when I determine the size of the array I am only getting it to ever equal one. I even changed the constant INITIAL_SIZE to the literal constant of 5 with the same results. What am I doing wrong?

I have provided first the code that defines the dynamic array and then the code that reads in the values. They are two separate functions so the problem may be my passing the pointer to the dynamic array but I'm not for sure. I'm sorry about the comment formatting it came out messed up and when I tried to fix it it just made it worse.

Declares the dynamic array:
 
int* pNumericalInput = new int[INITIAL_SIZE];


Retrieves user input:
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
void getInput(int* pDataInputArray)
{
	cout << "Input positive integer values one at a"//Ask the user to input values and
		 << " time. A negative value will stop "	//indicate that a negative value
		 << "input." << endl;						//terminates the program.

	short input;									//Will hold the user's inputted value.

	do												//Run through the process at least 
	{												//once to get user input.
		short counter = 0;							//This is the counter of inputted values.
		cin >> input;								//Store the input to input.
		*(pDataInputArray + counter) = input;		//Set the value in the array equal to input.
		counter++;									//Increase the value counter.

		short mod5;									//Will hold the value of counter % 5.
		mod5 = counter % 5;

		if (mod5 = 0)								//Since the array is multiples of 5 in 
		{											//size, anytime 5 values have been entered,
													//the size of the array needs increased.
			pDataInputArray = increaseArraySize(pDataInputArray);
		}

	} while (input >= 0);							//if the input is < 0 terminate the loop.
}
Last edited on
The counter variable on line 11 is set to 0 every iteration, so you are always at the first element. Declare that variable outside the loop.
I caught that, but I am having another issue now. The dynamic array that is getting allocated is only ever getting allocated with the size of 1 so I am overwriting memory when my program executes and as such it fails. Any ideas as to why it is only setting it to one? INITIAL_SIZE is a const value set to 5.
Topic archived. No new replies allowed.