944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3356
  • C++ RSS
Jul 24th, 2005
0

Need help with Address book

Expand Post »
Hello,
I just started C++ and I have this assignment to write. I started writing it but I can't seem to get this one thing and that is how to create a new contact and store it in a file.
I need to know if I used the structure correctly. Also I don't know how you create multiple contacts with a loop. I wouldn't necessarily need the answer from you guys. I just want to know what topics I need to learn to be able to do this.
What do I need to use? That's all I need. I can write the programs for myself.

Also if you guys can tell me how you search for and display a single contact. That would be helpful. Thanks for understanding.

pscha3

Here's the code:
C++ Syntax (Toggle Plain Text)
  1. //This is the term project by Chaitanya Pillalamarri. Class CS-102.
  2. #include<iostream>
  3. #include<iomanip>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<fstream>
  7. using namespace std;
  8.  
  9. void Display();
  10. void Delete();
  11. void Create();
  12. void Modify();
  13. void Search();
  14. void Quit(void);
  15. void Restart(int);
  16.  
  17. int Choice=0;
  18.  
  19. int main()
  20. {
  21. //The following output statements are the menu.
  22. cout<<""<<endl;
  23. cout<<" Welcome to Virtual Address Book "<<endl;
  24. cout<<"In this book, you have a variety of features to revolutionize your contact lists"<<endl;
  25. cout<<""<<endl;
  26. cout<<"Follow the directions below and chose appropriately from the menu."<<endl;
  27. cout<<""<<endl;
  28.  
  29. //These are the choices.
  30. cout<<"Please type in the following number to perform the following task:"<<endl;
  31. cout<<" 1 - Display all your contacts\n";
  32. cout<<" 2 - Create a new contact\n";
  33. cout<<" 3 - Edit information for a contact\n";
  34. cout<<" 4 - Search for a contact\n";
  35. cout<<" 5 - Delete contact\n";
  36. cout<<" 6 - Quit this application\n";
  37. cout<<""<<endl;
  38. cout<<""<<endl;
  39. cout<<"If the application asks you to restart the program, just type in '7'."<<endl;
  40. cout<<" 7 - Restarts program"<<endl;
  41. cout<<""<<endl;
  42.  
  43. //The user choice.
  44. cout<<"ENTER YOUR CHOICE:"<<endl;
  45. cin>>Choice;
  46.  
  47. switch(Choice)
  48. {
  49. case 1:
  50. {
  51. Display();
  52. break;
  53. }
  54. case 2:
  55. {
  56. Create();
  57. break;
  58. }
  59. case 3:
  60. {
  61. Modify();
  62. break;
  63. }
  64. case 4:
  65. {
  66. Search();
  67. break;
  68. }
  69. case 5:
  70. {
  71. Delete();
  72. break;
  73. }
  74. case 6:
  75. {
  76. Quit();
  77. break;
  78. }
  79. case 7:
  80. {
  81. Restart(Choice);
  82. break;
  83. }
  84. }
  85. }
  86.  
  87. void Restart(int Choice)
  88. {
  89. while(Choice==7)
  90. main();
  91. }
  92. void Quit(void)
  93. {
  94. cout<<"Goodbye!"<<endl;
  95. exit(0);
  96. }
  97. void Create()
  98. {
  99. char Name1;
  100. Name1=0;
  101. struct Contact
  102. {
  103. char Name;
  104. int Home;
  105. int Business;
  106. int Cell;
  107. int Other;
  108. char Email;
  109. char Address;
  110. int Zip;
  111. char City;
  112. char State;
  113. char Country;
  114. char Job;
  115. char Company;
  116. };
  117. Contact hi;
  118. ofstream Data;
  119. cout<<"This will let you create a new contact."<<endl;
  120. cout<<"Please provide the following information."<<endl;
  121. cout<<"When you do not want to give information for a certain field, just press enter to skip it."<<endl;
  122.  
  123. //This is the place where the user enters the information.
  124.  
  125. Data.open("Addressbook.txt",ios::out);
  126. cout<<"Name: ";
  127. cin>>hi.Name;
  128. cout<<""<<endl;
  129. cout<<"Job title: ";
  130. cin>>hi.Job;
  131. cout<<"Company: ";
  132. cin>>hi.Company;
  133. cout<<""<<endl;
  134. cout<<"Address: ";
  135. cin>>hi.Address;
  136. cout<<"City: ";
  137. cin>>hi.City;
  138. cout<<"State: ";
  139. cin>>hi.State;
  140. cout<<"Zip: ";
  141. cin>>hi.Zip;
  142. cout<<"Country: ";
  143. cin>>hi.Country;
  144. cout<<"E-mail: ";
  145. cin>>hi.Email;
  146. cout<<"Home: ";
  147. cin>>hi.Home;
  148. cout<<"Business: ";
  149. cin>>hi.Business;
  150. cout<<"Cell: ";
  151. cin>>hi.Cell;
  152. cout<<"Other: ";
  153. cin>>hi.Other;
  154. Data.close();
  155. }
  156. void Display()
[edit]Code tags added. Learn how to use them before posting again. -Narue[/edit]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pscha3 is offline Offline
6 posts
since Jul 2005
Jul 25th, 2005
0

Re: Need help with Address book

The use of ur structure seems fine...
To input multiple contacts just put an infinite while loop to enclose ur switch statment... so u will quit whenever u click quit.. that way u can get rid of the restart command..
For searching...
U should be storing ur contacts in an array of structures..now u r overwriting whenever the user says he wants to add a contact.. so just change that also.. so whenever u r supposed to search.. just ask for the field and find that entry that u require by a simple linear search...
Reputation Points: 10
Solved Threads: 1
Light Poster
shre86 is offline Offline
33 posts
since Jun 2005
Jul 25th, 2005
0

Re: Need help with Address book

Yea actually after an hour or so after my first post I totally knew how to do it. Read my book again. So yea. My only question is how do I get more memory for storing data. Since in my new program, here it is
C++ Syntax (Toggle Plain Text)
  1. //This is the term project by Chaitanya Pillalamarri. Class CS-102.
  2. #include<iostream>
  3. #include<iomanip>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<fstream>
  7. #include<stdlib.h>
  8. #include<stdio.h>
  9. using namespace std;
  10.  
  11. void Display();
  12. void Delete();
  13. void Create();
  14. void Modify();
  15. void Search();
  16. void Quit(void);
  17. void Restart(int);
  18.  
  19. int Choice=0;
  20. char ch;
  21. fstream Data;
  22. struct Contact
  23. {
  24.  
  25. char Email[46];
  26. char Address[46];
  27. int Zip;
  28. char City[26];
  29. char State[26];
  30. char Job[46];
  31. char Company[46];
  32. };
  33. char Name[41];
  34. char Country[56];
  35. int Home;
  36. int Business;
  37. int Cell;
  38. int Other;
  39. char Filename[101];
  40. Contact hi;
  41. int main()
  42. {
  43. //The following output statements are the menu.
  44. cout<<""<<endl;
  45. cout<<" Welcome to Virtual Address Book "<<endl;
  46. cout<<"In this book, you have a variety of features to revolutionize your contact lists"<<endl;
  47. cout<<""<<endl;
  48. cout<<"Follow the directions below and chose appropriately from the menu."<<endl;
  49. cout<<""<<endl;
  50.  
  51. //These are the choices.
  52. cout<<"Please type in the following number to perform the following task:"<<endl;
  53. cout<<" 1 - Display all your contacts\n";
  54. cout<<" 2 - Create a new contact\n";
  55. cout<<" 3 - Edit information for a contact\n";
  56. cout<<" 4 - Search for a contact\n";
  57. cout<<" 5 - Delete contact\n";
  58. cout<<" 6 - Quit\n";
  59. cout<<""<<endl;
  60. cout<<""<<endl;
  61. cout<<"If you want to return to this screen, just type in '7'."<<endl;
  62. cout<<" 7 - Returns to main menu"<<endl;
  63. cout<<""<<endl;
  64.  
  65. //The user choice.
  66. cout<<"Enter your choice:"<<endl;
  67. cin>>Choice;
  68.  
  69. switch(Choice)
  70. {
  71. case 1:
  72. {
  73. Display();
  74. break;
  75. }
  76. case 2:
  77. {
  78. Create();
  79. break;
  80. }
  81. case 3:
  82. {
  83. Modify();
  84. break;
  85. }
  86. case 4:
  87. {
  88. Search();
  89. break;
  90. }
  91. case 5:
  92. {
  93. Delete();
  94. break;
  95. }
  96. case 6:
  97. {
  98. Quit();
  99. break;
  100. }
  101. case 7:
  102. {
  103. Restart(Choice);
  104. break;
  105. }
  106. default:
  107. {
  108. cout<<"**********************************************"<<endl;
  109. cout<<"* Error: You have entered a wrong choice *"<<endl;
  110. cout<<"* Please follow menu instructions. *"<<endl;
  111. cout<<"* The program will now restart. *"<<endl;
  112. cout<<"**********************************************"<<endl;
  113. main();
  114. }
  115. }
  116. }
  117.  
  118. void Restart(int Choice)
  119. {
  120. while(Choice==7)
  121. main();
  122. }
  123. void Quit(void)
  124. {
  125. cout<<"Goodbye!"<<endl;
  126. exit(0);
  127. }
  128. void Create()
  129. {
  130. cout<<""<<endl;
  131. cout<<"*This will let you create a new contact*"<<endl;
  132. cout<<"*Please provide the following information*"<<endl;
  133. cout<<"*When you want to skip a filed, just press enter*"<<endl;
  134. cout<<""<<endl;
  135. cout<<"Please enter a file name for the contact."<<endl;
  136. cout<<"We recommend using the person's name."<<endl;
  137. cin>>Filename;
  138. cout<<""<<endl;
  139. cout<<"Creating..."<<endl;
  140. cout<<""<<endl;
  141.  
  142. //This is the place where the user enters the information.
  143. Data.open(Filename,ios::out);
  144. cout<<"Name: ";
  145. cin.getline(Name,40);
  146. cout<<""<<endl;
  147. cout<<"Job title: ";
  148. cin.getline(hi.Job,45);
  149. cout<<"Company: ";
  150. cin.getline(hi.Company,45);
  151. cout<<""<<endl;
  152. cout<<"Address: ";
  153. cin.getline(hi.Address,45);
  154. cout<<"City: ";
  155. cin.getline(hi.City,25);
  156. cout<<"State: ";
  157. cin.getline(hi.State,25);
  158. cout<<"Zip: ";
  159. cin>>hi.Zip;
  160. cout<<"Country: ";
  161. cin.getline(Country,50);
  162. cout<<"E-mail: ";
  163. cin.getline(hi.Email,45);
  164. cout<<"Home: ";
  165. cin>>Home;
  166. cout<<"Business: ";
  167. cin>>Business;
  168. cout<<"Cell: ";
  169. cin>>Cell;
  170. cout<<"Other: ";
  171. cin>>Other;
  172.  
  173. Data<<""<<endl;
  174. Data<<"Name: "<<Name<<endl;
  175. Data<<""<<endl;
  176. Data<<"Job title: "<<hi.Job<<endl;
  177. Data<<"Company: "<<hi.Company<<endl;
  178. Data<<""<<endl;
  179. Data<<"Address: "<<hi.Address<<endl;
  180. Data<<"City: "<<hi.City<<endl;
  181. Data<<"State: "<<hi.State<<endl;
  182. Data<<"Zip: "<<hi.Zip<<endl;
  183. Data<<"Country: "<<Country<<endl;
  184. Data<<""<<endl;
  185. Data<<"E-mail: "<<hi.Email<<endl;
  186. Data<<""<<endl;
  187. Data<<"Home: "<<Home<<endl;
  188. Data<<"Business: "<<Business<<endl;
  189. Data<<"Cell: "<<Cell<<endl;
  190. Data<<"Other: "<<Other<<endl;
  191. Data<<""<<endl;
  192. Data.close();
  193.  
  194. cout<<"Do you want to return to the main menu?(7=restart,6=Exit)"<<endl;
  195. cin>>Choice;
  196. if(Choice==7)
  197. main();
  198. else
  199. return;
  200. }
  201. void Display()
  202. {
  203. char ch;
  204.  
  205. Data.open(Filename,ios::in);
  206.  
  207. if(Data.fail())
  208. {
  209. cout << "Error: Unable to open file."<<endl;
  210. exit(1);
  211. }
  212. Data.get(ch);
  213. while(!Data.eof())
  214. {
  215. cout << ch;
  216. Data.get(ch);
  217. }
  218.  
  219. Data.close();
  220. }
  221. void Modify()
  222. {
  223.  
  224. }
  225. void Search()
  226. {
  227. cout<<""<<endl;
  228. cout<<"To start searching the directory follow the instructions."<<endl;
  229. cout<<"Please enter a file name."<<endl;
  230. cin>>Filename;
  231.  
  232. Data.open(Filename,ios::in);
  233. cout<<"Searching...."<<endl;
  234. cout<<""<<endl;
  235. if(!Data)
  236. {
  237. cout<<Filename<<" could not be opened."<<endl;
  238. cout<<"Please check your spelling and try again."<<endl;
  239. Search();
  240. }
  241. Data.get(ch);
  242. while(!Data.eof())
  243. {
  244. cout<<ch;
  245. Data.get(ch);
  246. }
  247. Data.close();
  248.  
  249. cout<<"Do you want to search for another contact?(0 for yes)"<<endl;
  250. cout<<"Or go back to the main menu?(7 for main menu)"<<endl;
  251. cin>>Choice;
  252. if(Choice==7)
  253. main();
  254. else if(Choice==0||Choice==0)
  255. Search();
  256. }
  257. void Delete()
  258. {
  259. }

When i create a new contact, it skips the first field which is Name. And it overwrites a lot of time. Like if I want to put in a phone number, If I put like more than three numbers it skips all the other fields and goes directly to the end.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pscha3 is offline Offline
6 posts
since Jul 2005
Jul 25th, 2005
0

Re: Need help with Address book

Read this, please.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 1st, 2009
0

Re: Need help with Address book

ok for your first issue the Inabillity to Name your new contact after creating the file i added another "cin.getline(Name,40);"
to the code just before the action is requested by the programme now the stage being skiped is no one of no importance

C++ Syntax (Toggle Plain Text)
  1. void Create()
  2. {
  3. cout<<""<<endl;
  4. cout<<"*This will let you create a new contact*"<<endl;
  5. cout<<"*Please provide the following information*"<<endl;
  6. cout<<"*When you want to skip a filed, just press enter*"<<endl;
  7. cout<<""<<endl;
  8. cout<<"Please enter a file name for the contact."<<endl;
  9. cout<<"We recommend using the person's name."<<endl;
  10. cin>>Filename;
  11. cout<<""<<endl;
  12. cout<<"Creating..."<<endl;
  13. cout<<""<<endl;
  14.  
  15. //This is the place where the user enters the information.
  16. Data.open(Filename,ios::out);
  17. cin.getline(Name,40);
  18. cout<<"Name: ";
  19. cin.getline(Name,40);
  20. cout<<""<<endl;


Also the other problem you had of telephone numbers crashing/reseting the programme if more that a few numbers are used ... I have no idea why that is happening but if you can afford to .. redefine the Telephone numbers as Char's
like this

C++ Syntax (Toggle Plain Text)
  1. char Home[12];
  2. char Business[12];
  3. char Cell[12];
  4. char Other[12];

or as strings

C++ Syntax (Toggle Plain Text)
  1. stringHome;
  2. stringBusiness;
  3. stringCell;
  4. stringOther;

or you could use plain simple strings it all depends and what you are goin to be doing with the data after if has been created
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bravo303 is offline Offline
1 posts
since Apr 2009
Apr 1st, 2009
0

Re: Need help with Address book

If they are still working on this more than 3.5 years later, there is something very wrong.

I suppose it could still be of use to someone that comes across this searching for help with their own problem.
Reputation Points: 22
Solved Threads: 28
Junior Poster
adam1122 is offline Offline
181 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: question on memory allocation for array
Next Thread in C++ Forum Timeline: switch statement not working





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


Follow us on Twitter


© 2011 DaniWeb® LLC