Advertisement
Guest User

Untitled

a guest
May 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdlib.h>
  6. #include <iomanip>
  7. #include <conio.h>
  8.  
  9. using namespace std;
  10.  
  11. const int _name = 0;
  12. const int _a = 1;
  13. const int _b = 2;
  14. const int _c = 3;
  15. const int _d = 4;
  16. const int _e = 5;
  17. const int _f = 6;
  18. int answer;
  19.  
  20. class record
  21. {
  22. string name,a,b,c,d,e,f;
  23. public:
  24. string get_var(int id)
  25. {
  26. switch (id)
  27. {
  28. case _name:
  29. return name;
  30. case _a:
  31. return a;
  32. case _b:
  33. return b;
  34. case _c:
  35. return c;
  36. case _d:
  37. return d;
  38. case _e:
  39. return e;
  40. case _f:
  41. return f;
  42. }
  43. }
  44. void set_var(string var, int id)
  45. {
  46. switch (id)
  47. {
  48. case _name:
  49. name=var;
  50. break;
  51. case _a:
  52. a=var;
  53. break;
  54. case _b:
  55. b=var;
  56. break;
  57. case _c:
  58. c=var;
  59. break;
  60. case _d:
  61. d=var;
  62. break;
  63. case _e:
  64. e=var;
  65. break;
  66. case _f:
  67. f=var;
  68. break;
  69. }
  70. }
  71. };
  72. void split_line(record *row, string line)
  73. {
  74. size_t len=0;
  75. for(int i=0;i<7;i++)
  76. {
  77. len=line.find(" ");
  78. row->set_var(line.substr(0,len),i);
  79. line=line.substr(len+1,line.length());
  80. }
  81. }
  82. int main()
  83. {
  84. int x=0;
  85. int n=49;
  86. int input;
  87. while(x!=1)
  88. {
  89. cout << "Welcome to the McDonald's Food Database" << endl;
  90. cout << "Press 1 view database" << endl;
  91. cout << "Press 2 view fields" << endl;
  92. cout << "Press 3 for search" << endl;
  93. cout << "Press 4 for adding" << endl;
  94. cout << "Press 5 to exit"<< endl;
  95. cin >> answer;
  96. system("cls");
  97. vector <record> records;
  98. record row;
  99. ifstream data("McDonalds.txt");
  100. if (data.good())
  101. {
  102. while(!data.eof())
  103. {
  104. string line;
  105. getline(data,line);
  106. split_line(&row,line);
  107. records.push_back(row);
  108. }
  109. if (answer==1)
  110. {
  111. for (int a=0; a<n+1; a++)
  112. {
  113. cout<<setw(32)<<records[a].get_var(_name)<<setw(5)<<records[a].get_var(_a)<<setw(5)<<records[a].get_var(_b)<<setw(5)<<records[a].get_var(_c)<<setw(5)<<records[a].get_var(_d)<<setw(5)<<records[a].get_var(_e)<<setw(5)<<records[a].get_var(_f)<<endl;
  114. }
  115. cout<<"press esc to return to menu"<<endl;
  116. do
  117. {
  118. input = getch();
  119. }while(input !=27);
  120. }
  121. else if (answer==2)
  122. {
  123. int a=0;
  124. cout<<"press enter to edit record"<<endl;
  125. cout<<"press esc to return to menu"<<endl;
  126. do
  127. {
  128.  
  129. input = getch();
  130. if (a<=n)
  131. {
  132. if (input==13)
  133. {
  134. system("cls");
  135. a=a-1;
  136. int alter;
  137. string change;
  138. cout<<"Name: "<<records[a].get_var(_name)<<endl;
  139. cout<<"Calories: "<<records[a].get_var(_a)<<endl;
  140. cout<<"Fat from Calories: "<<records[a].get_var(_b)<<endl;
  141. cout<<"Fat (g): "<<records[a].get_var(_c)<<endl;
  142. cout<<"Saturated Fat: "<<records[a].get_var(_d)<<endl;
  143. cout<<"Trans Fat: "<<records[a].get_var(_e)<<endl;
  144. cout<<"Cholesterol: "<<records[a].get_var(_f)<<endl;
  145. cout<<""<<endl;
  146. cout<<"Please specify what you would like to alter:"<<endl;
  147. cout<<"1. Name"<<endl;
  148. cout<<"2. Calories"<<endl;
  149. cout<<"3. Fat from Calories"<<endl;
  150. cout<<"4. Fat (g)"<<endl;
  151. cout<<"5. Saturated Fat"<<endl;
  152. cout<<"6. Trans Fat"<<endl;
  153. cout<<"7. Cholesterol"<<endl;
  154. cout<<""<<endl;
  155. cin>>alter;
  156. cout<<"specify what you would like to replace with:"<<endl;
  157. cin>>change;
  158. ofstream myfile ("McDonalds.txt");
  159. if (myfile.is_open())
  160. {
  161. if(alter==1)
  162. {
  163. records[a].get_var(_name)=change;
  164. }
  165. else if(alter==2)
  166. {
  167. records[a].get_var(_a)=change;
  168. }
  169. else if(alter==3)
  170. {
  171. records[a].get_var(_b)=change;
  172. }
  173. else if(alter==4)
  174. {
  175. records[a].get_var(_c)=change;
  176. }
  177. else if(alter==5)
  178. {
  179. records[a].get_var(_d)=change;
  180. }
  181. else if(alter==6)
  182. {
  183. records[a].get_var(_e)=change;
  184. }
  185. else if(alter==7)
  186. {
  187. records[a].get_var(_f)=change;
  188. }
  189. myfile.close();
  190. }
  191.  
  192. }
  193. if (input==80)
  194. {
  195. cout<<"Name: "<<records[a].get_var(_name)<<endl;
  196. cout<<"Calories: "<<records[a].get_var(_a)<<endl;
  197. cout<<"Fat from Calories: "<<records[a].get_var(_b)<<endl;
  198. cout<<"Fat (g): "<<records[a].get_var(_c)<<endl;
  199. cout<<"Saturated Fat: "<<records[a].get_var(_d)<<endl;
  200. cout<<"Trans Fat: "<<records[a].get_var(_e)<<endl;
  201. cout<<"Cholesterol: "<<records[a].get_var(_f)<<endl;
  202. cout<<""<<endl;
  203. a=a+1;
  204. if(a>n)
  205. {
  206. a=a-1;
  207. }
  208. }
  209. else if(input==72)
  210. {
  211. cout<<"Name: "<<records[a].get_var(_name)<<endl;
  212. cout<<"Calories: "<<records[a].get_var(_a)<<endl;
  213. cout<<"Fat from Calories: "<<records[a].get_var(_b)<<endl;
  214. cout<<"Fat (g): "<<records[a].get_var(_c)<<endl;
  215. cout<<"Saturated Fat: "<<records[a].get_var(_d)<<endl;
  216. cout<<"Trans Fat: "<<records[a].get_var(_e)<<endl;
  217. cout<<"Cholesterol: "<<records[a].get_var(_f)<<endl;
  218. cout<<""<<endl;
  219. a=a-1;
  220. if(a<0)
  221. {
  222. a=0;
  223. }
  224. }
  225. else if(input!=80 || 72)
  226. {
  227. system("cls");
  228. }
  229. }
  230. }while(input !=27);
  231. }
  232. else if (answer==3)
  233. {
  234. string chorroberru;
  235. int y;
  236. size_t found;
  237. cout<<"search by name (replace spaces with dashes)"<<endl;
  238. cin >> chorroberru;
  239. cout<<setw(32)<<"NAME"<<" CAL "<<" CFAT "<<" FAT "<<"SAT "<<" TRAN "<<"CHOLESTEROL"<<endl;
  240. for(int i=0; i<(int)records.size(); i++)
  241. {
  242. found = records[i].get_var(_name).find(chorroberru);
  243. if (found!=string::npos)
  244. {
  245. cout<<setw(32)<<records[i].get_var(_name)<<setw(5)<<records[i].get_var(_a)<<setw(5)<<records[i].get_var(_b)<<setw(5)<<records[i].get_var(_c)<<setw(5)<<records[i].get_var(_d)<<setw(5)<<records[i].get_var(_e)<<setw(5)<<records[i].get_var(_f)<<endl;
  246. }
  247. }
  248. cout<<"press esc for exit"<<endl;
  249. do
  250. {
  251. input = getch();
  252. cout<<input;
  253. }while(input !=27);
  254. }
  255. else if (answer==4)
  256. {
  257. int edit;
  258. cout<<"Press 1 for Adding a new Record";
  259. cin>>edit;
  260. if(edit=1)
  261. {
  262. string first;
  263. int ca;
  264. int cafat;
  265. int tfat;
  266. int satu;
  267. int trans;
  268. int chole;
  269. cout<<"type what you would like to add"<<endl;
  270. cin>>first;
  271. cin>>ca;
  272. cin>>cafat;
  273. cin>>tfat;
  274. cin>>satu;
  275. cin>>trans;
  276. cin>>chole;
  277. ofstream myfile ("McDonalds.txt", ios::out | ios::app | ios::binary);
  278. if (myfile.is_open())
  279. {
  280. myfile << first<<" "<<ca<<" "<<cafat<<" "<<tfat<<" "<<satu<<" "<<trans<<" "<<chole<<endl;
  281. myfile.close();
  282. n=n+1;
  283. }
  284. else cout << "Unable to open file";
  285. }
  286. cout<<"press esc for exit"<<endl;
  287. do
  288. {
  289. input = getch();
  290. }while(input !=27);
  291. }
  292. else if (answer=5)
  293. {
  294. x=1;
  295. }
  296. system("cls");
  297. }
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement