-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_3.cpp
More file actions
120 lines (96 loc) · 3.22 KB
/
7_3.cpp
File metadata and controls
120 lines (96 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// Structure to represent an item
struct Product {
char name[50];
int quantity;
float price;
};
// Function to add a new product to the inventory file
void addProduct(const char* filename) {
Product product;
cout << "\nEnter Product Name: ";
cin.ignore(); // Clear input buffer
cin.getline(product.name, 50);
cout << "Enter Quantity: ";
cin >> product.quantity;
cout << "Enter Price: ";
cin >> product.price;
ofstream file(filename, ios::app | ios::binary); // Append in binary mode
if (!file) {
cerr << "Error: Could not open file to add product.\n";
return;
}
file.write(reinterpret_cast<char*>(&product), sizeof(Product));
file.close();
cout << "Product added successfully.\n";
}
// Function to view all products from the inventory file
void viewInventory(const char* filename) {
ifstream file(filename, ios::in | ios::binary);
if (!file) {
cerr << "Error: Could not open inventory file.\n";
return;
}
Product product;
cout << "\n--- Inventory List ---\n";
while (file.read(reinterpret_cast<char*>(&product), sizeof(Product))) {
cout << "Name : " << product.name << endl;
cout << "Quantity : " << product.quantity << endl;
cout << "Price : " << product.price <<" rupees"<< endl;
cout << "----------------------\n";
}
file.close();
}
// Function to search for a product by name
void searchProduct(const char* filename) {
char searchName[50];
cout << "\nEnter Product Name to Search: ";
cin.ignore();
cin.getline(searchName, 50);
ifstream file(filename, ios::in | ios::binary);
if (!file) {
cerr << "Error: Could not open file for searching.\n";
return;
}
Product product;
bool found = false;
while (file.read(reinterpret_cast<char*>(&product), sizeof(Product))) {
if (strcmp(product.name, searchName) == 0) {
cout << "\nProduct Found:\n";
cout << "Name : " << product.name << endl;
cout << "Quantity : " << product.quantity << endl;
cout << "Price : rupees" << product.price << endl;
found = true;
break;
}
}
if (!found) {
cout << "Product not found in inventory.\n";
}
file.close();
}
int main() {
const char* filename = "inventory.dat";
int choice;
do {
cout << "\n=== Inventory Management System ===\n";
cout << "1. Add Product\n";
cout << "2. View Inventory\n";
cout << "3. Search Product\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addProduct(filename); break;
case 2: viewInventory(filename); break;
case 3: searchProduct(filename); break;
case 4: cout << "Exiting...\n"; break;
default: cout << "Invalid choice. Try again.\n";
}
} while (choice != 4);
cout<<endl<<"24CE052_Pushtikansara"<<endl;
return 0;
}