View Single Post
Join Date: May 2008
Posts: 13
Reputation: nouryn is an unknown quantity at this point 
Solved Threads: 0
nouryn nouryn is offline Offline
Newbie Poster

Re: help in phonebook

 
0
  #6
May 7th, 2008
i've edited the code and made a function for sorting but now i need the modifiy function how can i modify a contact that's my code hope u can help

  1. #include<iostream>
  2.  
  3. #include <cstring>
  4. #include<string>
  5. #include <iomanip>
  6. #include <map>
  7. #include<fstream>
  8. #include<algorithm>
  9. #include <vector>
  10. //#include <stdlio>
  11. using namespace std;
  12. fstream phonebook;
  13.  
  14.  
  15. struct phone
  16. {
  17. char word[50];
  18. char fword[50];
  19. fstream edit;
  20. int id;
  21. string fname;
  22. string fname2;
  23. string sname;
  24. string lname;
  25. int phonenumber;
  26. int mobilenumber;
  27. string email;
  28. };
  29.  
  30. phone info;
  31.  
  32. char d,f,r;
  33. string c,g;
  34. void sortingtany2()
  35. {
  36. vector<string> sV;
  37. phonebook.open("c:\\c\\phonebook.txt");
  38. string word;
  39.  
  40. // separates words in file at a whitespace
  41. // and loads a string vector
  42. while(getline(phonebook,word))
  43. sV.push_back(word);
  44.  
  45. cout << "Unsorted words:" << endl;
  46. for(int i = 0; i < sV.size(); i++)
  47. cout << sV[i] << endl;
  48.  
  49. cout << "---------------" << endl;
  50.  
  51. cout << "Sorted words:" << endl;
  52. sort( sV.begin(), sV.end() );
  53. for(int i = 0; i < sV.size(); i++)
  54. {
  55. cout << sV[i] << endl;
  56. phonebook<<sV[i];
  57. }
  58. }
  59. void edit()
  60. {
  61.  
  62. string c;
  63. char c1;
  64. cout << "Please type some lines of text. Enter a period to finish:\n";
  65. do {
  66. c=cin.get();
  67. phonebook>>info.fname;
  68. if(c==info.fname)
  69. {
  70. info.fname.clear();
  71. }
  72.  
  73.  
  74. } while (c1!='.');
  75.  
  76. }
  77.  
  78. void add()
  79. {
  80. phonebook.open("c:\\c\\phonebook.txt",ios::out|ios::app);
  81. cout<<"enter the first name\n";
  82. cin>>info.fname;
  83. cout<<"enter the second name\n";
  84. cin>>info.sname;
  85. cout<<"enter the last name\n";
  86. cin>>info.lname;
  87. cout<<"enter the phone number\n";
  88. cin>>info.phonenumber;
  89. cout<<"enter the moile number\n";
  90. cin>>info.mobilenumber;
  91. cout<<"enter the e-mail\n";
  92. cin>>info.email;
  93.  
  94. phonebook<<info.fname<<"\t "<<info.sname<<"\t "<<info.lname<<" \t "
  95. <<info.phonenumber<<" \t "<<info.mobilenumber<<" \t "<<info.email<<endl;;
  96. cout<<endl;
  97.  
  98.  
  99.  
  100. phonebook.close();
  101.  
  102.  
  103. }
  104.  
  105. void getdisplay()
  106. {
  107.  
  108. cout<<"if u wanna display all contacts press 'a'\n"<<"if u wanna display contacts begining with specific letter press 'c'\n";
  109. cin>>r;
  110. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  111. phonebook.clear();
  112. switch(r)
  113. {
  114.  
  115. case 'a':
  116. {
  117. while(!phonebook.eof())
  118. {
  119. getline(phonebook,c);
  120. cout<<c<<endl;
  121. }
  122. phonebook.close();
  123. break;
  124. }
  125. case 'c':
  126. {
  127.  
  128. char le;
  129. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  130. cout<<"enter the wanted letter";
  131. cin>>le;
  132. phonebook.seekg(0,ios::beg);
  133. while(getline(phonebook ,c))
  134. {
  135. cout<<"zashgdas";
  136.  
  137. for(int i=0;i<c.length();i++)
  138. {
  139. cout<<"afa";
  140. if(c[0]>=le)
  141. {
  142. cout<<c;
  143. cout<<"hasgdajk";
  144. break;
  145. }
  146. }
  147. }
  148. phonebook.close();
  149. break;
  150. }
  151. }
  152.  
  153. }
  154. void getsearch()
  155. {
  156. cout << "Enter your contact:"<<endl;
  157.  
  158. cin >> info.word;
  159. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  160. phonebook.clear();
  161. int counter =0;
  162. while (!phonebook.eof())
  163. {
  164. phonebook >> info.fword;
  165. int j=strcmpi(info.fword,info.word);
  166. if (j==0)
  167. {
  168. cout << "contact was found in the file :)"<<endl;
  169. cout<< info.word << endl;
  170. counter ++;
  171. break;
  172. }
  173. }
  174. if (counter == 0)
  175. cout << "contact was not found in the file :(" << endl;
  176.  
  177. phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
  178.  
  179.  
  180. cout<<info.fword<<" "<<info.fname<<" "<<info.sname<<" "<<info.lname
  181. <<" "<<info.phonenumber<<" "<<info.mobilenumber<<" "<<info.email<<endl;
  182. phonebook.close();
  183.  
  184.  
  185. }
  186.  
  187.  
  188.  
  189. void getmodify()
  190. {
  191.  
  192. cout << "Enter your letter:"<<endl;
  193. cin >> info.word;
  194. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  195. int counter =0;
  196. while (!phonebook.eof()){
  197. phonebook >> info.fword;
  198. int j=strcmpi(info.fword,info.word);
  199. if (j==0){
  200. cout << "contact was found in the file :)"<<endl;
  201. cout<< info.word << endl;
  202. counter ++;
  203. info.word==NULL;
  204.  
  205.  
  206. break;
  207. }
  208.  
  209.  
  210.  
  211. }
  212. if (counter == 0)
  213. cout << "contact was not found in the file :(" << endl;
  214. phonebook.close();
  215.  
  216.  
  217.  
  218. }
  219. int getdel()
  220. {
  221. char hot;
  222. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  223. ofstream myfile("c:\\c\\1.txt",ios::out);
  224. cout<<"ur u sure u want to delete all contact(y/n)"<<endl;
  225. cin>>hot;
  226. if (hot=='y')
  227. {
  228. phonebook>>info.fname;
  229. while( getline(phonebook,info.fname))
  230. {
  231. cout << info.fword<< "\n";
  232. }
  233. phonebook.close();
  234. myfile.close();
  235. // delete the original file
  236. remove("c:\\c\\phonebook.txt");
  237. // rename old to new
  238. rename("c:\\c\\1.txt","c:\\c\\phonebook.txt");
  239. }
  240. else
  241. {
  242. cout<<"the contact still as it is"<<endl;
  243. }
  244.  
  245. // all done!
  246. return 0;
  247. }
  248. void del()
  249. {
  250. string del;
  251. cout << "Enter your contact:"<<endl;
  252.  
  253. cin >> info.word;
  254. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  255. phonebook.clear();
  256. int counter =0;
  257. while (!phonebook.eof())
  258. {
  259. phonebook >> info.fword;
  260. int j=strcmpi(info.fword,info.word);
  261. if (j==0)
  262. {
  263. cout << "contact was found in the file :)"<<endl;
  264. cout<< info.word << endl;
  265. counter ++;
  266. break;
  267. }
  268. }
  269. if (counter == 0)
  270. cout << "contact was not found in the file :(" << endl;
  271.  
  272. phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
  273. info.fname.clear();
  274. info.fname=' ';
  275.  
  276.  
  277.  
  278. phonebook<<info.fname;
  279.  
  280.  
  281.  
  282. phonebook.close();
  283.  
  284.  
  285. }
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293. int main()
  294. {
  295.  
  296.  
  297. while(true)
  298. {
  299.  
  300.  
  301. cout<<endl<<"1-Adding new contacts"<<endl
  302. <<"2-Displaying contacts information"<<endl
  303. <<"3-Searching for a contact"<<endl
  304. <<"4-Deleting a contact"<<endl
  305. <<"5-Modifying a contact information"<<endl
  306. <<"6-Sort the contacts by name"<<endl
  307. <<"7-exit"<<endl<<endl;
  308. cout<<"enter your choice"<<endl;
  309. cin>>d;
  310. switch(d)
  311.  
  312. {
  313. case '1':
  314.  
  315. {
  316.  
  317. add();
  318. cout<<"if u wann add another contact press 'y' if not press 'n'";
  319. cin>>f;
  320. if(f=='y')
  321.  
  322. {
  323. add();
  324. }
  325.  
  326. break;
  327. }
  328. case '2':
  329. {
  330. getdisplay();
  331. break;
  332. }
  333. case '3':
  334. {
  335. getsearch();
  336.  
  337. break;
  338.  
  339. }
  340. case '4':
  341. {
  342. int e;
  343. cout<<"enter '1'if u want to delete all contact"<<endl
  344. <<"enter '2' if u want to delete specific contact"<<endl;
  345. cin>>e;
  346. switch (e)
  347. {
  348. case 1:
  349. {
  350.  
  351. getdel();
  352. break;
  353. }
  354. case 2:
  355. {
  356. del();
  357. break;
  358. }
  359. }
  360.  
  361. break;
  362. }
  363. case '5':
  364. {
  365.  
  366. getmodify();
  367. break;
  368. }
  369. case '6':
  370. {
  371.  
  372. sortingtany2();
  373. break;
  374.  
  375. }
  376.  
  377. case '7':
  378. {
  379. return 0;
  380. break;
  381. }
  382.  
  383. }
  384. }
  385.  
  386. system ("pause");
  387. return 0;
  388. }
Last edited by Ancient Dragon; May 7th, 2008 at 1:02 pm. Reason: add code tags
Reply With Quote