Pointing issue.

I'm just getting into pointers and I'm having trouble understanding what the hell is going wrong in my program. I'm trying to allow the user to enter an integer so that I can dynamically allocate memory for the array using a pointer. I can't seem to get it to work and it's driving me a little crazy. Thanks for your help!

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
void getInput(long int *, int);

// Main function
int main()
{

// Initialize the array
	long int *array;

	int arraySize=0;


// Welcomes the user to the program
	cout << "Welcome" << endl;
	cout << "How many integers would you like to add to the array" << endl;
	while (arraySize==0){
		cout << "You may not determine that you want 0 integers added to the array" << endl;
		cin >> arraySize;
	}
	cin.clear();
	cin.ignore();
	array = new long int [arraySize];

// Enters the getInput function
	getInput(&array, arraySize);

void getInput(long int *userArray, int arraySize)
{

// Initializes an integer to store the input
	long int testValue = 0;

// For loop from 0 to 9 increasing by 1
	for (int count = 0; count < arraySize; count++)
	{

// Prompts the user for their input
		cout << "Enter an integer number between -2,147,483,648 and 2,147,483,648" << endl;

// Test to make sure that the input is valid
		if (!(cin >> testValue))
		{
// If it fails
// Clears the Keyboard buffer
			cin.clear();
			cin.ignore(1000,'\n');

// Tell the user the problem
			cout << "That is not an integer" << endl;

// Decrease the count by 1 for overwriting
			--count;
		}

// If it passes
		else
		{

// The array at the index depending upon the for loop
// gains the value of the variable that stored the users
// input
			*(userArray+count)=testValue;
		}
	}
}
What is the exact error? (Your code doesn't seem to be complete.)


Your "array" is a pointer. The getInput takes a pointer. However, on line 25 you give the address of a pointer (same as giving a pointer to pointer).
I fixed the problem.
1
2
// Enters the getInput function
	getInput(&array, arraySize);


Should have been.....

1
2
3

// Enters the getInput function
	getInput(array, arraySize);
Topic archived. No new replies allowed.