// main.cpp - Dictionary Template Demonstration, Section 10.4.4. // Updated for STL classes, 11/27/00 #ifdef _DEBUG #pragma warning( disable : 4786 ) #endif #include #include #include #include using namespace std; #include "dict.h" // Dictionary class #include "noFile.h" // NoFileError Exception class void Safety_Deposit_Boxes() // This Dictionary contains unique safety deposit box // numbers and their customer account numbers. { Dictionary accounts( 100 ); cout << "Customer Accounts:\n"; try { accounts.Add( 101, 2287 ); accounts.Add( 102, 2368 ); accounts.Add( 103, 2401 ); accounts.Add( 104, 2368 ); // duplicate value is ok accounts.Add( 103, 2399 ); // duplicate key forbidden } catch( const DuplicateKey & dk ) { // cout << dk << endl; } // cout << accounts << endl; // display the list cout << "Safety deposit box number to find? "; int boxNum; cin >> boxNum; int pos = accounts.Find( boxNum ); if( pos != -1 ) { cout << "Box number " << boxNum << " is registered to account number " << accounts.At(pos) << '.' << endl; } else cout << "Box not found." << endl; cout << endl; } void create_parts_list() // Create a dictionary of automobile part // numbers (long) and part descriptions (String) // by reading them from an input file. { string filename( "parts.txt" ); ifstream infile( filename.c_str() ); if( !infile ) throw NoFileError( filename ); // Add part numbers and descriptions to a dictionary. int partNum = 0; string description; const int plistSize = 200; cout << "Automobile parts list:\n"; Dictionary pList( plistSize ); try { infile >> partNum; while( !infile.eof()) { infile.get(); getline(infile, description); pList.Add( partNum, description ); infile >> partNum; } } catch( const DuplicateKey & dk ) { // cout << dk << endl; } // cout << pList; cout << "Part number to find? "; cin >> partNum; if( cin.fail()) { cin.clear(); cin.ignore(255,'\n'); cerr << "Invalid input\n"; return; } int n = pList.Find( partNum ); if( n != -1 ) { cout << "Part " << partNum << " is a \"" << pList.At(n) << "\" ." << endl; } else cout << "Part number not found." << endl; } // main: create two dictionaries, handle all // uncaught exceptions. int main() { cout << "Dictionary Template Demo Program\n\n"; try { Safety_Deposit_Boxes(); create_parts_list(); } catch ( const DictionaryFull & df ) { cout << df; } catch ( const NoFileError & nf ) { cout << nf; } catch ( ... ) { cout << "Unknown exception caught in main.\n"; } return 0; }