Parallel Arrays

//  this program uses parallel arrays to hold item numbers and prices.
//  after the program reads data from the file 'prices.dat' to fill
//  the arrays, the user is prompted to enter L to lookup the price of
//  an item, or C to change the price of an item. the user is prompted
//  repeatedly until he/she enters Q

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>

//  prototypes
char getRequest();
void getItemInfo(int[],float[],int&);
void processRequest(char,int[],float[],int);
int search(int[],int,int);

const char QUIT = 'q';
const char LOOKUP = 'l';
const char CHANGE = 'c';
const int NOTFOUND = -1;
const int LIMIT = 100;

int main()
{
   int items[LIMIT];
   float prices[LIMIT];
   char request;
   int size;
   int loc;
   int itemnum;

   getItemInfo(items,prices,size);
   request = getRequest();
   while (request != QUIT)
   {
      cout << "Enter the item number: ";
      cin >> itemnum;
      loc = search(items, size, itemnum);
      if (loc == NOTFOUND)
         cout << "Error: invalid item number\n\n";
      else
         processRequest(request,items,prices,loc);
      request = getRequest();
   }
}

//  read data from file 'prices.dat' and store in items and
//  prices arrays
void getItemInfo(int items[], float prices[], int&size)
{
   ifstream itemFile;
   itemFile.open("prices.dat");
   size = 0;
   itemFile >> items[0] >> prices[0];
   while (itemFile && size < LIMIT)
   {
      size++;
      itemFile >> items[size] >> prices[size];
   }
}

//  prompt user for request (L, C, or Q). keep prompting until a valid
//  response is entered
char getRequest()
{
   char req;
   do
   {
      cout << "Request (l or L for lookup; c or C for change; "
            << "q or Q for quit): ";
      cin >> req;
   } while (req != 'l' && req != 'L' && req != 'c' && req != 'C'
                  && req != 'q' && req != 'Q');
   if (req == 'l' || req == 'L')
      return LOOKUP;
   if (req == 'c' || req == 'C')
      return CHANGE;
   if (req == 'q' || req == 'Q')
      return QUIT;
}

//  process the user request to lookup a price or change a price
void processRequest(char request, int items[], float prices[], int loc)
{
   if (request == LOOKUP)
      cout << "Cost of item number " << items[loc] << ": $"
            << prices[loc] << endl << endl;
   if (request == CHANGE)
   {
      cout << "Enter new price for item " << items[loc] << ": ";
      cin >> prices[loc];
      cout << endl;
   }
}

//  search list for value, assuming that length positions of list have
//  been filled; return the subscript where value was found or
//  -1 if not found
int search(int list[],int length, int value)
{
   int location = -1;
   int i = 0;
   while (i < length && location == -1)
   {
      if (list[i] == value)
         location = i;
      i++;
   }
   return location;
}

Input File prices.dat

1234 49.95
3324 179.95
8844 99.49
9753 124.50
4224 29.95
6161 74.49

Sample Output

Request (lookup: l or L, change: c or C, quit: q or Q): l
Enter the item number: 3324
Cost of item number 3324: $179.95

Request (lookup: l or L, change: c or C, quit: q or Q): l
Enter the item number: 8844
Cost of item number 8844: $99.49

Request (lookup: l or L, change: c or C, quit: q or Q): e
Request (lookup: l or L, change: c or C, quit: q or Q): w
Request (lookup: l or L, change: c or C, quit: q or Q): a
Request (lookup: l or L, change: c or C, quit: q or Q): c
Enter the item number: 8844
Enter new price for item 8844: 97.99

Request (lookup: l or L, change: c or C, quit: q or Q): l
Enter the item number: 8844
Cost of item number 8844: $97.99

Request (lookup: l or L, change: c or C, quit: q or Q): l
Enter the item number: 4224
Cost of item number 4224: $29.95

Request (lookup: l or L, change: c or C, quit: q or Q): c
Enter the item number: 4224
Enter new price for item 4224: 39.95

Request (lookup: l or L, change: c or C, quit: q or Q): l
Enter the item number: 4224
Cost of item number 4224: $39.95

Request (lookup: l or L, change: c or C, quit: q or Q): l
Enter the item number: 9999
Error: invalid item number

Request (lookup: l or L, change: c or C, quit: q or Q): c
Enter the item number: 8888
Error: invalid item number

Request (lookup: l or L, change: c or C, quit: q or Q): q


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002