[cpp] f
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <iomanip>
- using namespace std;
- struct InventoryItem {
- char name[50];
- int quantity;
- double wholesaleCost;
- double retailCost;
- };
- void addRecord(const string& filename);
- void displayRecord(const string& filename);
- void changeRecord(const string& filename);
- void displayAllRecords(const string& filename);
- void generateReport(const string& filename);
- int main() {
- const string filename = "inventory.dat";
- int choice;
- do {
- cout << "1. Add New Record\n";
- cout << "2. Display Record\n";
- cout << "3. Change Record\n";
- cout << "4. Display All Records\n";
- cout << "5. Generate Report\n";
- cout << "6. Exit\n";
- cout << "Enter your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- addRecord(filename);
- break;
- case 2:
- displayRecord(filename);
- break;
- case 3:
- changeRecord(filename);
- break;
- case 4:
- displayAllRecords(filename);
- break;
- case 5:
- generateReport(filename);
- break;
- case 6:
- cout << "Exiting program.\n";
- break;
- default:
- cout << "Invalid choice. Please try again.\n";
- }
- } while (choice != 6);
- return 0;
- }
- void addRecord(const string& filename) {
- InventoryItem item;
- cout << "Enter item name: ";
- cin.ignore();
- cin.getline(item.name, 50);
- cout << "Enter quantity: ";
- cin >> item.quantity;
- cout << "Enter wholesale cost: ";
- cin >> item.wholesaleCost;
- cout << "Enter retail cost: ";
- cin >> item.retailCost;
- if (item.quantity < 0 || item.wholesaleCost < 0 || item.retailCost < 0) {
- cout << "Invalid input. All values must be >= 0.\n";
- return;
- }
- ofstream outFile(filename, ios::binary | ios::app);
- if (!outFile) {
- cout << "Error opening file.\n";
- return;
- }
- outFile.write(reinterpret_cast<char*>(&item), sizeof(InventoryItem));
- outFile.close();
- }
- void displayRecord(const string& filename) {
- ifstream inFile(filename, ios::binary);
- if (!inFile) {
- cout << "Error opening file.\n";
- return;
- }
- InventoryItem item;
- string searchName;
- cout << "Enter item name or first few letters: ";
- cin.ignore();
- getline(cin, searchName);
- bool found = false;
- while (inFile.read(reinterpret_cast<char*>(&item), sizeof(InventoryItem))) {
- if (string(item.name).substr(0, searchName.size()) == searchName) {
- cout << "Item Name: " << item.name << "\n"
- << "Quantity: " << item.quantity << "\n"
- << "Wholesale Cost: $" << fixed << setprecision(2) << item.wholesaleCost << "\n"
- << "Retail Cost: $" << item.retailCost << "\n\n";
- found = true;
- break;
- }
- }
- if (!found) {
- cout << "Item not found.\n";
- }
- inFile.close();
- }
- void changeRecord(const string& filename) {
- fstream file(filename, ios::in | ios::out | ios::binary);
- if (!file) {
- cout << "Error opening file.\n";
- return;
- }
- InventoryItem item;
- string searchName;
- cout << "Enter the name of the item to modify: ";
- cin.ignore();
- getline(cin, searchName);
- bool found = false;
- long long pos = 0;
- while (file.read(reinterpret_cast<char*>(&item), sizeof(InventoryItem))) {
- if (string(item.name) == searchName) {
- cout << "Current item details:\n";
- cout << "Item Name: " << item.name << "\n"
- << "Quantity: " << item.quantity << "\n"
- << "Wholesale Cost: $" << fixed << setprecision(2) << item.wholesaleCost << "\n"
- << "Retail Cost: $" << item.retailCost << "\n";
- cout << "Enter new details for the item:\n";
- cout << "Enter item name: ";
- cin.getline(item.name, 50);
- cout << "Enter quantity: ";
- cin >> item.quantity;
- cout << "Enter wholesale cost: ";
- cin >> item.wholesaleCost;
- cout << "Enter retail cost: ";
- cin >> item.retailCost;
- if (item.quantity < 0 || item.wholesaleCost < 0 || item.retailCost < 0) {
- cout << "Invalid input. All values must be >= 0.\n";
- file.close();
- return;
- }
- file.seekp(pos);
- file.write(reinterpret_cast<char*>(&item), sizeof(InventoryItem));
- found = true;
- break;
- }
- pos += sizeof(InventoryItem);
- }
- if (!found) {
- cout << "Item not found.\n";
- }
- file.close();
- }
- void displayAllRecords(const string& filename) {
- ifstream inFile(filename, ios::binary);
- if (!inFile) {
- cout << "Error opening file.\n";
- return;
- }
- InventoryItem item;
- while (inFile.read(reinterpret_cast<char*>(&item), sizeof(InventoryItem))) {
- cout << "Item Name: " << item.name << "\n"
- << "Quantity: " << item.quantity << "\n"
- << "Wholesale Cost: $" << fixed << setprecision(2) << item.wholesaleCost << "\n"
- << "Retail Cost: $" << item.retailCost << "\n\n";
- }
- inFile.close();
- }
- void generateReport(const string& filename) {
- ifstream inFile(filename, ios::binary);
- if (!inFile) {
- cout << "Error opening file.\n";
- return;
- }
- double totalWholesale = 0.0, totalRetail = 0.0;
- int totalQuantity = 0;
- InventoryItem item;
- while (inFile.read(reinterpret_cast<char*>(&item), sizeof(InventoryItem))) {
- totalWholesale += item.wholesaleCost * item.quantity;
- totalRetail += item.retailCost * item.quantity;
- totalQuantity += item.quantity;
- }
- cout << "Total Wholesale Value: $" << fixed << setprecision(2) << totalWholesale << "\n";
- cout << "Total Retail Value: $" << totalRetail << "\n";
- cout << "Total Quantity: " << totalQuantity << "\n";
- inFile.close();
- }
Editor
You can edit this paste and save as new: