Driver for Fixed-Size Stack
#include <iostream> #include <string> #include "stackt2.h" using namespace std; int main() { // Make a stack of integers. Stack<int, 10> is; int n; cout << "Enter integers to -1:" << endl << "> "; while(cin >> n) { if(n == -1) break; if(!is.push(n)) break; cout << "> "; } while(!is.empty()) cout << is.pop() << " "; cout << endl; // Make a stack of strings. Stack<string> ss; string s; cout << "Enter lines to EOF:" << endl << "> "; cin.clear(); getline(cin,s); while(getline(cin,s)) { ss.push(s); cout << "> "; } cout << endl; while(!ss.empty()) cout << ss.pop() << endl; }

This program uses the fix-size stack class. It's very similar to the last one, but the pushing loop tests the return value of push and exits on failure (full stack).