Safe Array Echo
/* * Read some integers into an array, then echo them. */ #include <iostream> using namespace std; // It's usual to give a name to array sizes. const int ARRMAX = 100; int main() { // We create native array and fill it with input values up to // the size of the array. If there are more input values, they // are ignored. int arr[ARRMAX]; int sub = 0; cout << "Enter some integers: " << endl; while(sub < ARRMAX && cin >> arr[sub]) { ++sub; } int num = sub; // Print them back out again. cout << "===============" << endl; for(int sub = 0; sub < num; ++sub) { cout << arr[sub] << " "; } cout << endl; }

Same thing again, this time being carful not to take the array out of bounds. This is why the size is given a name: the native array has no size method.