COSC 1320 - C++ Programming and
ITSE 1307 - Introduction to C++ Programming
Bob Comer, Professor of Computer Studies
Using Arrays of Structs
To try this program with your compiler, highlight the program text below, make a copy of it (ctrl-c in Windows), open a source code window in your compiler and paste the program code into the window (ctrl-v in Windows).
//**************************************************************
//* Programmer: Bob Comer
//*
//* This program allows the user to look up item prices
//* at a store. The price information is loaded from
//* a file and stored in an array of structs.
//**************************************************************
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_ITEMS = 200;
struct ItemRec
{
int itemNo; //inventory item number
float price; //item price
};
void getItems( ifstream&, ItemRec[], int&);
int search( int, const ItemRec[], int);
int main()
{
ItemRec item[MAX_ITEMS]; // inventory items
int numItems; // number of items in the lists
int srchItemNo; // an item number to look up
int position; // position of an item in list
ifstream itemFile; // item price file
// open Item Price file
itemFile.open("a:\\cis2003\\itemfile.txt");
if (!itemFile)
{
cout << "Can not open item file \"items.txt\"" << endl;
return 1;
}
// load item price file
getItems( itemFile, item, numItems);
// look up item prices
cout << "Enter Item Number (or -1 to quit): ";
cin >> srchItemNo;
while (srchItemNo > 0)
{
// get price for item
position = search( srchItemNo, item, numItems);
if (position == -1)
{
cout << "Item " << srchItemNo
<< " is not in the inventory" << endl;
}
else
cout << "Price for item " << srchItemNo
<< " is $ " << item[position].price << endl;
// get next item
cout << "Enter Item Number (or -1 to quit): ";
cin >> srchItemNo;
}
return 0;
}
//**************************************************************
//* FUNCTION getItems
//*
//* Copies item information from file into arrays
//**************************************************************
void getItems( ifstream& itemFile, // in - item price file
ItemRec item[], // out - list of items
int& numItems) // out - number of items
{
int oneItemNo; // an item number
float onePrice; // price for the item
numItems = 0;
itemFile >> oneItemNo >> onePrice;
while (itemFile && numItems < MAX_ITEMS)
{
// move new item into arrays and increment item count
item[numItems].itemNo = oneItemNo;
item[numItems].price = onePrice;
numItems++;
// get next item information
itemFile >> oneItemNo >> onePrice;
}
}
//**************************************************************
//* FUNCTION search
//*
//* Find the location of an item in a list.
//*
//**************************************************************
int search( int srchVal, // in - value to search for
const ItemRec list[], // in - list to search
int length) // in - number of items in list
{
// search list for item and return its position if found
int index = 0;
while (index < length)
{
if (srchVal == list[index].itemNo)
return index;
index++;
}
// item not found - return a flag
return -1;
}