Certainly! Below is a simple console-based Inventory Management Program written in C++. This program allows users to:
cpp
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Structure to represent an inventory item
struct Item {
int id;
string name;
int quantity;
double price;
};
// Function Prototypes
void displayMenu();
void addItem(vector<Item> &inventory);
void viewInventory(const vector<Item> &inventory);
void updateItem(vector<Item> &inventory);
void deleteItem(vector<Item> &inventory);
void searchItem(const vector<Item> &inventory);
void saveToFile(const vector<Item> &inventory, const string &filename);
void loadFromFile(vector<Item> &inventory, const string &filename);
bool idExists(const vector<Item> &inventory, int id);
int main() {
vector<Item> inventory;
string filename = "inventory.txt";
loadFromFile(inventory, filename);
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
// Handle invalid input (non-integer)
if(cin.fail()){
cin.clear(); // clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard invalid input
cout << "Invalid input! Please enter a number between 1 and 7.\n";
continue;
}
switch(choice) {
case 1:
addItem(inventory);
break;
case 2:
viewInventory(inventory);
break;
case 3:
updateItem(inventory);
break;
case 4:
deleteItem(inventory);
break;
case 5:
searchItem(inventory);
break;
case 6:
saveToFile(inventory, filename);
cout << "Inventory saved to " << filename << " successfully.\n";
break;
case 7:
saveToFile(inventory, filename);
cout << "Exiting the program. Inventory saved to " << filename << ".\n";
break;
default:
cout << "Invalid choice! Please select a number between 1 and 7.\n";
}
} while(choice != 7);
return 0;
}
void displayMenu(){
cout << "\n===== Inventory Management System =====\n";
cout << "1. Add New Item\n";
cout << "2. View Inventory\n";
cout << "3. Update Item\n";
cout << "4. Delete Item\n";
cout << "5. Search Item\n";
cout << "6. Save Inventory\n";
cout << "7. Exit\n";
}
bool idExists(const vector<Item> &inventory, int id){
for(const auto &item : inventory){
if(item.id == id) return true;
}
return false;
}
void addItem(vector<Item> &inventory){
Item newItem;
cout << "Enter Item ID: ";
cin >> newItem.id;
// Check for unique ID
if(idExists(inventory, newItem.id)){
cout << "Item with ID " << newItem.id << " already exists.\n";
return;
}
cout << "Enter Item Name: ";
cin.ignore(); // To ignore the newline character left in the buffer
getline(cin, newItem.name);
cout << "Enter Quantity: ";
cin >> newItem.quantity;
if(cin.fail() || newItem.quantity < 0){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid quantity. Operation cancelled.\n";
return;
}
cout << "Enter Price: ";
cin >> newItem.price;
if(cin.fail() || newItem.price < 0){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid price. Operation cancelled.\n";
return;
}
inventory.push_back(newItem);
cout << "Item added successfully!\n";
}
void viewInventory(const vector<Item> &inventory){
if(inventory.empty()){
cout << "Inventory is empty.\n";
return;
}
cout << left << setw(10) << "ID"
<< left << setw(30) << "Name"
<< left << setw(10) << "Quantity"
<< left << setw(10) << "Price" << endl;
cout << string(60, '-') << endl;
for(const auto &item : inventory){
cout << left << setw(10) << item.id
<< left << setw(30) << item.name
<< left << setw(10) << item.quantity
<< left << setw(10) << fixed << setprecision(2) << item.price
<< endl;
}
}
void updateItem(vector<Item> &inventory){
if(inventory.empty()){
cout << "Inventory is empty. Nothing to update.\n";
return;
}
int id;
cout << "Enter the ID of the item to update: ";
cin >> id;
for(auto &item : inventory){
if(item.id == id){
cout << "Item found: " << item.name << "\n";
cout << "Enter new name (leave blank to keep current): ";
cin.ignore();
string newName;
getline(cin, newName);
if(!newName.empty()){
item.name = newName;
}
cout << "Enter new quantity (enter -1 to keep current): ";
int newQty;
cin >> newQty;
if(!cin.fail() && newQty >= 0){
item.quantity = newQty;
} else if(newQty != -1){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid quantity input. Quantity not updated.\n";
}
cout << "Enter new price (enter -1 to keep current): ";
double newPrice;
cin >> newPrice;
if(!cin.fail() && newPrice >= 0){
item.price = newPrice;
} else if(newPrice != -1){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid price input. Price not updated.\n";
}
cout << "Item updated successfully!\n";
return;
}
}
cout << "Item with ID " << id << " not found.\n";
}
void deleteItem(vector<Item> &inventory){
if(inventory.empty()){
cout << "Inventory is empty. Nothing to delete.\n";
return;
}
int id;
cout << "Enter the ID of the item to delete: ";
cin >> id;
for(auto it = inventory.begin(); it != inventory.end(); ++it){
if(it->id == id){
cout << "Are you sure you want to delete item \"" << it->name << "\"? (y/n): ";
char confirm;
cin >> confirm;
if(confirm == 'y' || confirm == 'Y'){
inventory.erase(it);
cout << "Item deleted successfully.\n";
} else {
cout << "Deletion cancelled.\n";
}
return;
}
}
cout << "Item with ID " << id << " not found.\n";
}
void searchItem(const vector<Item> &inventory){
if(inventory.empty()){
cout << "Inventory is empty.\n";
return;
}
cout << "Search by:\n1. ID\n2. Name\nEnter choice: ";
int choice;
cin >> choice;
if(choice == 1){
int id;
cout << "Enter Item ID: ";
cin >> id;
for(const auto &item : inventory){
if(item.id == id){
cout << "Item Found:\n";
cout << "ID: " << item.id << "\nName: " << item.name
<< "\nQuantity: " << item.quantity
<< "\nPrice: " << fixed << setprecision(2) << item.price << "\n";
return;
}
}
cout << "No item found with ID " << id << ".\n";
}
else if(choice == 2){
cout << "Enter Item Name: ";
cin.ignore();
string name;
getline(cin, name);
bool found = false;
for(const auto &item : inventory){
if(item.name.find(name) != string::npos){
if(!found){
cout << "Matching Items:\n";
cout << left << setw(10) << "ID"
<< left << setw(30) << "Name"
<< left << setw(10) << "Quantity"
<< left << setw(10) << "Price" << endl;
cout << string(60, '-') << endl;
found = true;
}
cout << left << setw(10) << item.id
<< left << setw(30) << item.name
<< left << setw(10) << item.quantity
<< left << setw(10) << fixed << setprecision(2) << item.price
<< endl;
}
}
if(!found){
cout << "No items match the name \"" << name << "\".\n";
}
}
else{
cout << "Invalid choice. Returning to main menu.\n";
}
}
void saveToFile(const vector<Item> &inventory, const string &filename){
ofstream outFile(filename);
if(!outFile){
cout << "Error opening file for writing.\n";
return;
}
for(const auto &item : inventory){
outFile << item.id << ','
<< item.name << ','
<< item.quantity << ','
<< item.price << '\n';
}
outFile.close();
}
void loadFromFile(vector<Item> &inventory, const string &filename){
ifstream inFile(filename);
if(!inFile){
// File does not exist. Start with empty inventory.
return;
}
string line;
while(getline(inFile, line)){
if(line.empty()) continue;
Item item;
size_t pos1 = line.find(',');
if(pos1 == string::npos) continue;
item.id = stoi(line.substr(0, pos1));
size_t pos2 = line.find(',', pos1 + 1);
if(pos2 == string::npos) continue;
item.name = line.substr(pos1 + 1, pos2 - pos1 - 1);
size_t pos3 = line.find(',', pos2 + 1);
if(pos3 == string::npos) continue;
item.quantity = stoi(line.substr(pos2 + 1, pos3 - pos2 - 1));
item.price = stod(line.substr(pos3 + 1));
inventory.push_back(item);
}
inFile.close();
}
code
===== Inventory Management System =====
1. Add New Item
2. View Inventory
3. Update Item
4. Delete Item
5. Search Item
6. Save Inventory
7. Exit
Enter your choice: 1
Enter Item ID: 101
Enter Item Name: Widget
Enter Quantity: 50
Enter Price: 2.99
Item added successfully!
===== Inventory Management System =====
1. Add New Item
2. View Inventory
3. Update Item
4. Delete Item
5. Search Item
6. Save Inventory
7. Exit
Enter your choice: 2
ID Name Quantity Price
------------------------------------------------------------
101 Widget 50 2.99
This basic program serves as a foundation and can be expanded based on specific requirements. Happy coding!