/* testalloc2.cpp CIS 150 David Klick 6/26/08 Demonstration of allocating memory for an array. */ #include using std::cin; using std::cout; void cont(); int main() { int size; // holds size of array int i, sum; int *arr; // pointer for array we will create // find out how large the user would like the array to be do { cout << "Enter array size (1-10): "; cin >> size; if (size < 1 || size > 10) { cout << "Error: Invalid size\n"; } } while (size < 1 || size > 10); // create the array arr = new int[size]; // get numbers for array from user for (i=0; i> *(arr+i); } // add up all the elements of the array sum = 0; for (i=0; isungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: Enter array size (1-10): -2 Error: Invalid size Enter array size (1-10): 12 Error: Invalid size Enter array size (1-10): 5 Enter element 0: 12 Enter element 1: 18 Enter element 2: -20 Enter element 3: 15 Enter element 4: 5 The sum is 30 */