Please write out an inventory management program in C++.
- User
User: Please write out an inventory management program in C++.
- Assistant
User: Please write out an inventory management program in C++.
Certainly! Below is a simple console-based Inventory Management Program written in C++. This program allows you to:
The inventory data is managed in-memory using a std::vector. For persistent storage, you could extend this program to read from and write to a file.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
// Structure to represent an inventory item
struct Item {
int id;
std::string name;
int quantity;
};
// Function prototypes
void displayMenu();
void addItem(std::vector<Item>& inventory);
void removeItem(std::vector<Item>& inventory);
void displayInventory(const std::vector<Item>& inventory);
void searchItem(const std::vector<Item>& inventory);
void updateItemQuantity(std::vector<Item>& inventory);
int main() {
std::vector<Item> inventory;
int choice;
do {
displayMenu();
std::cout << "Enter your choice: ";
std::cin >> choice;
// Clear input buffer to handle any leftover input
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (choice) {
case 1:
addItem(inventory);
break;
case 2:
removeItem(inventory);
break;
case 3:
displayInventory(inventory);
break;
case 4:
searchItem(inventory);
break;
case 5:
updateItemQuantity(inventory);
break;
case 6:
std::cout << "Exiting program. Goodbye!\n";
break;
default:
std::cout << "Invalid choice. Please select a valid option.\n";
}
std::cout << "\n";
} while (choice != 6);
return 0;
}
void displayMenu() {
std::cout << "========== Inventory Management ==========\n";
std::cout << "1. Add Item\n";
std::cout << "2. Remove Item\n";
std::cout << "3. Display Inventory\n";
std::cout << "4. Search Item\n";
std::cout << "5. Update Item Quantity\n";
std::cout << "6. Exit\n";
std::cout << "==========================================\n";
}
void addItem(std::vector<Item>& inventory) {
Item newItem;
std::cout << "Enter Item ID: ";
std::cin >> newItem.id;
std::cin.ignore(); // Clear newline character from input buffer
std::cout << "Enter Item Name: ";
std::getline(std::cin, newItem.name);
std::cout << "Enter Quantity: ";
std::cin >> newItem.quantity;
// Check for duplicate ID
auto it = std::find_if(inventory.begin(), inventory.end(),
[&](const Item& item) { return item.id == newItem.id; });
if (it != inventory.end()) {
std::cout << "Item with ID " << newItem.id << " already exists.\n";
} else {
inventory.push_back(newItem);
std::cout << "Item added successfully.\n";
}
}
void removeItem(std::vector<Item>& inventory) {
if (inventory.empty()) {
std::cout << "Inventory is empty. Nothing to remove.\n";
return;
}
int id;
std::cout << "Enter Item ID to remove: ";
std::cin >> id;
auto it = std::remove_if(inventory.begin(), inventory.end(),
[&](const Item& item) { return item.id == id; });
if (it != inventory.end()) {
inventory.erase(it, inventory.end());
std::cout << "Item removed successfully.\n";
} else {
std::cout << "Item with ID " << id << " not found.\n";
}
}
void displayInventory(const std::vector<Item>& inventory) {
if (inventory.empty()) {
std::cout << "Inventory is empty.\n";
return;
}
std::cout << "======= Current Inventory =======\n";
std::cout << "ID\tName\t\tQuantity\n";
std::cout << "--------------------------------\n";
for (const auto& item : inventory) {
std::cout << item.id << "\t" << item.name << "\t\t" << item.quantity << "\n";
}
std::cout << "================================\n";
}
void searchItem(const std::vector<Item>& inventory) {
if (inventory.empty()) {
std::cout << "Inventory is empty. Nothing to search.\n";
return;
}
std::string name;
std::cout << "Enter Item Name to search: ";
std::getline(std::cin, name);
bool found = false;
for (const auto& item : inventory) {
if (item.name == name) {
std::cout << "Item Found: ID = " << item.id << ", Quantity = " << item.quantity << "\n";
found = true;
break;
}
}
if (!found) {
std::cout << "Item \"" << name << "\" not found in inventory.\n";
}
}
void updateItemQuantity(std::vector<Item>& inventory) {
if (inventory.empty()) {
std::cout << "Inventory is empty. Nothing to update.\n";
return;
}
int id;
std::cout << "Enter Item ID to update quantity: ";
std::cin >> id;
for (auto& item : inventory) {
if (item.id == id) {
int newQty;
std::cout << "Current Quantity: " << item.quantity << "\n";
std::cout << "Enter New Quantity: ";
std::cin >> newQty;
item.quantity = newQty;
std::cout << "Quantity updated successfully.\n";
return;
}
}
std::cout << "Item with ID " << id << " not found.\n";
}
Item Structure: Defines the structure of an inventory item with an id, name, and quantity.
Main Function:
std::vector<Item> to store the inventory items.Display Menu: Shows the available options to the user.
Add Item:
Remove Item:
Display Inventory:
Search Item:
Update Item Quantity:
Save the Code: Save the above code in a file named inventory_management.cpp.
Compile: Use a C++ compiler like g++ to compile the program. Open your terminal or command prompt and navigate to the directory containing the file, then run:
g++ -o inventory_management inventory_management.cpp
Run:
./inventory_management
(Note: On Windows, the executable will be inventory_management.exe.)
========== Inventory Management ==========
1. Add Item
2. Remove Item
3. Display Inventory
4. Search Item
5. Update Item Quantity
6. Exit
==========================================
Enter your choice: 1
Enter Item ID: 101
Enter Item Name: Widget
Enter Quantity: 50
Item added successfully.
========== Inventory Management ==========
1. Add Item
2. Remove Item
3. Display Inventory
4. Search Item
5. Update Item Quantity
6. Exit
==========================================
Enter your choice: 3
======= Current Inventory =======
ID Name Quantity
--------------------------------
101 Widget 50
================================
This basic program should serve as a good starting point for further development based on your specific requirements.
Not yet included in a published release.