Source of istruct.cpp


  1: // Filenme: ISTRUCT.CPP
  2: // Purpose: Illustrates a simple struct for representing an
  3: //          item in inventory.

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

  9: struct ItemType
 10: {
 11:     int numberInStock;
 12:     bool warrantyAvailable;
 13:     double price;
 14: };

 16: void InitItem(/* out */ ItemType& item,
 17:               /* in */  int numberInStock,
 18:               /* in */  bool warrantyAvailable,
 19:               /* in */  double price);
 20: void UpdateItemCount(/* inout */ ItemType& item,
 21:                      /* in */    int numberInOrOut);
 22: void DisplayItemInfo(/* in */    const ItemType& item);
 23: void Pause(/* in */ int indentLevel);


 26: int main()
 27: {
 28:     cout << endl
 29:          << "This program uses a struct to "
 30:          << "represent an item in inventory. "   << endl
 31:          << "You should study the source code "
 32:          << "at the same time as the output. "   << endl << endl;

 34:     ItemType item = {15, true, 29.95};
 35:     DisplayItemInfo(item);  cout << endl;
 36:     UpdateItemCount(item, 7);
 37:     DisplayItemInfo(item);  cout << endl;
 38:     Pause(0);

 40:     item.numberInStock = 10;
 41:     item.warrantyAvailable = false;
 42:     item.price = 45.50;
 43:     DisplayItemInfo(item);  cout << endl;
 44:     UpdateItemCount(item, 5);
 45:     DisplayItemInfo(item);  cout << endl;
 46:     Pause(0);

 48:     InitItem(item, 35, true, 100.00);
 49:     DisplayItemInfo(item);  cout << endl;
 50:     UpdateItemCount(item, -15);
 51:     DisplayItemInfo(item);  cout << endl;
 52:     Pause(0);

 54:     ItemType otherItem = item;
 55:     DisplayItemInfo(otherItem);  cout << endl;

 57:     return 0;
 58: }


 61: //******************************************************************
 62: void InitItem(/* out */ ItemType& item,
 63:               /* in */  int numberInStock,
 64:               /* in */  bool warrantyAvailable,
 65:               /* in */  double price)
 66: // Pre:  All in-parameters have been initialized.
 67: // Post: The information in all in-parameters has been
 68: //       stored in item.
 69: {
 70:     item.numberInStock = numberInStock;
 71:     item.warrantyAvailable = warrantyAvailable;
 72:     item.price = price;
 73: }


 76: //******************************************************************
 77: void UpdateItemCount(/* inout */ ItemType& item,
 78:                      /* in */    int numberInOrOut)
 79: // Pre:  item and numberInOrOut have been initialized.
 80: // Post: The number available of item has been updated by the
 81: //       amount numberInOrOut, which may be positive or negative.
 82: {
 83:     item.numberInStock += numberInOrOut;
 84: }



 88: //******************************************************************
 89: void DisplayItemInfo(/* in */ const ItemType& item)
 90: // Pre:  item has been initialized.
 91: // Post: The information in all fields of item has been displayed.
 92: {
 93:     cout.setf(ios::fixed, ios::floatfield);
 94:     cout.setf(ios::showpoint);
 95:     cout << setprecision(2);
 96:     cout << "Number in stock:   "   << item.numberInStock << endl;
 97:     cout << "Current price:     $"  << item.price         << endl;
 98:     cout << "Warranty Avaiable: "
 99:          << (item.warrantyAvailable ? "Yes" : "No")       << endl;
100: }


103: //******************************************************************
104: void Pause(/* in */ int indentLevel)
105: // Pre:  indentLevel has been initialized.
106: //       The input stream cin is empty.
107: // Post: The program has paused and displayed a one-line message
108: //       indented indentLevel spaces, after which the user has
109: //       pressed the ENTER key.
110: {
111:     cout << setw(indentLevel) << ""
112:          << "Press return to continue ... ";
113:     char returnChar;
114:     cin.get(returnChar);
115:     cout << endl;
116: }