/* testarr04.cpp CIS 150 06/19/2008 David Klick Demonstration of setting array elements from user input. */ #include using std::cout; using std::cin; void cont(); int main() { // common way to set array size globally const int ARRAY_SIZE = 10; double nums[ARRAY_SIZE]; int numItems = 0; // number of items stored in array int ctr; double sum; // get values for array from user, but don't // allow array to overflow while (numItems < ARRAY_SIZE) { cout << "Enter item #" << (numItems+1) << " (-999 to quit): "; cin >> nums[numItems]; // stop if user enters -999 if (nums[numItems] == -999) break; // keeps track of how many items are in the array numItems++; } // skip this section if nothing was entered if (numItems > 0) { // display all items stored in array cout << "You entered:\n"; for (ctr=0; ctrsungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: Enter item #1 (-999 to quit): 42.7 Enter item #2 (-999 to quit): 32 Enter item #3 (-999 to quit): 12.0 Enter item #4 (-999 to quit): -999 You entered: 42.7 32 12 The average is 28.9 */