// // Program: print_reverse // // Purpose: print input in reverse order // // Input: sequence of numbers x1, x2, .. xN (up to 20), from standard // input. // // Output: x1, x2, .. xN printed to standard output in reverse order // (xN, .... x2, x1). If the user enters more than 20 numbers, the // program prints a warning message. // #include #include // has EXIT_SUCCESS // Function prototypes (for comments, see definitions below.) bool moreInput(void); // Main program. int main(void) { const int MaxNums = 20; cout << "Enter some integers (no more than " << MaxNums << "), " " control-D to end:\n"; int nums[MaxNums]; int count = 0; // how many numbers were read. // Read numbers until end-of-file on input or array is full. while (count < MaxNums && (cin >> nums[count])) ++count; // Check to see if user has tried to enter more than MaxNums // numbers. if (moreInput()) cout << "Warning: More than " << MaxNums << " numbers entered. Only the first " << MaxNums << " will be used.\n"; // Print numbers back out in reverse order. cout << "Here are the numbers you entered, in reverse order:\n"; for (int i = count-1; i >= 0; --i) cout << nums[i] << endl; return EXIT_SUCCESS; } // Function definitions. // Post: returns true if there is more non-whitespace input to read // on standard input, false if not. bool moreInput(void) { // Already hit end of file in previous attempt to read? if (cin.eof()) return false; // Discard whitespace and check for end of file again. cin >> ws; return !(cin.eof()); }