home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX08032.CPP
- // member function of the PhoneBk class
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX08031.H%EX08031.H definition of class String
- // %F,15,EX08032.H%EX08032.H definition of class PhoneBk
- // %F,15,EX08031.CPP%EX08031.CPP member functions of String
- // EX08032.CPP this file
- // %F,15,EX0803.CPP%EX0803.CPP main() to exercise PhoneBk Class
-
- //--------------------------------------------------------------
- #include "EX08032.H" // definition of PhoneBk class
- #include "EX08031.H" // definition of String class
-
- //--------------------------------------------------------------
- // The function insert() adds or changes an entry in a phonebook
- // PARAMETERS: char* who the name of the new entry- if
- // already in the book, change number
- // long telnum then new telephone number
- // RETURNS: int 1 success
- // int 0 failure, phonebook full
- //--------------------------------------------------------------
- int PhoneBk::insert( char* who, long telnum)
- // look at each name entry in the book to see if its matches who
- { for( int i = 0; i < numEntry; i++)
- { if( names[i].compare( who ) )
- continue;
- // If name matched, set the associated telephone number = telnum
- tel[i] = telnum;
- return ( 1 );
- }
- // If the book is full, new name and number can not be added
- if( numEntry == 100 )
- return ( 0 );
- // Add new netry
- tel[numEntry] = telnum; // insert telephone number
- String newname(who); // convert char* to String
- names[numEntry++] =String(who); // insert name
- return ( 1 );
- };
-
- //--------------------------------------------------------------
- // The function remove() deletes an entry from a PhoneBk object
- // PARAMETERS: char* who the name of the entry to delete
- // RETURNS: int 1 success
- // int 0 failure, name not in phonebook
- //--------------------------------------------------------------
- int PhoneBk::remove( char* who)
- // look at each name entry in the book to see if its matches who
- { for( int i = 0; i < numEntry; i++)
- { if( names[i].compare( who ) )
- continue;
- // entry found, delete it - do not leave gaps between entries
- // is this the only entry?
- if( numEntry > 1 )
- // if there are more than one entry, overwrite with last entry
- { tel[i] = tel[--numEntry];
- names [i] = names[numEntry];
- }
- // if the entry to delete is the only entry, just reset counter
- else
- { --numEntry;
- }
- return (1);
- }
- // if name not found in book, return failure code
- return ( 0 );
- };
-
- //--------------------------------------------------------------
- // The function lookup() finds a name in a PhoneBk object
- // and prints associated telephone number
- // PARAMETERS: char* who the name of the entry
- // RETURNS: long !=0 success, the telephone number
- // long 0 failure, name not in phonebook
- //--------------------------------------------------------------
- long PhoneBk::lookup( char* who)
- // look at each name entry in the book to see if its matches who
- { for( int i = 0; i < numEntry; i++)
- if( names[i].compare( who ) == 0 )
- // return telephone number associated with matching name
- return ( tel [i] );
- // if name not found in book, return failure code
- return ( 0 );
- };
-
- //--------------------------------------------------------------
- // The function print() lists every name and telephone number
- // PARAMETERS: ostream& os the output stream
- // RETURNS: ostream& os the output stream
- //--------------------------------------------------------------
- ostream& PhoneBk::print (ostream& os)
- // consider every entry in turn
- { for( int i = 0; i < numEntry; i++)
- // display name, tab for formatting, telephone number, newline
- names [i].print(os)
- << "\t"
- << tel[i]
- << endl;
- // add an end of file message
- os << "EOF"
- << endl;
- return ( os );
- };
- //--------------------------------------------------------------
-