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!
void getInput(longint *, int);
// Main function
int main()
{
// Initialize the array
longint *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 = newlongint [arraySize];
// Enters the getInput function
getInput(&array, arraySize);
void getInput(longint *userArray, int arraySize)
{
// Initializes an integer to store the input
longint 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;
}
}
}