Posted By

acecengic on 12/17/14


Tagged

memory phonebook addressbook malloc alloc c++


Versions (?)

Phonebook


 / Published in: C++
 

phonebook in c++ uses memory allocation and file io

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <fstream>
  5.  
  6. //Start with maximun 100 entries
  7. #define MaxNum 100
  8.  
  9. //Define some types
  10. typedef char NameStr[31];
  11. typedef char AddrStr[101];
  12. typedef char Phone[11];
  13.  
  14. //Define the structure
  15.  
  16. typedef struct Entry
  17. {
  18. NameStr Name;
  19. AddrStr Address;
  20. Phone HomeTel, HandPhone;
  21. } PhEntry;
  22.  
  23. typedef PhEntry* PhoneList[MaxNum];
  24.  
  25. //Function Prototypes
  26. void Display_menu(void); //Display the Menu
  27. void AddEntries(PhoneList, int&); //Add Entries to the Phone Book
  28. void DelEntry(PhoneList, int&); //Delete an entry from Phone Book
  29. void DispEntries(PhoneList, int); //List out all Entries one by one
  30. void Search(PhoneList, int); //Show an Entry of a given name
  31. int Locate(const NameStr&, PhoneList, int); //Locate the Entry by Name
  32. void Read(ifstream&, PhoneList, int&); //Read Phone Book from File
  33. void Write(ofstream&, PhoneList, int); //Write Phone Book to File
  34. void Trim(char*); //Remove trailing spaces
  35.  
  36. int main(void)
  37. {
  38. PhoneList PhoneBook; //Declare an array of Pointers
  39.  
  40. char FileName[]="Phonebook.dat";
  41. ifstream Fin;
  42. ofstream Fout;
  43. char option;
  44. int Size = 0;
  45.  
  46. // Read Phone Book from File
  47. Fin.open(FileName, ios::nocreate); //Open an existing file
  48. if (Fin.fail()) //File not exist
  49. {
  50. cout << "File not exist, creating a new phone book..." << endl;
  51. }
  52. else
  53. {
  54. cout << "Phone Book read in from File...." << endl;
  55. Read(Fin, PhoneBook, Size);
  56. Fin.close();
  57. }
  58. cout << "Press a key.....";
  59. cin.get(option).ignore();
  60. do
  61. {
  62. Display_menu();
  63. cin.get(option).ignore();
  64.  
  65. switch (option)
  66. {
  67. case 'a':
  68. case 'A': AddEntries(PhoneBook, Size); break;
  69. case 'd':
  70. case 'D': DelEntry(PhoneBook, Size); break;
  71. case 'l':
  72. case 'L': DispEntries(PhoneBook, Size); break;
  73. case 's':
  74. case 'S': Search(PhoneBook, Size); break;
  75. }
  76.  
  77. } while (option != 'q' && option != 'Q');
  78.  
  79. //Write Phone Book back to File
  80. if (Size>0)
  81. {
  82. Fout.open(FileName);
  83. cout << "Write Entries to file..." << endl;
  84. Write(Fout, PhoneBook, Size);
  85. cout << Size << " Entries written to file." << endl;
  86. Fout.close();
  87. for (int i=0; i<Size; i++)
  88. {
  89. delete PhoneBook[i]; //deallocate spaces
  90. PhoneBook[i] = NULL;
  91. }
  92. }
  93.  
  94. return 0;
  95. }
  96.  
  97. //Implementations
  98. void Display_menu(void)
  99. {
  100. system("CLS");
  101. cout << " Electronic Phone Book" << endl;
  102. cout << " =====================" << endl << endl;
  103. cout << "(a) Add new Entries" << endl;
  104. cout << "(d) Delete an Entry" << endl;
  105. cout << "(l) List all Entries" << endl;
  106. cout << "(s) Search by Name" << endl;
  107. cout << "(q) Call it a Day" << endl;
  108. cout << "Choose an Option: ";
  109. }
  110.  
  111. void AddEntries(PhoneList PK, int& Size)
  112. {
  113. char Ans;
  114. PhEntry Temp; //Declare a temporary structure
  115.  
  116. int Count = 0, i;
  117.  
  118. do
  119. {
  120. i = Size;
  121. system("CLS");
  122. cout << "Add Entries to Phone Book..." << endl;
  123. cout << "Enter Name: ";
  124. cin.getline(Temp.Name, 30, '\n');
  125. cout << "Enter Address Below:" << endl;
  126. cin.getline(Temp.Address, 100, '\n');
  127. cout << "Enter Home Tel: ";
  128. cin.getline(Temp.HomeTel, 10, '\n');
  129. cout << "Enter Hand Phone: ";
  130. cin.getline(Temp.HandPhone, 10, '\n');
  131. while ((i>0) && (strcmp(Temp.Name, PK[i-1]->Name) < 0))
  132. {
  133. PK[i] = PK[i-1];
  134. i--;
  135. }
  136. PK[i]=&Temp;
  137. Count++; Size++;
  138. cout << Count << " Entries Added." << endl << endl;
  139. cout << "Any more (y/n): ";
  140. cin.get(Ans).ignore();
  141. } while (Ans != 'n' && Ans != 'N');
  142. }
  143.  
  144. void DelEntry(PhoneList PK, int& Size)
  145. {
  146. NameStr Temp;
  147. int i;
  148. char Ans[10], C;
  149.  
  150. system("CLS");
  151. cout << "Delete an entry from Phone Book..." << endl << endl;
  152. cout << "Enter Name to Delete: ";
  153. cin.getline(Temp, 30);
  154.  
  155. i = Locate(Temp, PK, Size); //Call Locate function to find where is it
  156. if (i >= 0) //Fount it
  157. {
  158. cout << endl << PK[i]->Name << " to be Deleted." << endl;
  159. cout << endl << "Sure (y/n)? ";
  160. cin.get(C).ignore();
  161. if (C == 'y' || C == 'Y')
  162. {
  163. delete PK[i]; //deallocate memory
  164. for (int j=i+1; j < Size; j++)
  165. PK[j-1] = PK[j];
  166. Size--;
  167. PK[Size] = NULL;
  168. cout << Temp << " Deleted." << endl;
  169. }
  170. else
  171. cout << Temp << " Not Deleted." << endl;
  172. }
  173. else
  174. cout << endl << "Name not in the Phone Book." << endl;
  175.  
  176. cout << "Press Enter to continue....";
  177. cin.getline(Ans, 9);
  178. }
  179.  
  180. void DispEntries(PhoneList PK, int Size)
  181. {
  182. char Ans[10];
  183. system("CLS");
  184. cout << "List of Entries in the Phone Book....." << endl << endl;
  185. for (int i=0; i<Size; i++)
  186. {
  187. cout << "Entry " << i+1 << ": " << endl;
  188. cout << PK[i]->Name << endl;
  189. cout << PK[i]->Address << endl;
  190. cout << "Tel: " << PK[i]->HomeTel << "(Home), ";
  191. cout << PK[i]->HandPhone << "(HP)" << endl << endl;
  192. cout << "Press Enter to continue....";
  193. cin.getline(Ans, 9);
  194. }
  195. }
  196.  
  197. void Search(PhoneList PK, int Size)
  198. {
  199. NameStr Temp;
  200. int i;
  201. char Ans[10];
  202.  
  203. system("CLS");
  204. cout << "Search Entry by Name..." << endl << endl;
  205. cout << "Enter Name to Search: ";
  206. cin.getline(Temp, 30);
  207.  
  208. i = Locate(Temp, PK, Size); //Call Locate function to find where is it
  209. if (i >= 0) //Fount it
  210. {
  211. cout << endl;
  212. cout << PK[i]->Name << endl;
  213. cout << PK[i]->Address << endl;
  214. cout << "Tel: " << PK[i]->HomeTel << "(Home), ";
  215. cout << PK[i]->HandPhone << "(HP)" << endl << endl;
  216. }
  217. else
  218. cout << endl << "Name not in the Phone Book." << endl;
  219.  
  220. cout << "Press Enter to continue....";
  221. cin.getline(Ans, 9);
  222. }
  223.  
  224. int Locate(const NameStr& SName, PhoneList PK, int Size)
  225. {
  226. int i=0;
  227. while (i < Size)
  228. {
  229. if (strcmp(SName, PK[i]->Name) == 0) //Found it
  230. return i;
  231. if (strcmp(SName, PK[i]->Name) < 0) //Not in the list
  232. return -1;
  233. i++;
  234. }
  235. return -1; //Reach the end of list, still din't find it
  236. }
  237.  
  238. void Read(ifstream& IN, PhoneList PK, int& Size)
  239. {
  240. char Buffer[201]={0}; //Read Buffer
  241.  
  242. while(IN.getline(Buffer, 200)) //if read success
  243. {
  244. PK[Size] = new PhEntry; //allocate space
  245. strncpy(PK[Size]->Name, Buffer, 30);
  246. PK[Size]->Name[30]='\0';
  247. Trim(PK[Size]->Name);
  248. strncpy(PK[Size]->Address, &Buffer[30], 100);
  249. PK[Size]->Address[100]='\0';
  250. Trim(PK[Size]->Address);
  251. strncpy(PK[Size]->HomeTel, &Buffer[130], 8);
  252. PK[Size]->HomeTel[8] = '\0';
  253. strncpy(PK[Size]->HandPhone, &Buffer[139], 8);
  254. PK[Size]->HandPhone[8] = '\0';
  255. Size++;
  256. }
  257. }
  258.  
  259. void Write(ofstream& OUT, PhoneList PK, int Size)
  260. {
  261. for (int i=0; i<Size; i++)
  262. {
  263. OUT << setiosflags(ios::left);
  264. OUT << setw(30) << PK[i]->Name;
  265. OUT << setw(100) << PK[i]->Address;
  266. OUT << setw(9) << PK[i]->HomeTel;
  267. OUT << setw(9) << PK[i]->HandPhone << endl;
  268. }
  269. }
  270.  
  271. void Trim(char* S)
  272. {
  273. int L = strlen(S) - 1;
  274. while (L > 0 && S[L] == ' ')
  275. S[L--] = 0;
  276. }

Report this snippet  

You need to login to post a comment.