944,053 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5462
  • C++ RSS
Jul 24th, 2005
0

Need Help with Address Book Assignment

Expand Post »
I have to program an Address book program in C++. So far I've done about 40% of the assignment.
The main requirements of the Address Book are the following (Which I am confused about are):
1. Display 20 Contacts at a time, using the space bar to go to the next screen.
2. Search for Contacts by Name.
3. Edit Contact.

So far I have programmed the following, let me know what I need to modify in order for me to display 20 contacts at a time. Any help will be greatly appreciated :cheesy:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cctype>
  3. #include <cstring>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. struct Phonebook
  8. {
  9. char fname[15];
  10. char lname[15];
  11. char fullname[45];
  12. char number[50];
  13. char email[81];
  14. };
  15.  
  16. bool openPhonebook(fstream &, char [81]);
  17. void Displayphonelist(fstream &);
  18. void Addnewcontacts(fstream &);
  19. void Deletecontacts(fstream &);
  20. void Editinfo();
  21. void Searchcontact(fstream &);
  22.  
  23. int main()
  24. {
  25. fstream info;
  26.  
  27. int choice = 0;
  28. int count = 0;
  29.  
  30. cout << "1. Display Contact List\n";
  31. cout << "2. Add a new contact\n";
  32. cout << "3. Delete a personal contact\n";
  33. cout << "4. Edit personal contact information\n";
  34. cout << "5. Search for contact\n";
  35. cout << "6. Exit Program\n";
  36.  
  37. cout << "\nPlease enter choice number: \n";
  38. cin >> choice;
  39.  
  40. switch(choice)
  41. {
  42. case 1:
  43. {
  44. if(!openPhonebook(info, "Contactlist.txt"))
  45. {
  46. cout << "File open error!\n";
  47. exit(1); //Exits the program on error
  48. }
  49.  
  50. cout << "File opened successfully.\n";
  51. cout << "Now reading data from file.\n\n";
  52. Displayphonelist(info);
  53. info.close();
  54. cout << "\nDone\n";
  55. break;
  56. }
  57. case 2:
  58. {
  59. cout << "Add a new contact\n";
  60.  
  61.  
  62. Addnewcontacts(info);
  63. cout << "\nSaving Information....\n";
  64. break;
  65. }
  66. case 3: cout << "Delete a personal contact\n";
  67. break;
  68. case 4: cout << "Edit personal contact information\n";
  69. break;
  70. case 5: cout << "Searching for contacts";
  71. Searchcontact(info);
  72. cout << "\nDone\n";
  73. break;
  74. case 6: cout << "Goodbye!\n";
  75. exit(1);
  76.  
  77. default: cout << "Please enter valid choice\n";
  78. cin >> choice;
  79. }
  80.  
  81. return 0;
  82. }
  83. // END OF MAIN
  84.  
  85. bool openPhonebook(fstream &file, char *name)
  86. {
  87. bool status;
  88.  
  89. file.open(name, ios::in);
  90. if (file.fail())
  91. status = false;
  92. else
  93. status = true;
  94. return status;
  95. }
  96.  
  97. void Displayphonelist(fstream &file)
  98. {
  99. Phonebook Info;
  100.  
  101. file.getline(Info.fullname, 45);
  102. file.getline(Info.number,50);
  103. file.getline(Info.email, 81);
  104.  
  105. while(!file.eof())
  106. {
  107. cout << Info.fullname << endl;
  108. cout << Info.number << endl;
  109. cout << Info.email << endl;
  110.  
  111. file.getline(Info.fullname, 45);
  112. file.getline(Info.number, 50);
  113. file.getline(Info.email, 81);
  114. }
  115. }
  116.  
  117. void Addnewcontacts(fstream &file)
  118. {
  119. Phonebook Info;
  120. char choice;
  121. do
  122. {
  123. cout << "Opening file.....\n";
  124. file.open("Contactlist.txt", ios::out| ios::app);
  125. cout << "File open successfully\n";
  126.  
  127. if(!file)
  128. {
  129. cout << "Error opening Contactlist.txt\n";
  130. exit(1);
  131. }
  132.  
  133. if(!file.eof())
  134. {
  135. cout << "Enter first name: ";
  136. cin >> Info.fname;
  137. cout << "Enter last name: ";
  138. cin >> Info.lname;
  139. strcpy(Info.fullname, Info.fname);
  140. strcat(Info.fullname, " ");
  141. strcat(Info.fullname, Info.lname);
  142.  
  143. cout << "Enter number: ";
  144. cin.ignore();
  145. cin.getline(Info.number, 50);
  146.  
  147. cout << "Enter email: ";
  148. cin.getline(Info.email, 81);
  149.  
  150. file << "Name: " << Info.fullname << endl;
  151. file << "Number: " << Info.number << endl;
  152. file << "Email: " << Info.email << endl;
  153. file << "\n";
  154.  
  155. file.close();
  156. }
  157.  
  158. cout << "Enter another contact?\n";
  159. cin >> choice;
  160. }while((choice == 'y')||(choice == 'Y'));
  161.  
  162. if((choice == 'n')||(choice == 'N'))
  163. {
  164. file.close();
  165. }
  166. }
  167.  
  168. void Searchcontact(fstream &file)
  169. {
  170. Phonebook Info;
  171. char fname2[15];
  172. char lname2[15];
  173. char fullname2[45];
  174.  
  175. cout << "Opening file...\n";
  176. file.open("Contactlist.txt", ios::in);
  177. cout << "File open successfully\n";
  178.  
  179. while(!file)
  180. {
  181. cout << "Error opening file\n";
  182. exit(1);
  183. }
  184.  
  185. while(!file.eof())
  186. {
  187. cout << "Search by Full Name: \n";
  188. cout << "First Name: \n";
  189. cin >> fname2;
  190. cout << "Last Name: \n";
  191. cin >> lname2;
  192. strcpy(fullname2, fname2);
  193. strcat(fullname2, "");
  194. strcat(fullname2, lname2);
  195.  
  196. if(strcmp(fullname2, Info.fullname))
  197. {
  198. cout << "There is no match\n";
  199. file.close();
  200. }
  201. else
  202. {
  203. cout << "Name Found!\n";
  204. cout << fullname2 << endl;
  205. cout << Info.fullname << endl;
  206.  
  207. file.getline(Info.fullname, 45);
  208. file.getline(Info.number, 50);
  209. file.getline(Info.email, 81);
  210.  
  211. cout << "\nDone\n";
  212. }
  213. }
  214. }
[edit]
I added code tags for you this time. Don't post again until you understand that code is easier to read with them. I also took the time to remove excessive newlines from your butt ugly formatting, changed tabs to spaces because tabs can be erratic, and made everything neat and tidy without actually changing the meat of your code. You're welcome. Now do me a favor and do the same thing without my help in the future. -Narue
[/edit]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
navamous is offline Offline
1 posts
since Jul 2005
Apr 14th, 2008
0

Re: Need Help with Address Book Assignment

Hey I was looking online for a solution to a program I'm trying to write and came accross this message. I'm taking a programming class and it's getting pretty tough. I barely understand what's going on anymore. Anyway, i've been writing a program that has to be seperated into different files and i'll show you guys what i have so far. Btw, these are the requirements.

************Read “AddressBook.txt” file (if the file exists) Module:

Insert each entry into the array of structures by name
a. First record (line) contains the number of entries
b. Entry consists of the following information
Name (maximum 30 characters)
BirthDate (mm/dd/yyyy)
Address Information
Type (maximum 8 characters: Friend, Family, Business)
c. Address Information consists of the following information:
Street (maximum 35 characters)
City (maximum 20 characters)
State (2 characters)
Zip (5 characters)
Phone (14 characters)

4) User can choose the following options:
a. List all entries. This option will result in an alphabetic list.
b. Look up a specific entry by Name (use binary search). Ask user for the desired name. Output error message if the name is not found otherwise output the information.
c. List all the entries for a specific Type. Ask user for the desired type (Friend, Family, Business). Output the type followed by all the entries that match this type (i.e. Friend). This will also result in an alphabetic listing.***********************





With that being said I don't have much coding done because of how there's so many different approaches to this program. Just to warn whoever looks at this i'm aware that a lot of this is incomplete and a little sloppy.


