1: // Filenme: ARRITEM.CPP
2: // Purpose: Illustrates an array of (ItemType) structs.
4: #include <iostream>
5: #include <iomanip>
6: using namespace std;
8: #include "PAUSE.H"
11: const int MAX_NAME_SIZE = 10;
12: typedef char NameType[MAX_NAME_SIZE+1];
14: struct ItemType
15: {
16: int idNumber;
17: NameType itemName;
18: int numberInStock;
19: bool warrantyAvailable;
20: double price;
21: };
24: void InitItem(/* out */ ItemType& item,
25: /* in */ int idNumber,
26: /* in */ const NameType itemName,
27: /* in */ int numberInStock,
28: /* in */ bool warrantyAvailable,
29: /* in */ double price);
31: void UpdateItemCount(/* inout */ ItemType& item,
32: /* in */ int numberInOrOut);
34: void DisplayItemInfo(/* in */ const ItemType& item);
37: int main()
38: {
39: cout << "\nThis program uses an array of "
40: << "structs to deal with items in inventory. " << endl
41: << "You should study the source code "
42: << "at the same time as the output. " << endl << endl;
44: const int SIZE = 4;
45: ItemType items[SIZE] = {{123, "computer", 15, true, 1999.95},
46: {147, "printer", 11, false, 199.95},
47: {216, "monitor", 13, true, 249.95},
48: {321, "modem", 5, false, 69.95}};
50: int i;
51: for (i = 0; i < SIZE; i++)
52: {
53: DisplayItemInfo(items[i]); cout << endl;
54: Pause(0);
55: }
57: UpdateItemCount(items[0], 7);
58: UpdateItemCount(items[1], -3);
59: UpdateItemCount(items[2], 2);
60: UpdateItemCount(items[3], -1);
62: for (i = 0; i < SIZE; i++)
63: {
64: DisplayItemInfo(items[i]); cout << endl;
65: Pause(0);
66: }
68: return 0;
69: }
72: //******************************************************************
73: void InitItem(/* out */ ItemType& item,
74: /* in */ int idNumber,
75: /* in */ const NameType itemName,
76: /* in */ int numberInStock,
77: /* in */ bool warrantyAvailable,
78: /* in */ double price)
79: // Pre: All in-parameters have been initialized.
80: // Post: The information in all in-parameters has been
81: // stored in "item", which is then returned.
82: {
83: item.idNumber = idNumber;
84: strcpy(item.itemName, itemName);
85: item.numberInStock = numberInStock;
86: item.warrantyAvailable = warrantyAvailable;
87: item.price = price;
88: }
92: //******************************************************************
93: void UpdateItemCount(/* inout */ ItemType& item,
94: /* in */ int numberInOrOut)
95: // Pre: item and numberInOrOut have been initialized.
96: // Post: The number available of item has been updated by the
97: // amount numberInOrOut, which may be positive or negative.
98: {
99: item.numberInStock += numberInOrOut;
100: }
104: //******************************************************************
105: void DisplayItemInfo(/* in */ const ItemType& item)
106: // Pre: item has been initialized.
107: // Post: The information in all fields of item has been displayed.
108: {
109: cout.setf(ios::fixed, ios::floatfield);
110: cout.setf(ios::showpoint);
111: cout << setprecision(2);
112: cout << "Item Name: " << item.itemName << endl;
113: cout << "Item ID Number: " << item.idNumber << endl;
114: cout << "Number in stock: " << item.numberInStock << endl;
115: cout << "Current price: $" << item.price << endl;
116: cout << "Warranty Avaiable: "
117: << (item.warrantyAvailable ? "Yes" : "No") << endl;
118: }