// structdemo.cpp CIS 205 C/C++ Programming 04/15/2002
// This program demonstrates structure passing by value and by reference
// Adapted from Gaddis Fig 11.8 p. 626
/* Sample Interaction
C:\cis205\2002\spring\examples>structdemo
You will be prompted to enter information for 3 items:
Enter part number 1 (25 chars max): 235EFX
Enter part 1 description (100 chars max): Widget Number One
Enter part 1 qty unit of measure (each, lbs ...): each
Enter part 1 inventory quantity on hand: 144
Enter part 1 price ($'s): 19.87
Enter part number 2 (25 chars max): 34-01-45e
Enter part 2 description (100 chars max): Widget Number Two
Enter part 2 qty unit of measure (each, lbs ...): gallons
Enter part 2 inventory quantity on hand: 2400
Enter part 2 price ($'s): 1.48
Enter part number 3 (25 chars max): T-33RT (blue)
Enter part 3 description (100 chars max): Widget Number Three
Enter part 3 qty unit of measure (each, lbs ...): lbs
Enter part 3 inventory quantity on hand: 23000
Enter part 3 price ($'s): 0.47
Here is the information you entered:
Information for item 1:
Part Number: 35EFX
Description: Widget Number One
Qty on Hand: 144.00 each
Unit Price: $19.87
Information for item 2:
Part Number: 34-01-45e
Description: Widget Number Two
Qty on Hand: 2400.00 gallons
Unit Price: $1.48
Information for item 3:
Part Number: T-33RT (blue)
Description: Widget Number Three
Qty on Hand: 23000.00 lbs
Unit Price: $0.47
*/
#define MAX_ITEMS 3 // change as needed
#define MAX_CHARS 50 // change as needed
#include <iostream.h>
struct InvItem // note uppercase!
{
// note primitive variable sanity notation
// intVarName, chrVarName, fltVarName, dblVarname
char chrPartNumber[MAX_CHARS / 2]; // part numbers can be alphanumeric!
char chrPartDesc[MAX_CHARS * 2];
char chrQtyUnit[MAX_CHARS / 4]; // gallons, lbs, kg, each etc.
float fltQtyOnHand; // quantites for example weights can be fractional!
float fltUnitPrice;
};
// function skeletons - prototypes
// a mutator method; note &, we send a memory address of an InvItem:
void setItemInfo(int, InvItem &);
// an accessor method, we send a copy of an InvItem:
void showItemInfo(int, InvItem);
int main(void)
{
int i;
InvItem partInvItem[MAX_ITEMS]; // note sanity notation: nameStructName
cout << "You will be prompted to enter information for 3 items:" << endl;
cout << endl;
for(i = 0; i < MAX_ITEMS; i++)
{
setItemInfo(i, partInvItem[i]);
cout << endl;
}
cout << "Here is the information you entered:" << endl;
cout << endl;
for(i = 0; i < MAX_ITEMS; i++)
{
showItemInfo(i, partInvItem[i]);
cout << endl;
}
return 0;
}
// this method accepts a memory reference to the specific structure item we want to update
// note the & used to specify a memory address
void setItemInfo(int intItemCount, InvItem &someInvItem)
{
cin.get(); // discard the rest of the line
cout << "Enter part number " << (intItemCount + 1) << " (" << (MAX_CHARS / 2) << " chars max): ";
cin.getline(someInvItem.chrPartNumber, MAX_CHARS);
cout << "Enter part " << (intItemCount + 1) << " description (" << (MAX_CHARS * 2) << " chars max): ";
// we need to be careful, the description is likely to have spaces in it!
// if we used cin someInvItem.description we would only capture
// up to the first space character text the user entered
cin.getline(someInvItem.chrPartDesc, MAX_CHARS);
cout << "Enter part " << (intItemCount + 1) << " qty unit of measure (" << (MAX_CHARS / 4) << " chars--each, lbs): ";
cin.getline(someInvItem.chrQtyUnit, MAX_CHARS);
cout << "Enter part " << (intItemCount + 1) << " inventory quantity on hand: ";
cin >> someInvItem.fltQtyOnHand;
// prompt the user to enter the price here!!
}
// this method accepts a copy of the specific structure item we want to show
void showItemInfo(int intItemCount, InvItem someInvItem)
{
// first some output formatting instructions to make for pretty output
cout.precision(2);
cout.setf(ios::fixed); // always show trailing zeros
cout.setf(ios::showpoint); // always show the decimal point
// now we can output the information
// put your code here!!
}