Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. // Karl Ramberg
  2. // 2-17-19
  3. // CS181 - Assignment 1 - Q4
  4.  
  5. #include<iostream>
  6. #include<fstream>
  7.  
  8. using namespace std;
  9.  
  10. struct Division
  11. {
  12. string name;
  13. int id;
  14. double sales;
  15. int year;
  16. };
  17.  
  18. void openFile(string &filename, fstream &file);
  19.  
  20. void readFile(string filename, fstream &file);
  21.  
  22. void writeFile(string filename, fstream &file);
  23.  
  24. void modifyFile(string filename, fstream &file);
  25.  
  26. void getInfo(Division &div);
  27.  
  28. int main()
  29. {
  30. fstream file;
  31. string filename;
  32. openFile(filename, file);
  33.  
  34. int choice;
  35. cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
  36. cin >> choice;
  37.  
  38. while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
  39. {
  40. cout << "Not a valid operation" << endl;
  41. cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
  42. cin >> choice;
  43. }
  44. while(choice != 4)
  45. {
  46. if(choice == 1)
  47. {
  48. readFile(filename, file);
  49. }
  50. else if(choice == 2)
  51. {
  52. writeFile(filename, file);
  53. }
  54. else
  55. {
  56. modifyFile(filename, file);
  57. }
  58.  
  59. cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
  60. cin >> choice;
  61. while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
  62. {
  63. cout << "Not a valid operation" << endl;
  64. cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
  65. cin >> choice;
  66. }
  67. }
  68.  
  69. file.close();
  70. }
  71.  
  72. void openFile(string &filename, fstream &file)
  73. {
  74. cout << "Choose a file: ";
  75. cin >> filename;
  76.  
  77. file.open(filename, ios::in | ios::out | ios::binary);
  78. while(file.fail())
  79. {
  80. cout << "Cannot read that file" << endl;
  81. cout << "Choose a file: ";
  82. cin >> filename;
  83. file.open(filename, ios::in | ios::out | ios::binary);
  84. }
  85. }
  86.  
  87. void readFile(string filename, fstream &file)
  88. {
  89. cout << "Reading " << filename << "..." << endl;
  90.  
  91. file.seekg(0);
  92. file.clear();
  93. Division div;
  94. while(!file.eof())
  95. {
  96. file.read(reinterpret_cast<char *>(&div), sizeof(div));
  97.  
  98. cout << endl;
  99. cout << "Name: " << div.name << endl;
  100. cout << "ID: " << div.id << endl;
  101. cout << "Sales: $" << div.sales << endl;
  102. cout << "Year: " << div.year << endl;
  103. cout << endl;
  104. }
  105. }
  106.  
  107. void writeFile(string filename, fstream &file)
  108. {
  109. cout << "Writing to " << filename << "..." << endl;
  110.  
  111. Division div;
  112. getInfo(div);
  113.  
  114. file.seekp(ios::end);
  115. file.write(reinterpret_cast<char *>(&div), sizeof(div));
  116.  
  117. cout << endl;
  118. }
  119.  
  120. void modifyFile(string filename, fstream &file)
  121. {
  122. int divNum;
  123. cout << "Which record would you like to modify? ";
  124. cin >> divNum;
  125.  
  126. Division div;
  127. getInfo(div);
  128.  
  129. file.seekp(divNum * sizeof div);
  130. file.write(reinterpret_cast<char *>(&div), sizeof div);
  131. }
  132.  
  133. void getInfo(Division &div)
  134. {
  135. string dummy;
  136. getline(cin, dummy);
  137.  
  138. string name;
  139. cout << "Enter a name: ";
  140. getline(cin, name);
  141. while(name == "")
  142. {
  143. cout << "Invalid name" << endl;
  144. cout << "Enter a name: ";
  145. getline(cin, name);
  146. }
  147.  
  148. int id;
  149. cout << "Enter an ID: ";
  150. cin >> id;
  151. while(id < 0)
  152. {
  153. cout << "ID cannot be negative" << endl;
  154. cout << "Enter an ID: ";
  155. cin >> id;
  156. }
  157.  
  158. double sales;
  159. cout << "Enter total sales: ";
  160. cin >> sales;
  161. while(sales < 0.0)
  162. {
  163. cout << "Sales cannot be negative" << endl;
  164. cout << "Enter total sales: ";
  165. cin >> sales;
  166. }
  167.  
  168. int year;
  169. cout << "Enter a year: ";
  170. cin >> year;
  171. while(year < 0)
  172. {
  173. cout << "Year cannot be negative" << endl;
  174. cout << "Enter a year: ";
  175. cin >> year;
  176. }
  177.  
  178. div.name = name;
  179. div.id = id;
  180. div.sales = sales;
  181. div.year = year;
  182. cout << div.name << " " << div.id << " " << div.sales << " " << div.year << endl;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement