943,513 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1379
  • C++ RSS
May 5th, 2008
0

help in phonebook

Expand Post »
i need help in c++ program I'm asked to make a phone book which can sort all the names in it alphabetically plllllz i need help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nouryn is offline Offline
14 posts
since May 2008
May 5th, 2008
0

Re: help in phonebook

Why don't you show us what you have started and we'll go from there. If you have nothing started start and you'll be more likely to get help.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
May 5th, 2008
0

Re: help in phonebook

i don't have a clue a bout the sort i've tried many functions from the internet but they didn't work but i've began in the other function in the phone book and that is what i get thnx




C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<algorithm>
  5. using namespace std;
  6. fstream phonebook;
  7. struct phone
  8. {
  9. char word[50];
  10. char fword[50];
  11. fstream edit;
  12. int id;
  13. string fname;
  14. string sname;
  15. string lname;
  16. int phonenumber;
  17. int mobilenumber;
  18. string email;
  19. };
  20.  
  21. phone info;
  22. char d,f,r;
  23. string c,g;
  24.  
  25. int add()
  26. {
  27. phonebook.open("c:\\c\\phonebook.txt",ios::out|ios::app);
  28. cout<<"enter the first name\n";
  29. cin>>info.fname;
  30. cout<<"enter the second name\n";
  31. cin>>info.sname;
  32. cout<<"enter the last name\n";
  33. cin>>info.lname;
  34. cout<<"enter the phone number\n";
  35. cin>>info.phonenumber;
  36. cout<<"enter the moile number\n";
  37. cin>>info.mobilenumber;
  38. cout<<"enter the e-mail\n";
  39. cin>>info.email;
  40.  
  41. phonebook<<info.fname<<"\t "<<info.sname<<"\t "<<info.lname<<" \t "
  42. <<info.phonenumber<<" \t "<<info.mobilenumber<<" \t "<<info.email<<endl;;
  43. cout<<endl;
  44. phonebook.close();
  45.  
  46. return 0;
  47. }
  48.  
  49. int getdisplay()
  50. {
  51.  
  52. cout<<"if u wanna display all contacts press 'a'\n"<<"if u wanna display contacts begining with specific letter press 'c'\n";
  53. cin>>r;
  54. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  55. phonebook.clear();
  56. switch(r)
  57. {
  58.  
  59. case 'a':
  60. {
  61. while(!phonebook.eof())
  62. {
  63. getline(phonebook,c);
  64. cout<<c<<endl;
  65. }
  66. phonebook.close();
  67. break;
  68. }
  69. case 'c':
  70. {
  71.  
  72. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  73. phonebook>>c;
  74. cout<<"enter the wanted letter";
  75. cin>>g;
  76. // getdisplay;
  77. cout<<"dsucyeehr";
  78.  
  79.  
  80. phonebook.close();
  81. break;
  82. }
  83. }
  84. return 0;
  85.  
  86. }
  87. void getsearch()
  88. {
  89. cout << "Enter your contact:"<<endl;
  90.  
  91. cin >> info.word;
  92. phonebook.open("c:\\c\\phonebook.txt",ios::in);
  93. phonebook.clear();
  94. int counter =0;
  95. while (!phonebook.eof()){
  96. phonebook >> info.fword;
  97. int j=strcmp(info.fword,info.word);
  98. if (j==0){
  99. cout << "contact was found in the file :)"<<endl;
  100. cout<< info.word << endl;
  101. counter ++;
  102. phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
  103. cout<<info.fword<<" "<<info.fname<<" "<<info.sname<<" "<<info.lname
  104. <<" "<<info.phonenumber<<" "<<info.mobilenumber<<" "<<info.email;
  105. break;
  106. }
  107. }
  108. if (counter == 0)
  109. cout << "contact was not found in the file :(" << endl;
  110.  
  111.  
  112. phonebook.close();
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. int main()
  123. {
  124.  
  125.  
  126. while(1)
  127. {
  128.  
  129.  
  130. cout<<endl<<"1-Adding new contacts"<<endl
  131. <<"2-Displaying contacts information"<<endl
  132. <<"3-Searching for a contact"<<endl
  133. <<"4-Deleting a contact"<<endl
  134. <<"5-Modifying a contact information"<<endl
  135. <<"6-Sort the contacts by name"<<endl
  136. <<"7-exit"<<endl<<endl;
  137. cout<<"enter your choice"<<endl;
  138. cin>>d;
  139. switch(d)
  140.  
  141. {
  142. case '1':
  143.  
  144. {
  145. add();
  146. cout<<"if u wann add another contact press 'y' if not press 'n'";
  147. cin>>f;
  148. if(f=='y')
  149.  
  150. {
  151. add();
  152. }
  153.  
  154. break;
  155. }
  156. case '2':
  157. {
  158. getdisplay();
  159. break;
  160. }
  161. case '3':
  162. {
  163. getsearch();
  164.  
  165. break;
  166.  
  167. }
  168. case '4':
  169. {
  170. break;
  171. }
  172. case '5':
  173. {
  174. break;
  175. }
  176. case '6':
  177. {
  178.  
  179. break;
  180. }
  181. case '7':
  182. {
  183. return 0;
  184. break;
  185. }
  186. }
  187. }
  188.  
  189. system ("pause");
  190. return 0;
  191. }
Last edited by Ancient Dragon; May 7th, 2008 at 1:01 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nouryn is offline Offline
14 posts
since May 2008
May 5th, 2008
0

Re: help in phonebook

If you reading the information from phonebook why are you asking user to input it ?
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
May 5th, 2008
0

Re: help in phonebook

that is one of the function that if the user want to add another contact to the original phone book which already contain names
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nouryn is offline Offline
14 posts
since May 2008
May 7th, 2008
0

Re: help in phonebook

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nouryn is offline Offline
14 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Windows Message loop
Next Thread in C++ Forum Timeline: string to int





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC