Source of bstruct.cpp


  1: // Filenme: BSTRUCT.CPP
  2: // Purpose: Illustrates a simple struct for representing a bank account.

  4: #include <iostream>
  5: #include <iomanip>
  6: using namespace std;


  9: struct Person           // Must be defined first so that
 10: {                       // it can be used in BankAccount
 11:     char firstInitial;
 12:     char lastInitial;
 13:     int  idNumber;
 14: };

 16: struct BankAccount
 17: {
 18:     Person owner;       // This field is itself a struct.
 19:     double balance;
 20: };


 23: void InitAccount(/* out */ BankAccount& account,
 24:                  /* in */  int idNum,
 25:                  /* in */  char first,
 26:                  /* in */  char last,
 27:                  /* in */  double balance);
 28: void UpdateBalance(/* inout */ BankAccount& account,
 29:                    /* in */    double amount);
 30: void DisplayAccountInfo(/* in */ const BankAccount& account);
 31: void Pause(/* in */ int indentLevel);


 34: int main()
 35: {
 36:     cout << endl
 37:          << "This program uses a struct "
 38:          << "to represent a bank account. "      << endl
 39:          << "You should study the source code "
 40:          << "at the same time as the output. "   << endl
 41:          << endl;
 42:     Pause(0);

 44:     BankAccount account = { {'P', 'S', 123}, 148.37};
 45:     DisplayAccountInfo(account);  cout << endl;
 46:     UpdateBalance(account, 12.59);
 47:     DisplayAccountInfo(account);  cout << endl;
 48:     Pause(0);

 50:     account.owner.firstInitial = 'W';
 51:     account.owner.lastInitial  = 'C';
 52:     account.owner.idNumber     = 321;
 53:     account.balance            = -175.50;
 54:     DisplayAccountInfo(account);  cout << endl;
 55:     UpdateBalance(account, -345.20);
 56:     DisplayAccountInfo(account);  cout << endl;
 57:     Pause(0);

 59:     InitAccount(account, 246, 'B', 'C', 100.00);
 60:     DisplayAccountInfo(account);  cout << endl;
 61:     UpdateBalance(account, 150);
 62:     DisplayAccountInfo(account);  cout << endl;
 63:     Pause(0);

 65:     return 0;
 66: }



 70: //******************************************************************
 71: void InitAccount(/* out */ BankAccount& account,
 72:                  /* in */  int idNum,
 73:                  /* in */  char first,
 74:                  /* in */  char last,
 75:                  /* in */  double balance)
 76: // Pre:  All in-parameters have been initialized.
 77: // Post: The information in each in-parameter has been
 78: //       stored in account.
 79: {
 80:     account.owner.idNumber     = idNum;
 81:     account.owner.firstInitial = first;
 82:     account.owner.lastInitial  = last;
 83:     account.balance            = balance;
 84: }


 87: //******************************************************************
 88: void UpdateBalance(/* inout */ BankAccount& account,
 89:                    /* in */    double amount)
 90: // Pre:  account and amount have been initialized.
 91: // Post: The balance in account has been updated by the
 92: //       quantity amount, which may be positive or negative.
 93: {
 94:     account.balance += amount;
 95: }


 98: //******************************************************************
 99: void DisplayAccountInfo(/* in */ const BankAccount& account)
100: // Pre:  "account" has been initialized.
101: // Post: The information in "account" has been displayed.
102: {
103:     cout.setf(ios::fixed, ios::floatfield);
104:     cout.setf(ios::showpoint);
105:     cout << setprecision(2);
106:     cout << "Owner:           "  << account.owner.firstInitial
107:                                  << account.owner.lastInitial << endl;
108:     cout << "Owner's ID#:     "  << account.owner.idNumber    << endl;
109:     cout << "Current balance: $"  << account.balance          << endl;
110: }



114: //******************************************************************
115: void Pause(/* in */ int indentLevel)
116: // Pre:  indentLevel has been initialized.
117: //       The input stream cin is empty.
118: // Post: The program has paused and displayed a one-line message
119: //       indented indentLevel spaces, after which the user has
120: //       pressed the ENTER key.
121: {
122:     cout << setw(indentLevel) << ""
123:          << "Press return to continue ... ";
124:     char returnChar;
125:     cin.get(returnChar);
126:     cout << endl;
127: }