Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include "Hardware.h"
  6. #include <iomanip>
  7. #include <string>
  8.  
  9. using std::ofstream;
  10. using std::istream;
  11. using std::ios;
  12. using std::cerr;
  13. using std::endl;
  14. using std::cout;
  15. using std::cin;
  16. using std::ios;
  17. using std::setw;
  18. using std::string;
  19. using namespace std;
  20. int main()
  21. {
  22.     int input;
  23.     int ID;
  24.     string Name;
  25.     int Amount;
  26.     double Price;
  27.     void outputline(ostream&, const Hardware &);
  28.     Hardware Empty;
  29.     ofstream File("Hardware.dat", ios::binary);
  30.     /*for (int i = 0; i < 100; i++)
  31.     {
  32.         File.write(reinterpret_cast<const char *> (&Empty), sizeof(Hardware));
  33.     }*/
  34.     cout << "If your want to add record press 1\n";
  35.     cout << "If your want to view all records press 2\n";
  36.     cin >> input;
  37.     while (1)
  38.     {
  39.         if (input == 1)
  40.         {
  41.  
  42.  
  43.             while (input == 1)
  44.             {
  45.                 cout << "Please enter record,Name,Amount and Price";
  46.                 cin >> ID;
  47.                 cin >> Name;
  48.                 cin >> Amount;
  49.                 cin >> Price;
  50.                 Empty.setID(ID);
  51.                 Empty.setName(Name);
  52.                 Empty.setAmount(Amount);
  53.                 Empty.setPrice(Price);
  54.                 File.seekp((Empty.getID()-1)*sizeof(Hardware));
  55.                 File.write(reinterpret_cast<const char *> (&Empty), sizeof(Hardware));
  56.                 system("cls");
  57.                 cout << "If your want to add record press 1\n";
  58.                 cout << "If your want to view all records press 2\n";
  59.                 cin >> input;
  60.             }
  61.         }
  62.         //cin >> input;
  63.         if (input == 2)
  64.         {
  65.             ifstream File("Hardware.dat", ios::app);
  66.            
  67.             while (File && !File.eof()) {
  68.                 outputline(cout, Empty);
  69.                 File.read(reinterpret_cast<char *>(&Empty),
  70.                     sizeof(Hardware));
  71.  
  72.             }
  73.             cout << "If your want to add record press 1\n";
  74.             cout << "If your want to view all records press 2\n";
  75.             cin >> input;
  76.  
  77.         }
  78.     }
  79.     system("pause");
  80.     return 0;
  81. }
  82. void outputline(ostream &output, const Hardware &ID)
  83. {
  84.     {
  85.         output << left << setw(10) << ID.getID()
  86.             << setw(16) << ID.getName()
  87.             << setw(11) << ID.getAmount()
  88.             << setw(10) << setprecision(2) << right << fixed
  89.             << showpoint << ID.getPrice() << endl;
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement