#include #include #include "stack.h" using namespace std; int main() { cout << "Type a positive integer to push it,\n" << "non-positive to pop the stack,\n" << "control-d to halt.\n"; stack s; //Construct the stack. for (;;) { cout << "Go ahead: "; int i {0}; cin >> i; if (!cin) { //If the user typed control-d break; //out of the for loop } try { if (i > 0) { s.push(i); } else { cout << s.pop() << "\n";; } } //Arrive here if push or pop threw an exception. catch (out_of_range &out) { cerr << out.what() << "\n"; cerr << "Try again.\n"; } } //Arrive here when user typed control-d. cout << "About to destruct the stack.\n"; return EXIT_SUCCESS; //Destruct s here because we're leaving main's {} }