You are on page 1of 2

// Filename: INVSRCH.

CPP
// Demonstrates the sequential parallel array searching
// technique. An inventory ID number is asked for. If
// the ID is found in the arrays, that product's
// inventory data is displayed for the user.
#include <iostream>
#include <iomanip>
using namespace std;

int GetKey();
int SearchInv(int keyVal, int prodID[]);
void PrintData(int foundSub, int prodID[], int numItems[],
float price[], int reorder[]);

const int INV = 10; // Products in this sample array

int main() {

// First, initialize a few sample elements


// Product IDs
int prodID[INV] = {32, 45, 76, 10, 94,
52, 27, 29, 87, 60};
// Number of each product currently in the inventory
int numItems[INV] = {6, 2, 1, 0, 8,
2, 4, 7, 9, 3};
// Price of each product
float price[INV] = {5.43, 6.78, 8.64, 3.32, 1.92,
7.03, 9.87, 7.65, 4.63, 2.38};
// Reorder levels
int reorder[INV] = {5, 4, 1, 3, 5,
6, 2, 1, 1, 4};
int keyVal, foundSub;

// The program's primary logic begins here


keyVal = GetKey(); // Ask the user for an ID
foundSub = SearchInv(keyVal, prodID); // Search the inventory
if (foundSub == -99) {
cout << "That product is not in the inventory.";
return -1;
}
// Here only if the item was found
PrintData(foundSub, prodID, numItems, price, reorder);
return 0;
}
//**********************************************************
int GetKey() {
// Ask the user for an ID
int keyVal;
cout << "** Inventory Search Program **" << endl;
cout << endl << endl << "Enter a product ID: ";
cin >> keyVal;
return (keyVal);
}
//**********************************************************
int SearchInv(int keyVal, int prodID[]) {
// Search the ID array and return the subscript of
// the found product, or return -99 if not found
int foundSub = -99;
int c;
for (c = 0; c < INV; c++) {
if (keyVal == prodID[c]) {
foundSub = c;
break;
}
}
// The -99 will still be in foundSub
// if the search failed
return (foundSub);
}
//**********************************************************
void PrintData(int foundSub, int prodID[], int numItems[],
float price[], int reorder[]) {
// Print the data from the matching parallel arrays
cout << setprecision(2);
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout << endl << "Product ID: " << prodID[foundSub]
<< "\tNumber in stock: "
<< numItems[foundSub] << endl;
cout << "Price: $" << price[foundSub] << "\tReorder: "
<< reorder[foundSub] << endl;
return;
}

You might also like