In: Computer Science
Using C++ (microsoft visual studios 2013) create a program that uses three parallel numberic arrays of size 6. The program searches one of the arrays and then displays the corresponding values from the other two arrays. The program should prompt the user to enter ProductID. Valid ProductID's should be the numbers 24, 37, 42, 51, 66 and 79. The program should search the array for the product ID in the ID's array and display the corresponding price and quantity from the prices and quantities array. Populate the prices and quantities arrays with any values you desire. Price should allow for two decimal places. Allow the user to display the price and quantitiy for as many product ID's as desired without having to execute the program again. Of course and bad ProductID's should display an error message, but the user should be able to continue until a sentinel is entered. The program should be structured to work with any numbers should to company change product ID's in the future.
C++ Program:
// products.cpp
#include "stdafx.h"
#include <iostream>
#include <iomanip>
int main()
{
//Three Parallel arrays
//Product id array
int productIds[] = {24, 37, 42, 51, 66, 79};
//Quantity array
int quantity[] = {10, 8, 6, 23, 2, 5};
//Price array
double prices[] = {23.6, 18.4, 65.255, 10.69,
85.144, 50.24};
int i, pos, prodId;
//Reading product id from user
std::cout << "\n Enter product Id (-999 to
Exit): ";
std::cin >> prodId;
//Loop till user enters a sentinel
value
while(prodId != -999)
{
//Initializing pos to
-1
pos = -1;
//Searching for
product id
for(i=0; i<6;
i++)
{
if(productIds[i] == prodId)
{
//Updating pos value
pos = i;
}
}
//Prodcut with given id is not
found
if(pos == -1)
{
std::cout << "\n Invalid Product ID.... \n";
}
else
{
//Product
found
std::cout << "\n Quantity: " << quantity[pos];
std::cout << "\n Price: $" << std::setprecision(2)
<< prices[pos];
}
//Reading product id
from user
std::cout << "\n\n
Enter product Id (-999 to Exit): ";
std::cin >>
prodId;
}
std::cout << "\n\n";
system("pause");
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output: