0% found this document useful (0 votes)
21 views4 pages

File Based Inventory Management System CPP

Uploaded by

aryantelang264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

File Based Inventory Management System CPP

Uploaded by

aryantelang264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File-Based Inventory Management System (C++)

Objective:
To design and implement a file-based inventory management system using C++ file handling to
store, update, search, and display product records efficiently.

Features:
• Add new product records
• Display all product records
• Search for a product by ID
• Update product details
• Delete product records
• Data stored permanently in 'inventory.txt'

Source Code (C++):


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Product {
public:
int id;
string name;
int quantity;
float price;

void input() {
cout << "Enter Product ID: ";
cin >> id;
cin.ignore();
cout << "Enter Product Name: ";
getline(cin, name);
cout << "Enter Quantity: ";
cin >> quantity;
cout << "Enter Price: ";
cin >> price;
}

void display() const {


cout << left << setw(10) << id
<< setw(20) << name
<< setw(10) << quantity
<< setw(10) << fixed << setprecision(2) << price << endl;
}
};

void addProduct() {
ofstream file("inventory.txt", ios::app);
if (!file) {
cout << "Error opening file!" << endl;
return;
}

Product p;
p.input();

file << p.id << " " << p.name << " " << p.quantity << " " << p.price << endl;
file.close();
cout << "\n■ Product added successfully!\n";
}

void displayProducts() {
ifstream file("inventory.txt");
if (!file) {
cout << "No records found.\n";
return;
}

Product p;
cout << left << setw(10) << "ID" << setw(20) << "Name"
<< setw(10) << "Quantity" << setw(10) << "Price" << endl;
cout << "--------------------------------------------------------\n";

while (file >> p.id >> p.name >> p.quantity >> p.price) {
p.display();
}

file.close();
}

void searchProduct() {
ifstream file("inventory.txt");
if (!file) {
cout << "No records found.\n";
return;
}

int searchId;
bool found = false;
Product p;

cout << "Enter Product ID to search: ";


cin >> searchId;

while (file >> p.id >> p.name >> p.quantity >> p.price) {
if (p.id == searchId) {
cout << "\n■ Product Found!\n";
cout << "ID: " << p.id << "\nName: " << p.name
<< "\nQuantity: " << p.quantity << "\nPrice: " << p.price << endl;
found = true;
break;
}
}

if (!found) cout << "\n■ Product not found.\n";

file.close();
}

void updateProduct() {
ifstream file("inventory.txt");
ofstream temp("temp.txt");
if (!file || !temp) {
cout << "Error opening file!" << endl;
return;
}
int updateId;
bool found = false;
Product p;

cout << "Enter Product ID to update: ";


cin >> updateId;

while (file >> p.id >> p.name >> p.quantity >> p.price) {
if (p.id == updateId) {
cout << "\nEnter new details:\n";
p.input();
found = true;
}
temp << p.id << " " << p.name << " " << p.quantity << " " << p.price << endl;
}

file.close();
temp.close();

remove("inventory.txt");
rename("temp.txt", "inventory.txt");

if (found)
cout << "\n■ Product updated successfully!\n";
else
cout << "\n■ Product not found.\n";
}

void deleteProduct() {
ifstream file("inventory.txt");
ofstream temp("temp.txt");
if (!file || !temp) {
cout << "Error opening file!" << endl;
return;
}

int deleteId;
bool found = false;
Product p;

cout << "Enter Product ID to delete: ";


cin >> deleteId;

while (file >> p.id >> p.name >> p.quantity >> p.price) {
if (p.id != deleteId) {
temp << p.id << " " << p.name << " " << p.quantity << " " << p.price << endl;
} else {
found = true;
}
}

file.close();
temp.close();

remove("inventory.txt");
rename("temp.txt", "inventory.txt");

if (found)
cout << "\n■ Product deleted successfully!\n";
else
cout << "\n■ Product not found.\n";
}
int main() {
int choice;
do {
cout << "\n========== INVENTORY MANAGEMENT SYSTEM ==========\n";
cout << "1. Add Product\n";
cout << "2. Display All Products\n";
cout << "3. Search Product by ID\n";
cout << "4. Update Product\n";
cout << "5. Delete Product\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: addProduct(); break;
case 2: displayProducts(); break;
case 3: searchProduct(); break;
case 4: updateProduct(); break;
case 5: deleteProduct(); break;
case 6: cout << "Exiting program...\n"; break;
default: cout << "Invalid choice! Try again.\n";
}
} while (choice != 6);

return 0;
}

Conclusion:
This C++ microproject demonstrates file handling and class-based programming concepts by
building an Inventory Management System that stores, updates, and manages records efficiently.

You might also like