#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\n"; for (stack s;;) { //Construct the stack. 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 types control-d. cout << "The stack has already been destructed.\n"; return EXIT_SUCCESS; }