This is the header file
C++ Syntax (Toggle Plain Text)
  1. int NAME_LENGTH;
  2. int BIRTHDAY_LENGTH;
  3. int ADDRESS_LENGTH;
  4. int MAX_LISTING;
  5. char relation [relation_+1];
  6.  
  7.  
  8. struct addressType
  9. {
  10. string street;
  11. string city;
  12. string state;
  13. string zip;
  14. string phone;
  15. };
  16.  
  17.  
  18. struct contactType
  19. {
  20. char name[NAME_LENGTH];
  21. int Bday;
  22. addressType address;
  23. char relation;
  24. };
  25. contactType newContact;
  26.  
  27. contactType contacts[100];
  28.  
  29.  
  30.  
  31.  
  32. void inputName (ifstream &, char , nameType &);
  33. void inputBday (ifstream &, int , BdayType &);
  34. void inputAddress (ifstream &, int , addressType &);
  35. void inputRelation (ifstream &, char , relationType &);
  36. int extern search (const nameType [], char, const char *);






This is the main
C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3.  
  4. int numNames, numBdays, numAdds, index, location;
  5. char searchArg[birthday_LENGTH+1];
  6. ifstream infile;
  7. ofstream outfile;
  8. contactType allcontact[MAX_CONTACTS]
  9.  
  10.  
  11.  
  12.  
  13.  
  14. infile.open (".AddressBook.txt");
  15. assert (!infile.fail());
  16.  
  17. outfile.open ("./contacts.txt");
  18. assert (!outfile.fail());
  19.  
  20. inputName ();
  21.  
  22.  
  23. inputBday ();
  24.  
  25.  
  26.  
  27. searchArg[birthday_LENGTH] = '\0';
  28. while (true)
  29. {
  30. cout << "\n\nEnter a Birthday(mm/dd/yyyy) or 0000000 to end: ";
  31. cin >> searchArg;
  32. if (strcmp (searchArg, "0000000") == 0)
  33. break;
  34. }
  35.  
  36.  
  37. infile.close();
  38.  
  39.  
  40.  
  41.  
  42. And this one I think is the input
  43.  
  44. for (int j = 0; j < 100; j++)
  45. cin >> contacts[j].name.first >> contacts[j].name.middle
  46. >> contacts[j].name.last;
  47.  
  48. for (int j = 0; j < 100; j++)
  49. cin >> contacts[j].Bday.month >> contacts[j].Bday.day
  50. >> contacts[j].Bday.year;
  51.  
  52. for (int j = 0; j < 100; j++)
  53. cin >> contacts[j].address.street >> contacts[j].name.state
  54. >> contacts[j].address.zip >> contacts[j].address.phone;
  55.  
  56. for (int j = 0; j < 100; j++)
  57. cin >> contacts[j].relation.Friend >> contacts[j].relation.Family
  58. >> contacts[j].relation.business;
  59.  
  60.  
  61. I know that this needs a lot of changes.
  62. I also have an example of binary coding so if sum1 could show me where my information would go that would be appreciated as well.
  63.  
  64.  
  65. int search (const studentType list[], int length, const char * searchArg)
  66. {
  67. bool found = false;
  68. int low = 0, high = length-1, mid;
  69.  
  70. while (!found && low <= high)
  71. {
  72. mid = (low + high) / 2;
  73. if (strcmp(searchArg, list[mid].ID) == 0)
  74. found = true;
  75. else
  76. if (strcmp(searchArg, list[mid].ID) < 0)
  77. high = mid - 1;
  78. else
  79. low = mid + 1;
  80. }
  81.  
  82. if (found)
  83. return mid;
  84. else
  85. return -1;
  86. }




Thanks for whoever looks at and helps me with my poor programming.
Last edited by Ancient Dragon; Apr 15th, 2008 at 1:32 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Light Poster
Spagett912 is offline Offline
31 posts
since Apr 2008
Apr 15th, 2008
0

Re: Need Help with Address Book Assignment

Thanks a lot i really appreciate it. I don't know why but coding became 100x's harder this time around.
Reputation Points: 10
Solved Threads: 0
Light Poster
Spagett912 is offline Offline
31 posts
since Apr 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: partial template friend
Next Thread in C++ Forum Timeline: Only accept an alphabetical character





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


Follow us on Twitter


© 2011 DaniWeb® LLC