Write a program using C++ that maintains inventory records. Assume you can have
ID: 3753805 • Letter: W
Question
Write a program using C++ that maintains inventory records. Assume you can have up to 100 items. Use the pair data type to store the following information (product_name, product_quantity). Hint: you might want to use the bag class to store the pairs into an array and have the functionality of a container class - also good practice to take this approach.
In your main program, create a menu that has the functionality to do the following:
+ :Add new product item with quantity
- :Remove product by product name
d :Display inventory
q :Quit program
Explanation / Answer
Hi There, I have used vector of pair to implement inventory.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector of pair
vector< pair <string,int> > inventory;
string productName;
int productQuantity;
while(1)
{
char d;
cout << "Menu Enter + for adding product to inventory Enter - for removing product from inventory Enter d to dispaly Enter q for quiting program ";
cin >> d;
switch(d)
{
case '+':
cout << "Enter the name of product" << endl;
cin >> productName;
cout << "Enter the Quantity of product" << endl;
cin >> productQuantity;
inventory.push_back( make_pair(productName, productQuantity));
break;
case '-':
cout << "Enter the name of the product to be removed" << endl;
cin >> productName;
for(int i=0;i<inventory.size();i++)
{
if(inventory[i].first == productName)
{
inventory.erase(inventory.begin() + i);
}
}
break;
case 'd':
cout << "List of products" << endl;
for(int i=0;i<inventory.size();i++)
{
cout << inventory[i].first << " " << inventory[i].second << endl;
}
break;
case 'q':
cout << "quiting program";
return 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.