[cpp] f

Viewer

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. struct InventoryItem {
  10.     char name[50];
  11.     int quantity;
  12.     double wholesaleCost;
  13.     double retailCost;
  14. };
  15.  
  16. void addRecord(const string& filename);
  17. void displayRecord(const string& filename);
  18. void changeRecord(const string& filename);
  19. void displayAllRecords(const string& filename);
  20. void generateReport(const string& filename);
  21.  
  22. int main() {
  23.     const string filename = "inventory.dat";
  24.     int choice;
  25.  
  26.     do {
  27.         cout << "1. Add New Record\n";
  28.         cout << "2. Display Record\n";
  29.         cout << "3. Change Record\n";
  30.         cout << "4. Display All Records\n";
  31.         cout << "5. Generate Report\n";
  32.         cout << "6. Exit\n";
  33.         cout << "Enter your choice: ";
  34.         cin >> choice;
  35.  
  36.         switch (choice) {
  37.             case 1:
  38.                 addRecord(filename);
  39.                 break;
  40.             case 2:
  41.                 displayRecord(filename);
  42.                 break;
  43.             case 3:
  44.                 changeRecord(filename);
  45.                 break;
  46.             case 4:
  47.                 displayAllRecords(filename);
  48.                 break;
  49.             case 5:
  50.                 generateReport(filename);
  51.                 break;
  52.             case 6:
  53.                 cout << "Exiting program.\n";
  54.                 break;
  55.             default:
  56.                 cout << "Invalid choice. Please try again.\n";
  57.         }
  58.     } while (choice != 6);
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
  64. void addRecord(const string& filename) {
  65.     InventoryItem item;
  66.     cout << "Enter item name: ";
  67.     cin.ignore();
  68.     cin.getline(item.name50);
  69.     cout << "Enter quantity: ";
  70.     cin >> item.quantity;
  71.     cout << "Enter wholesale cost: ";
  72.     cin >> item.wholesaleCost;
  73.     cout << "Enter retail cost: ";
  74.     cin >> item.retailCost;
  75.  
  76.     if (item.quantity < 0 || item.wholesaleCost < 0 || item.retailCost < 0) {
  77.         cout << "Invalid input. All values must be >= 0.\n";
  78.         return;
  79.     }
  80.  
  81.     ofstream outFile(filename, ios::binary | ios::app);
  82.     if (!outFile) {
  83.         cout << "Error opening file.\n";
  84.         return;
  85.     }
  86.     outFile.write(reinterpret_cast<char*>(&item)sizeof(InventoryItem));
  87.     outFile.close();
  88. }
  89.  
  90. void displayRecord(const string& filename) {
  91.     ifstream inFile(filename, ios::binary);
  92.     if (!inFile) {
  93.         cout << "Error opening file.\n";
  94.         return;
  95.     }
  96.  
  97.     InventoryItem item;
  98.     string searchName;
  99.     cout << "Enter item name or first few letters: ";
  100.     cin.ignore();
  101.     getline(cin, searchName);
  102.  
  103.     bool found = false;
  104.     while (inFile.read(reinterpret_cast<char*>(&item)sizeof(InventoryItem))) {
  105.         if (string(item.name).substr(0, searchName.size()) == searchName) {
  106.             cout << "Item Name: " << item.name << "\n"
  107.                  << "Quantity: " << item.quantity << "\n"
  108.                  << "Wholesale Cost: $" << fixed << setprecision(2) << item.wholesaleCost << "\n"
  109.                  << "Retail Cost: $" << item.retailCost << "\n\n";
  110.             found = true;
  111.             break;
  112.         }
  113.     }
  114.  
  115.     if (!found) {
  116.         cout << "Item not found.\n";
  117.     }
  118.  
  119.     inFile.close();
  120. }
  121.  
  122. void changeRecord(const string& filename) {
  123.     fstream file(filename, ios::in | ios::out | ios::binary);
  124.     if (!file) {
  125.         cout << "Error opening file.\n";
  126.         return;
  127.     }
  128.  
  129.     InventoryItem item;
  130.     string searchName;
  131.     cout << "Enter the name of the item to modify: ";
  132.     cin.ignore();
  133.     getline(cin, searchName);
  134.  
  135.     bool found = false;
  136.     long long pos = 0;
  137.     while (file.read(reinterpret_cast<char*>(&item)sizeof(InventoryItem))) {
  138.         if (string(item.name) == searchName) {
  139.             cout << "Current item details:\n";
  140.             cout << "Item Name: " << item.name << "\n"
  141.                  << "Quantity: " << item.quantity << "\n"
  142.                  << "Wholesale Cost: $" << fixed << setprecision(2) << item.wholesaleCost << "\n"
  143.                  << "Retail Cost: $" << item.retailCost << "\n";
  144.  
  145.             cout << "Enter new details for the item:\n";
  146.             cout << "Enter item name: ";
  147.             cin.getline(item.name50);
  148.             cout << "Enter quantity: ";
  149.             cin >> item.quantity;
  150.             cout << "Enter wholesale cost: ";
  151.             cin >> item.wholesaleCost;
  152.             cout << "Enter retail cost: ";
  153.             cin >> item.retailCost;
  154.  
  155.             if (item.quantity < 0 || item.wholesaleCost < 0 || item.retailCost < 0) {
  156.                 cout << "Invalid input. All values must be >= 0.\n";
  157.                 file.close();
  158.                 return;
  159.             }
  160.  
  161.             file.seekp(pos);
  162.             file.write(reinterpret_cast<char*>(&item)sizeof(InventoryItem));
  163.  
  164.             found = true;
  165.             break;
  166.         }
  167.         pos += sizeof(InventoryItem);
  168.     }
  169.  
  170.     if (!found) {
  171.         cout << "Item not found.\n";
  172.     }
  173.  
  174.     file.close();
  175. }
  176.  
  177. void displayAllRecords(const string& filename) {
  178.     ifstream inFile(filename, ios::binary);
  179.     if (!inFile) {
  180.         cout << "Error opening file.\n";
  181.         return;
  182.     }
  183.  
  184.     InventoryItem item;
  185.     while (inFile.read(reinterpret_cast<char*>(&item)sizeof(InventoryItem))) {
  186.         cout << "Item Name: " << item.name << "\n"
  187.              << "Quantity: " << item.quantity << "\n"
  188.              << "Wholesale Cost: $" << fixed << setprecision(2) << item.wholesaleCost << "\n"
  189.              << "Retail Cost: $" << item.retailCost << "\n\n";
  190.     }
  191.  
  192.     inFile.close();
  193. }
  194.  
  195. void generateReport(const string& filename) {
  196.     ifstream inFile(filename, ios::binary);
  197.     if (!inFile) {
  198.         cout << "Error opening file.\n";
  199.         return;
  200.     }
  201.  
  202.     double totalWholesale = 0.0, totalRetail = 0.0;
  203.     int totalQuantity = 0;
  204.     InventoryItem item;
  205.  
  206.     while (inFile.read(reinterpret_cast<char*>(&item)sizeof(InventoryItem))) {
  207.         totalWholesale += item.wholesaleCost * item.quantity;
  208.         totalRetail += item.retailCost * item.quantity;
  209.         totalQuantity += item.quantity;
  210.     }
  211.  
  212.     cout << "Total Wholesale Value: $" << fixed << setprecision(2) << totalWholesale << "\n";
  213.     cout << "Total Retail Value: $" << totalRetail << "\n";
  214.     cout << "Total Quantity: " << totalQuantity << "\n";
  215.  
  216.     inFile.close();
  217. }

Editor

You can edit this paste and save as new:


File Description
  • f
  • Paste Code
  • 10 Dec-2023
  • 6.29 Kb
You can Share it: