See More

/*----------------------------------------------------------------------------+ | Author: Jerome Richards | | | | Assignment: Project 7 | | File: driver.cpp | | | | Instructor: Dr. Roger deBry | | Class: CNS-1350 Section X01 | | | | Date Written: February 27, 2005 | | | | Description: | | | | Test the use of the CStack with Exceptions | +----------------------------------------------------------------------------*/ #include #include #include #include #include #include #include "stack.h" #include "stackrep.h" using namespace std; const int TRUE = 1; const int FALSE = 0; void dispPrologue(); /*----------------------------------------------------------------------------+ | Purpose: Test the CStack class | | | | Parameters: argc an int representing the number of parameters | | | | argv an array of character parameters | | | | Returns: 0 representing successful execution | | | | Pre-conditions: none | | Post-conditions: none | +----------------------------------------------------------------------------*/ int main( int argc, char* argv[] ) { CStack* theStack = NULL; dispPrologue(); cout << "Creating the stack with no parameters (default size is 5)\n\n"; theStack = new CStack; assert( theStack != NULL ); cout << " The current stack size is: " << theStack->stackSize() << "\n"; cout << " The amount of free space is: " << theStack->freeSpace() << "\n\n"; cin.exceptions( ios_base::failbit | ios_base::badbit ); int tmpInt = -1; while( tmpInt != 0 ) { cout << "Enter an integer value (zero to stop): "; try { cin >> tmpInt; if( tmpInt != 0 ) theStack->push( tmpInt ); } catch( ios_base::failure & e ) { cout << "\nPlease enter an integer." << "\n"; cin.clear(); cin.ignore( 1000, '\n' ); } } cout << "\n\nRemoving data from the stack...\n"; bool lExit = FALSE; while( ! lExit ) { try { cout << theStack->top() << "\n"; } catch (CEmptyStackException& err ) { cout << "\n" << err.what(); } try { theStack->pop(); } catch (CEmptyStackException& err ) { cout << "\n" << err.what(); lExit = TRUE; } } cout << "\n\nAttempting to move past top of stack...\n"; try { theStack->pop(); } catch (CEmptyStackException& err ) { cout << "\n" << err.what() << "\n\n"; } assert( theStack != NULL ); delete theStack; theStack = NULL; cout << "Program terminated normally. "; system( "pause" ); return 0; } /*----------------------------------------------------------------------------+ | Purpose: Display file prologue | | Parameters: none | | Returns: nothing | | | | Pre-conditions: none | | Post-conditions: none | +----------------------------------------------------------------------------*/ void dispPrologue() { system( "cls" ); cout << " Name: Jerome Richards\n"; cout << " Course: CNS-1350 Section X01\n"; cout << "Instructor: Dr. Roger deBry\n"; cout << " Program: Project Seven\n"; cout << " File: Stack Exceptions (stack.exe)\n"; cout << "\n\n"; }