Guest User

Untitled

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7.  
  8. struct inventorydata
  9. {
  10. char description[15];
  11. int quantityonhand;
  12. float wholesalecost;
  13. float retailcost;
  14. char dateadded[15];
  15. };
  16.  
  17. int main()
  18. {
  19.     inventorydata record;
  20.     char menu='Y';
  21.     int number, recordnum=0, whichrecord, count=0;
  22.    
  23.     fstream inventory("inventorydata.dat", ios::in | ios::out | ios::binary | ios::beg);
  24.     if (!inventory.good()) return -10000;
  25.    
  26.     while(menu == 'y'||menu == 'Y')
  27.     {
  28.         cout << "chose an option by typing the correct number: " << endl;
  29.         cout << "add record: 1" << endl;
  30.         cout << "display a record: 2" << endl;
  31.         cout << "change record: 3" << endl;
  32.         cin >> number;
  33.         cout << endl;
  34.  
  35.         if (number == 1)
  36.         {
  37.             count+=1;
  38.  
  39.             inventorydata record = {"",0,0.0,0.0,""};
  40.  
  41.             cout << "Enter data\n";
  42.             cout << "Description: \n";
  43.             cin.ignore();
  44.             cin.getline(record.description, 15);
  45.             cout << "Quantity on hand: \n";
  46.             cin >> record.quantityonhand;
  47.                 while (record.quantityonhand < 0){
  48.                 cout <<"Error cannot be negative\n";
  49.                 cin >> record.quantityonhand;}
  50.             cout << "Wholesale cost: \n";
  51.             cin >> record.wholesalecost;
  52.                 while (record.wholesalecost < 0){
  53.                 cout <<"Error cannot be negative\n";
  54.                 cin >> record.wholesalecost;}
  55.             cout << "Retail cost: \n";
  56.             cin >> record.retailcost;
  57.                 while (record.retailcost < 0){
  58.                 cout <<"Error cannot be negative\n";
  59.                 cin >> record.retailcost;}
  60.             cout << "Date added: \n";
  61.             cin >> record.dateadded;
  62.                 while (record.dateadded < 0){
  63.                 cout <<"Error a date cannot be negative.. \n";
  64.                 cin >> record.dateadded;}
  65.  
  66.             inventory.seekp(recordnum * sizeof(record), ios::beg);
  67.             inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
  68.            
  69.             recordnum += 1;
  70.  
  71.             cout << "do something else? y for yes.\n";
  72.             cin >> menu;
  73.         }
  74.         else if(number == 2)
  75.         {
  76.             cout << "which record would you like to display?\n";
  77.             cin >> whichrecord;
  78.             while (whichrecord > count)
  79.             {
  80.                 cout << "error: record not added\n";
  81.                 cin >> whichrecord;
  82.             }
  83.             whichrecord -=1;
  84.  
  85.             /*fstream inventory("inventorydata.dat", ios::in | ios::out | ios::binary);
  86.             if (!inventory.good()) return -10000;*/
  87.             inventory.seekg(whichrecord * sizeof(record), ios::beg);
  88.             inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
  89.  
  90.             cout << "Description: ";
  91.             cout << record.description << "\n";
  92.             cout << "Quantity on hand: ";
  93.             cout << record.quantityonhand << "\n";
  94.             cout << "Wholesale cost: ";
  95.             cout << record.wholesalecost << "\n";
  96.             cout << "Retail cost: ";
  97.             cout << record.retailcost << "\n";
  98.             cout << "Date added: ";
  99.             cout << record.dateadded << "\n";
  100.             //inventory.close();
  101.  
  102.             cout << "do something else? y for yes.\n";
  103.             cin >> menu;
  104.         }
  105.         else if(number == 3)
  106.         {
  107.            
  108.             cout << "which record would you like to edit?\n";
  109.             cin >> whichrecord;
  110.             while (whichrecord > count)
  111.             {
  112.                 cout << "error: record not added\n";
  113.                 cin >> whichrecord;
  114.             }
  115.             whichrecord -=1;
  116.  
  117.             /*fstream inventory("inventorydata.dat", ios::in | ios::out | ios::binary);
  118.             if (!inventory.good()) return -10000;*/
  119.             inventory.seekg(whichrecord * sizeof(record), ios::beg);
  120.             inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
  121.  
  122.             cout << "Enter data\n";
  123.             cout << "Description: \n";
  124.             cin.ignore();
  125.             cin.getline(record.description, 15);
  126.             cout << "Quantity on hand: \n";
  127.             cin >> record.quantityonhand;
  128.             while (record.quantityonhand < 0){
  129.             cout <<"Error cannot be negative\n";
  130.             cin >> record.quantityonhand;}
  131.             cout << "Wholesale cost: \n";
  132.             cin >> record.wholesalecost;
  133.             while (record.wholesalecost < 0){
  134.             cout <<"Error cannot be negative\n";
  135.             cin >> record.wholesalecost;}
  136.             cout << "Retail cost: \n";
  137.             cin >> record.retailcost;
  138.             while (record.retailcost < 0){
  139.             cout <<"Error cannot be negative\n";
  140.             cin >> record.retailcost;}
  141.             cout << "Date added: \n";
  142.             cin >> record.dateadded;
  143.             while (record.dateadded < 0){
  144.             cout <<"Error a date cannot be negative.. \n";
  145.             cin >> record.dateadded;}
  146.  
  147.             inventory.seekp(whichrecord * sizeof(record), ios::beg);
  148.             inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
  149.  
  150.  
  151.             cout << "do something else? y for yes.\n";
  152.             cin >> menu;
  153.  
  154.         }
  155.     }
  156.     inventory.close();
  157.     return 0;
  158. }
Add Comment
Please, Sign In to add comment