home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Info / TeachU14 / SAMS / Code / Day03 / pointer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-08  |  2.4 KB  |  99 lines

  1. //---------------------------------------------------------------------------
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #pragma hdrstop
  6.  
  7. #include "structur.h"
  8.  
  9. void displayRecord(int, mailingListRecord mlRec);
  10. int main(int, char**)
  11. {
  12.   //
  13.   // create an array of pointers to
  14.   // the mailingListRecord structure
  15.   //
  16.   mailingListRecord* listArray[3];
  17.   //
  18.   // create an object for each element of the array
  19.   //
  20.   for (int i=0;i<3;i++)
  21.     listArray[i] = new mailingListRecord;
  22.   cout << endl;
  23.   int index = 0;
  24.   //
  25.   // get three records
  26.   //
  27.   do {
  28.     cout << "First Name: ";
  29.     cin.getline(listArray[index]->firstName,
  30.       sizeof(listArray[index]->firstName) - 1);
  31.     cout << "Last Name: ";
  32.     cin.getline(listArray[index]->lastName,
  33.       sizeof(listArray[index]->lastName) - 1);
  34.     cout << "Address: ";
  35.     cin.getline(listArray[index]->address,
  36.       sizeof(listArray[index]->address) - 1);
  37.     cout << "City: ";
  38.     cin.getline(listArray[index]->city,
  39.       sizeof(listArray[index]->city) - 1);
  40.     cout << "State: ";
  41.     cin.getline(listArray[index]->state,
  42.       sizeof(listArray[index]->state) - 1);
  43.     char buff[10];
  44.     cout << "Zip: ";
  45.     cin.getline(buff, sizeof(buff) - 1);
  46.     listArray[index]->zip = atoi(buff);
  47.     index++;
  48.     cout << endl;
  49.   }
  50.   while (index < 3);
  51.   //
  52.   // display the three records
  53.   //
  54.   clrscr();
  55.   //
  56.   // must dereference the pointer to pass an object
  57.   // to the displayRecord function.
  58.   //
  59.   for (int i=0;i<3;i++) {
  60.     displayRecord(i, *listArray[i]);
  61.   }
  62.   //
  63.   // ask the user to choose a record
  64.   //
  65.   cout << "Choose a record: ";
  66.   int rec;
  67.   do {
  68.     rec = getch();
  69.     rec -= 49;
  70.   } while (rec < 0 || rec > 2);
  71.   //
  72.   // assign the selected record to a temporary variable
  73.   // must dereference here, too
  74.   //
  75.   mailingListRecord temp = *listArray[rec];
  76.   clrscr();
  77.   cout << endl;
  78.   //
  79.   // display the selected recrord
  80.   //
  81.   displayRecord(rec, temp);
  82.   getch();
  83.   return 0;
  84. }
  85. void displayRecord(int num, mailingListRecord mlRec)
  86. {
  87.   cout << "Record " << (num + 1) << ":" << endl;
  88.   cout << "Name:     " << mlRec.firstName << " ";
  89.   cout << mlRec.lastName;
  90.   cout << endl;
  91.   cout << "Address:  " << mlRec.address;
  92.   cout << endl << "          ";
  93.   cout << mlRec.city << ", ";
  94.   cout << mlRec.state << "  ";
  95.   cout << mlRec.zip;
  96.   cout << endl << endl;
  97. }
  98.  
  99.