Analyse this C++ code: <code fileName="untitled" notes="">#include…
- User
User: Analyse this C++ code: #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"; - Assistant