Advertisement
Jobyyes

Untitled

Feb 16th, 2020
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. struct kitten
  8. {
  9. int score;
  10. string name;
  11. string color;
  12. };
  13.  
  14. struct roster
  15. {
  16. kitten kittens[10];
  17. int size = 0;
  18. };
  19.  
  20. string printMenu()
  21. {
  22. int loop = 1;
  23. string choice;
  24. string valid[8] = {"a", "d", "u", "f", "l", "s", "o", "q"};
  25. while (loop == 1)
  26. {
  27. cout << "MENU" << endl;
  28. cout << "a - Add kitten" << endl;
  29. cout << "d - Delete kitten" << endl;
  30. cout << "u - Update kitten color and cuteness score" << endl;
  31. cout << "f - Find kitten" << endl;
  32. cout << "l - Load kitten info from file" << endl;
  33. cout << "s - Save kitten info to file" << endl;
  34. cout << "o - Output roster" << endl;
  35. cout << "q - Quit" << endl;
  36. cout << endl;
  37. cout << "Choose an option:" << endl;
  38. cin >> choice;
  39. int validity = 0;
  40. for (int i = 0; i < 8; i++)
  41. {
  42. if (choice == valid[i])
  43. {
  44. validity += 1;
  45. }
  46. }
  47. if (validity >= 1)
  48. {
  49. if (choice == "q")
  50. {
  51. break;
  52. }
  53. else
  54. {
  55. return choice;
  56. }
  57. }
  58. else
  59. {
  60. cout << "Invalid option. Please try again." << endl;
  61. }
  62. }
  63.  
  64. }
  65.  
  66. int findKitten(string name, roster& q)
  67. {
  68. int nameinroster;
  69. int position = 0;
  70. for (int i = 0; i < 10; i++)
  71. {
  72. if (name == q.kittens[i].name)
  73. {
  74. return i;
  75. }
  76.  
  77. }
  78. return -1;
  79. }
  80.  
  81. void addKitten(kitten x, roster& q)
  82. {
  83. if (q.size == 10)
  84. {
  85. cout << "Impossible to add new kitten: roster is full." << endl;
  86. }
  87. else
  88. {
  89. q.kittens[q.size] = x;
  90. cout << "Successfully added new kitten to roster." << endl;
  91. q.size += 1;
  92. }
  93. }
  94.  
  95. bool deleteKitten(string name, roster& q)
  96. {
  97. int exists = findKitten(name, q);
  98. if (exists == -1)
  99. {
  100. return false;
  101. }
  102. else
  103. {
  104. for (int i = exists; i < q.size; i++)
  105. {
  106. q.kittens[i] = q.kittens[i+1];
  107. }
  108. q.size -= 1;
  109. return true;
  110. }
  111. }
  112.  
  113. void getKittenFromFile(string filename, roster& q)
  114. {
  115. string temp = "";
  116. ifstream file;
  117. file.open(filename);
  118. if (file.is_open())
  119. {
  120. while(true)
  121. {
  122. if (q.size >= 10)
  123. {
  124. cout << "Impossible to add new kitten: roster is full." << endl;
  125. cout << endl;
  126. return;
  127. }
  128. if (file.eof()) break;
  129. getline(file, q.kittens[q.size].name);
  130. if (file.eof()) break;
  131. getline(file, q.kittens[q.size].color);
  132. if (file.eof()) break;
  133. getline(file, temp);
  134. q.kittens[q.size].score = stoi(temp);
  135. q.size += 1;
  136. }
  137. }
  138. else
  139. {
  140. cout << "Error! File not found." << endl;
  141. }
  142. }
  143.  
  144.  
  145. bool updateKitten(kitten x, roster& q)
  146. {
  147. int exists = findKitten(x.name, q);
  148. if (exists == -1)
  149. {
  150. return false;
  151. }
  152. else
  153. {
  154. q.kittens[exists].color = x.color;
  155. q.kittens[exists].score = x.score;
  156. return true;
  157. }
  158. }
  159.  
  160. void printToFile(string filename, roster q)
  161. {
  162. ofstream file;
  163. file.open(filename);
  164. file << "ROSTER:" << endl;
  165. for (int i = 0; i < q.size; i++)
  166. {
  167. int number = i+1;
  168. file << "Kitten " << number << " -- Name: " << q.kittens[i].name << ", Color: " << q.kittens[i].color << ", Score: " << q.kittens[i].score << endl;
  169. }
  170. }
  171.  
  172. void printRoster(roster q)
  173. {
  174. cout << "ROSTER:" << endl;
  175. for (int i = 0; i < q.size; i++)
  176. {
  177. int number = i+1;
  178. cout << "Kitten " << number << " -- Name: " << q.kittens[i].name << ", Color: " << q.kittens[i].color << ", Score: " << q.kittens[i].score << endl;
  179. }
  180. cout << endl;
  181. }
  182. int main()
  183. {
  184. roster kittenRoster;
  185. kittenRoster.size = 0;
  186. int keep = 1;
  187. while (keep == 1)
  188. {
  189. string decision = printMenu();
  190. if (decision == "a")
  191. {
  192. kitten adding;
  193. cout << "Enter a new kitten's name:" << endl;
  194. cin >> adding.name;
  195. cout << "Enter the kittens's color:" << endl;
  196. cin >> adding.color;
  197. cout << "Enter the kittens's cuteness score:" << endl;
  198. cin >> adding.score;
  199. addKitten(adding, kittenRoster);
  200. cout << endl;
  201. }
  202. if (decision == "d")
  203. {
  204. if (kittenRoster.size == 0)
  205. {
  206. cout << "Cannot delete from empty roster." << endl;
  207. }
  208. else
  209. {
  210. kitten removing;
  211. cout << "Enter kitten name to delete:" << endl;
  212. cin >> removing.name;
  213. bool tried = deleteKitten(removing.name, kittenRoster);
  214. if (tried == false)
  215. {
  216. cout << "Error! Kitten not found." << endl;
  217. }
  218. else
  219. {
  220. kittenRoster.size -= 1;
  221. }
  222. }
  223. }
  224. if (decision == "u")
  225. {
  226. kitten update;
  227. cout << "Enter a kitten name:" << endl;
  228. cin >> update.name;
  229. cout << "Enter a new color for the kitten:" << endl;
  230. cin >> update.color;
  231. cout << "Enter a new cuteness score for the kitten:" << endl;
  232. cin >> update.score;
  233. bool attempt = updateKitten(update, kittenRoster);
  234. if (attempt == true)
  235. {
  236. cout << "Successfully updated kitten info." << endl;
  237. }
  238. if (attempt == false)
  239. {
  240. cout << "Cannot find kitten." << endl;
  241. }
  242. }
  243. if (decision == "f")
  244. {
  245. kitten find;
  246. cout << "Enter a kitten name: " << endl;
  247. cin >> find.name;
  248. int found = findKitten(find.name, kittenRoster);
  249. if (found == -1)
  250. {
  251. cout << "Kitten not found." << endl;
  252. }
  253. else
  254. {
  255. cout << find.name << " info: " << kittenRoster.kittens[found].color << ", " << kittenRoster.kittens[found].score << endl;
  256. }
  257. cout << endl;
  258. }
  259. if (decision == "l")
  260. {
  261. string namefile;
  262. cout << "Enter file name:" << endl;
  263. cin >> namefile;
  264. getKittenFromFile(namefile, kittenRoster);
  265. }
  266. if (decision == "s")
  267. {
  268. string namefile;
  269. cout << "Enter file name:" << endl;
  270. cin >> namefile;
  271. printToFile(namefile, kittenRoster);
  272. }
  273. if (decision == "o")
  274. {
  275. printRoster(kittenRoster);
  276. }
  277. if (decision == "q")
  278. {
  279. break;
  280. }
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement