Advertisement
Guest User

Contact Management by bits

a guest
Aug 18th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<stdio.h>
  4. #include<utility>
  5. #include<string>
  6. #include<vector>
  7.  
  8. using namespace std;
  9. void insert_contact();
  10. void search_contact();
  11. void upORdel(string x);
  12. void update_contact(string outputName, string outputLine, vector<string>output);
  13. void delete_contact(string outputName, string outputLine);
  14. void view_all_contact();
  15.  
  16. char f1[]="file1.txt", f2[]="file2.txt";
  17.  
  18. int main()
  19. {
  20. while(1)
  21. {
  22.  
  23. system("cls");
  24. system("color A1");
  25. cout<<"\t\t\t\t\t:::WELCOME TO CONTACT MANAGEMENT APPLICATION:::"<<endl;
  26. cout<<"\t\t\t\t\t\t\t\t\tCreated by Bits"<<endl<<endl<<endl;
  27. int c;
  28. cout<<":::Choice your option:::"<<endl<<endl<<endl;
  29. cout<<"Type 1 for insert contact."<<endl;
  30. cout<<"Type 2 for search contact."<<endl;
  31. cout<<"Type 3 for view all contact."<<endl;
  32. cin>>c;
  33. switch(c)
  34. {
  35. case 1:
  36. insert_contact();
  37. break;
  38. case 2:
  39. search_contact();
  40. break;
  41. case 3:
  42. view_all_contact();
  43. break;
  44. default:
  45. cout<<"Entered wrong key"<<endl;
  46. break;
  47. }
  48. }
  49. return 0;
  50. }
  51. void insert_contact()
  52. {
  53. system("color D1");
  54.  
  55. ofstream FILE;
  56. FILE.open(f1, ios::app);
  57. cout<<endl<<"Enter User name: ";
  58. string x;
  59. getchar();
  60. getline(cin, x);
  61. FILE<<endl<<"#"<<x;
  62.  
  63. cout<<"Phone number: ";
  64. cin>>x;
  65. FILE<<endl<<x<<"^";
  66.  
  67.  
  68. cout<<"Email address: ";
  69. cin>>x;
  70. FILE<<x<<"^";
  71.  
  72.  
  73. cout<<"Address: ";
  74. cin>>x;
  75. FILE<<x<<"^"<<endl;
  76.  
  77. x.clear();
  78. FILE.close();
  79. cout<<endl<<endl<<"User info inserted"<<endl<<endl;
  80. again:
  81. cout<<endl<<endl<<"For Home page press home."<<endl;
  82. string h;
  83. cin>>h;
  84. if(h=="home"||h=="HOME"||h=="Home")
  85. {
  86. system("cls");
  87. return;
  88. }
  89. else {
  90. cout<<"Wrong key pressed"<<endl;
  91. goto again;
  92. }
  93.  
  94. }
  95. void search_contact()
  96. {
  97. system("color B1");
  98. ifstream FILE;
  99. FILE.open(f1);
  100. cout<<"User Name: ";
  101. string x="#", data, extra;
  102. getchar();
  103. getline(cin, extra);
  104. x.append(extra);
  105. bool flag=false;
  106.  
  107. while(!FILE.eof())
  108. {
  109. getline(FILE, data);
  110. if(x==data)
  111. {
  112. flag=true;
  113. upORdel(x);
  114. break;
  115. }
  116. }
  117. if(!flag)
  118. cout<<"Contact not found"<<endl;
  119. FILE.close();
  120. }
  121. void upORdel(string x)
  122. {
  123.  
  124. ifstream FILE;
  125. FILE.open(f1);
  126. string data, extra, ex, exx;
  127. vector<string>output;
  128. while(!FILE.eof())
  129. {
  130. getline(FILE, data);
  131. if(x==data)
  132. {
  133. extra.assign(x, 1, x.size());
  134. output.push_back(extra);
  135.  
  136. getline(FILE, extra);
  137.  
  138. int s=0, k=0;
  139. for(int i=0;i<extra.size();i++)
  140. {
  141. if(k==0) k++;
  142. else if(k==2) k++;
  143. else if(k==4) k++;
  144.  
  145. if(extra[i]=='^')
  146. {
  147. k++;
  148. output.push_back(ex);
  149. ex.clear();
  150. }
  151. else
  152. {
  153. //cout<<extra[i];
  154. exx=extra[i];
  155. ex.append(exx);
  156.  
  157. }
  158. }
  159. break;
  160. }
  161. }
  162. FILE.close();
  163. int j=0;
  164. cout<<":::Searched result:::"<<endl;
  165. for(auto&i:output)
  166. {
  167. //cout<<"x"<<endl;
  168. if(j==0) cout<<"User Name: ", j++;
  169. else if(j==1) cout<<"Contact Number: ", j++;
  170. else if(j==2) cout<<"Email Number: ", j++;
  171. else if(j==3)cout<<"Address: ", j++;
  172. cout<<i<<endl;
  173. }
  174. string outputName='#'+output[0];
  175. output.erase(output.begin());
  176. string outputLine;
  177. for(auto&i:output)
  178. outputLine.append(i+'^');
  179. up:
  180. cout<<endl<<endl<<":::Choice your option:::"<<endl;
  181. cout<<"Press 1 for update"<<endl;
  182. cout<<"Press 2 for delete the contact"<<endl;
  183. cout<<"Press 3 for going home"<<endl;
  184. cout<<"Choose: ";
  185. int c;
  186. cin>>c;
  187. switch(c)
  188. {
  189. case 1:
  190. update_contact(outputName, outputLine, output);
  191. break;
  192. case 2:
  193. delete_contact(outputName, outputLine);
  194. break;
  195. case 3:
  196.  
  197. system("cls");
  198. return;
  199. break;
  200. default:
  201. {
  202. cout<<"Sorry! you've entered a wrong key."<<endl;
  203. cout<<"Try again."<<endl;
  204. goto up;
  205. }
  206. break;
  207. }
  208.  
  209.  
  210.  
  211. }
  212. void update_contact(string outputName, string outputLine, vector<string>output)
  213. {
  214. vector<string>updatevec(3);
  215. string x, updateName;
  216.  
  217. cout<<":::Update:::"<<endl;
  218. cout<<"For not making change in a specific filed just type null"<<endl;
  219. for(int i=0;i<4;i++)
  220. {
  221. if(i==0)
  222. {
  223. cout<<"New name: ";
  224. getchar();
  225. getline(cin, x);
  226. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  227. updateName='#'+x;
  228. else updateName=outputName;
  229. }
  230. else if(i==1)
  231. {
  232. cout<<"New Contact Number: ";
  233. getline(cin, x);
  234. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  235. updatevec[0]=x+'^';
  236. else updatevec[0]=output[0]+'^';
  237. }
  238. else if(i==2)
  239. {
  240. cout<<"New Email Address: ";
  241. getline(cin, x);
  242. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  243. updatevec[1]=x+'^';
  244. else updatevec[1]=output[1]+'^';
  245. }
  246. else if(i==3)
  247. {
  248. cout<<"New Address: ";
  249. getline(cin, x);
  250. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  251. updatevec[2]=x+'^';
  252. else updatevec[2]=output[2]+'^';
  253. }
  254. }
  255. string updateInfo;
  256. for(auto&i:updatevec)
  257. updateInfo.append(i);
  258.  
  259.  
  260. cout<<"Make change?"<<endl;
  261. cout<<"Type yes/no"<<endl;
  262. cout<<"Choose: ";
  263. getline(cin, x);
  264. //cout<<"Change able Name: "<<updateName<<endl;
  265. //cout<<"Change able info: "<<updateInfo<<endl;
  266. if(x.compare("YES")==0||x.compare("yes")==0||x.compare("Yes")==0)
  267. {
  268.  
  269. fstream FILE(f1);
  270. ofstream FILEx(f2);
  271. //temp.open(f2, ios::out);
  272. string line;
  273. while(!FILE.eof())
  274. {
  275.  
  276. if(line==outputName)
  277. {
  278. FILEx<<updateName<<endl;
  279. getline(FILE, line);
  280. FILEx<<updateInfo<<endl;
  281. }
  282. else FILEx<<line<<endl;
  283. getline(FILE, line);
  284. }
  285.  
  286. FILE.close();
  287.  
  288.  
  289.  
  290. ofstream file(f1, ios::trunc);
  291. ifstream reRight(f2);
  292. string extra;
  293. while(!reRight.eof())
  294. {
  295. file<<extra<<endl;
  296. getline(reRight, extra);
  297. }
  298. reRight.close();
  299. FILEx.close();
  300. ofstream file2(f2, ios::trunc);
  301. file2.close();
  302.  
  303. }
  304.  
  305. }
  306. void delete_contact(string outputName, string outputLine)
  307. {
  308. cout<<"Are you sure to delete this contact?"<<endl;
  309. cout<<"Type yes/no."<<endl;
  310. cout<<"Choose: ";
  311. string x;
  312. cin>>x;
  313. if(x=="yes")
  314. {
  315. fstream FILE(f1);
  316. ofstream FILEx(f2);
  317.  
  318. string line;
  319. while(!FILE.eof())
  320. {
  321.  
  322. if(line==outputName)
  323. {
  324. FILEx<<""<<endl;
  325. getline(FILE, line);
  326. FILEx<<""<<endl;
  327. }
  328. else FILEx<<line<<endl;
  329. getline(FILE, line);
  330. }
  331.  
  332. FILE.close();
  333.  
  334.  
  335.  
  336. ofstream file(f1, ios::trunc);
  337. ifstream reRight(f2);
  338. string extra;
  339. while(!reRight.eof())
  340. {
  341. file<<extra<<endl;
  342. getline(reRight, extra);
  343. }
  344. reRight.close();
  345. FILEx.close();
  346. ofstream file2(f2, ios::trunc);
  347. file2.close();
  348. }
  349. }
  350. void view_all_contact()
  351. {
  352. system("color E1");
  353. vector<pair<string, vector<string> > >OutputData;
  354. ifstream FILE(f1);
  355. string str, x;
  356. vector<string>vec;
  357. string ex1, ex2;
  358.  
  359. while(!FILE.eof())
  360. {
  361. if(str[0]=='#')
  362. {
  363.  
  364. ex1.assign(str, 1, str.size());
  365. getline(FILE, str);
  366. for(int i=0;i<str.size();i++)
  367. {
  368. if(str[i]=='^') vec.push_back(x), x.clear();
  369. else x+=str[i];
  370. }
  371.  
  372. OutputData.push_back(make_pair(ex1, vec));
  373. ex1.clear();
  374.  
  375. vec.clear();
  376. }
  377. getline(FILE, str);
  378. }
  379. FILE.close();
  380. //sorting
  381. string extra;
  382. vector<string>xvec;
  383.  
  384. for(int i=1;i<OutputData.size();i++)
  385. {
  386. for(int j=1;j<OutputData.size();j++)
  387. {
  388. if((OutputData[j-1].first).compare(OutputData[j].first)>0)
  389. {
  390. extra=OutputData[j-1].first;
  391. xvec=OutputData[j-1].second;
  392.  
  393. OutputData[j-1]=OutputData[j];
  394.  
  395. OutputData[j].first=extra;
  396. OutputData[j].second=xvec;
  397.  
  398. }
  399. }
  400. }
  401. system("cls");
  402. cout<<endl<<endl<<":::All User Info:::"<<endl;
  403. int user=0;
  404. for(auto&i:OutputData)
  405. {
  406. cout<<endl<<user+1<<". User Name: "<<i.first<<endl, user++;
  407. vec=i.second;
  408. int x=0;
  409. for(auto&j:vec)
  410. {
  411. if(x==0) cout<<"Contact Number: ", x++;
  412. else if(x==1) cout<<"Email Address: ", x++;
  413. else if(x==2) cout<<"Address: ", x++;
  414. cout<<j<<endl;
  415. }
  416. cout<<endl;
  417. }
  418. again:
  419. cout<<endl<<endl<<"For Home page press home."<<endl;
  420. string h;
  421. cin>>h;
  422. if(h=="home"||h=="HOME"||h=="Home")
  423. {
  424. system("cls");
  425. return;
  426. }
  427. else {
  428. cout<<"Wrong key pressed"<<endl;
  429. goto again;
  430. }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement