Advertisement
Fattyrivers

Untitled

May 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. const int DES = 20;
  8.  
  9. //declare variables
  10. void Add();
  11. void Display();
  12. void Edit();
  13. void Exit();
  14.  
  15. struct Inventory
  16. {
  17. char item[DES];
  18. int qty;
  19. int wcost;
  20. int rcost;
  21. char date[10];
  22. };
  23.  
  24. //Main function
  25. int main()
  26. {
  27. int choice;
  28. do
  29. {
  30. cout << "1. Add a new record: " << endl;
  31. cout << "2. Display a previous record: " << endl;
  32. cout << "3. Edit an old record:" << endl;
  33. cout << "4. Exit the program:" << endl;
  34.  
  35. cin >> choice;
  36.  
  37. switch (choice)
  38. {
  39. case 1:
  40. Add();
  41. break;
  42. case 2:
  43. Display();
  44. break;
  45. case 3:
  46. Edit();
  47. break;
  48. case 4:
  49. Exit();
  50. break;
  51.  
  52.  
  53. default: cout << "Invalid Selection" << endl;
  54. }
  55. } while
  56. (choice <= 2);
  57.  
  58. system("PAUSE");
  59. return 0;
  60. }
  61.  
  62.  
  63. //Add funtion
  64. void Add()
  65. {
  66. fstream fout;
  67. const int size = 3;
  68. char ch;
  69. int i = 0;
  70. fout.open("Records.txt", ios::out | ios::ate);
  71. Inventory inv;
  72.  
  73. //get data
  74. do
  75. {
  76. cout << "Enter item description: " << endl;
  77. cin.ignore();
  78. cin.getline(inv.item, DES);
  79. cout << "Enter quantity: " << endl;
  80. cin >> inv.qty;
  81. cout << "Enter wholesale cost: " << endl;
  82. cin >> inv.wcost;
  83. cout << "Enter retail cost: " << endl;
  84. cin >> inv.rcost;
  85. cout << "Enter date: " << endl;
  86. cin.ignore();
  87. cin.getline(inv.date, 10);
  88.  
  89. //write record to file
  90. fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
  91. cout << "Do you want to add another record? " << endl;
  92. cin >> ch;
  93. } while
  94. (ch == 'Y' && 1 < 3);
  95.  
  96. //close the file
  97. fout.close();
  98. }
  99.  
  100. //"Display" function
  101. void Display()
  102. {
  103. fstream fout;
  104. fout.open("Records.txt", ios::in);
  105. Inventory inv;
  106. fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
  107. while (!fout.eof())
  108.  
  109. {
  110.  
  111. cout << "\nDescription\t: ";
  112. cout << inv.item;
  113. cout << "\nQuantity\t: ";
  114. cout << inv.qty;
  115. cout << "\nWholesale Cost\t: ";
  116. cout << inv.wcost;
  117. cout << "\nRetail Cost\t: ";
  118. cout << inv.rcost;
  119. cout << "\nDate\t: ";
  120. cout << inv.date;
  121. fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
  122. }
  123. //close the file
  124. fout.close();
  125. void edit
  126. {
  127. fstream fout;
  128. fout.open("Records.txt", ios::in);
  129. Inventory inv;
  130. fout.open("Records.txt", ios::out | ios::ate);
  131. Inventory inv;
  132.  
  133. //get data
  134. do
  135. {
  136. cout << "Enter item description: " << endl;
  137. cin.ignore();
  138. cin.getline(inv.item, DES);
  139. cout << "Enter quantity: " << endl;
  140. cin >> inv.qty;
  141. cout << "Enter wholesale cost: " << endl;
  142. cin >> inv.wcost;
  143. cout << "Enter retail cost: " << endl;
  144. cin >> inv.rcost;
  145. cout << "Enter date: " << endl;
  146. cin.ignore();
  147. cin.getline(inv.date, 10);
  148.  
  149. //write record to file
  150. fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
  151. cout << "Do you want to add another record? " << endl;
  152. cin >> ch;
  153. } while
  154. (ch == 'Y' && 1 < 3);
  155.  
  156. //close the file
  157. fout.close();
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement