Advertisement
Guest User

contactHelers.c

a guest
Dec 10th, 2018
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.22 KB | None | 0 0
  1. /*
  2. Hunter Wright
  3. hwright5
  4. 104039185
  5. IPC144 - SYY
  6. hwright5@myseneca.ca
  7. Assignment 2 - Milestone 4
  8. 2018-11-22
  9. */
  10.  
  11. /*
  12. This source file contains all of the helping code for the entire program. It states all of the functions from contactHelpers.h
  13. and assigns them values so that when they are called else where in the program they may perform that same task, and actions such as
  14. clearKeyboard doesnt have to be re-coded (or copy and pasted) numerous times.
  15. */
  16.  
  17. #define _CRT_SECURE_NO_WARNINGS
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "contactHelpers.h"
  21.  
  22. #define MAXCONTACTS 5
  23.  
  24.  
  25. // Function Definitions
  26.  
  27. // Clear the standard input buffer
  28. //this function creates a 'buffer' between each segment of the program
  29. void clearKeyboard(void)
  30. {
  31. //clear the buffer
  32. while (getchar() != '\n');
  33. }
  34.  
  35. // pause: Empty function definition goes here:
  36. //this pause function clears the keyboard and the program from any function that was just occuring
  37. void pause(void)
  38. {
  39. printf("(Press Enter to Continue)");
  40. clearKeyboard();
  41. printf("\n");
  42. }
  43.  
  44. // getInt: Empty function definition goes here:
  45. //getInt gathers information from the user in integer(number) values
  46. //if the user enters anything other than an integer number, then it outputs an error and asks the user to try again
  47. int getInt(void)
  48. {
  49. int value;
  50. char NL = 'x';
  51.  
  52. scanf(" %d%c", &value, &NL);
  53.  
  54. while (NL != '\n')
  55. {
  56. clearKeyboard();
  57. printf("*** INVALID INTEGER *** <Please enter an integer>: ");
  58. scanf(" %d%c", &value, &NL);
  59.  
  60. }
  61. return value;
  62. }
  63.  
  64. // getIntInRange: Empty function definition goes here:
  65. //this function works similar to getInt, but similar to if/else statements it has restrictions, if the user enters any number
  66. //outside what the restrictions are set to, then the programm outputs an error and asks the user to try again
  67. int getIntInRange(int inferiorLimit, int superiorLimit)
  68. {
  69. int iLoc = getInt();
  70.  
  71. while ((iLoc < inferiorLimit) || (iLoc > superiorLimit))
  72. {
  73. printf("*** OUT OF RANGE *** <Enter a number between %d and %d>: ", inferiorLimit, superiorLimit);
  74. iLoc = getInt();
  75. }
  76.  
  77. return iLoc;
  78. }
  79.  
  80. // getIntPositive: Empty function definition goes here:
  81. //this function is used specifically for the contacts address portion of the program, where if the user enters a number that is negative
  82. //then it outputs an error message - similar to the getIntInRange function
  83. int getIntPositiveStreet(int inferiorLimit, int superiorLimit)
  84. {
  85. int iLoc = getInt();
  86.  
  87. while ((iLoc < inferiorLimit) || (iLoc > superiorLimit))
  88. {
  89. printf("Please enter the contact's street number: ");
  90. iLoc = getInt();
  91. }
  92.  
  93. return iLoc;
  94. }
  95.  
  96. // getIntPositive: Empty function definition goes here:
  97. //this function is used specifically for the contacts address portion of the program, where if the user enters a number that is negative
  98. //then it outputs an error message - similar to the getIntInRange function
  99. int getIntPositiveApartment(int inferiorLimit, int superiorLimit)
  100. {
  101. int iLoc = getInt();
  102.  
  103. while ((iLoc < inferiorLimit) || (iLoc > superiorLimit))
  104. {
  105. printf("Please enter the contact's apartment number: ");
  106. iLoc = getInt();
  107. }
  108.  
  109. return iLoc;
  110. }
  111.  
  112. // yes: Empty function definition goes here:
  113. //this yes function is used for yes or no questions
  114. //exmaple: Would you like to enter a middle inital(s)? (y)es or (n)o
  115. int yes(void)
  116. {
  117. //variable response is used as a flag to determine whether or not the reponse is valid
  118. //if the user enters the correct response then the value == 1/0, and the program may move forward, otherwise
  119. //response == -1 and it is an error, and the program will not accept that
  120. char lChar = 'x';
  121. char lChar2 = 'x';
  122. int response = -1;
  123.  
  124. scanf(" %c%c", &lChar, &lChar2);
  125.  
  126. //if the user enters any character other than Y, y, N, or n then the program outputs an error message that it is an invalid response
  127. while (!((lChar2 == '\n') && ((lChar == 'Y') || (lChar == 'y') ||
  128. (lChar == 'n') || (lChar == 'N'))))
  129. {
  130. clearKeyboard();
  131. printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
  132. scanf(" %c%c", &lChar, &lChar2);
  133.  
  134. }
  135.  
  136. //exmaple of response == 1/0 or -1, to determine whether or not the program will accept the anser
  137. if ((lChar == 'Y') || (lChar == 'y')) {
  138. response = 1;
  139. }
  140. else if ((lChar == 'N') || (lChar == 'n')) {
  141. response = 0;
  142. }
  143.  
  144. return response;
  145. }
  146.  
  147.  
  148. // menu: Empty function definition goes here:
  149. //this menu function displays the list of every function available in the contact management system function
  150. int menu(void)
  151. {
  152.  
  153. printf("Contact Management System\n");
  154. printf("-------------------------\n");
  155. printf("1. Display contacts\n");
  156. printf("2. Add a contact\n");
  157. printf("3. Update a contact\n");
  158. printf("4. Delete a contact\n");
  159. printf("5. Search contacts by cell phone number\n");
  160. printf("6. Sort contacts by cell phone number\n");
  161. printf("0. Exit\n");
  162. printf("\n");
  163. printf("Select an option:> ");
  164.  
  165. return getIntInRange(0, 6); //restrictions set so the user must enter an option listed
  166. printf("\n");
  167. }
  168.  
  169.  
  170. // ContactManagerSystem: Empty function definition goes here:
  171. //this function is for the (not fully implemented) contact manager system, which will allow the user to edit, enter new, and sort
  172. //the contacts they have input into the system
  173. void contactManagerSystem(void)
  174. {
  175. struct Contact uContacts[MAXCONTACTS] =
  176. {
  177. //Manually entering and giving value to these initial contacts, used for the testing of adding, updating and deleting contacts
  178. {
  179. { "Rick", { '\0' }, "Grimes" },
  180. { 11, { '\0' }, "Trailer Park", "A7A 2J2", "King City" },
  181. { "4161112222", "4162223333", "4163334444" }
  182. },{
  183. { "Maggie", "R.", "Greene" },
  184. { 55, { '\0' }, "Hightop House", "A9A 3K3", "Bolton" },
  185. { "9051112222", "9052223333", "9053334444" }
  186. },{
  187. { "Morgan", "A.", "Jones" },
  188. { 11, { '\0' }, "Cottage Lane", "C7C 9Q9", "Peterborough" },
  189. { "7051112222", "7052223333", "7053334444" }
  190. },{
  191. { "Sasha", { '\0' }, "Williams" },
  192. { 55, { '\0' }, "Hightop House", "A9A 3K3", "Bolton" },
  193. { "9052223333", "9052223333", "9054445555" }
  194. },
  195. };
  196.  
  197. //Switch cases for each of the different options in the contact management system (maximum 5 contacts)
  198. int selection;
  199. int yn = 0;
  200. while (yn == 0) {
  201. selection = menu();
  202. switch (selection)
  203. {
  204. case 1:
  205. displayContacts(uContacts, MAXCONTACTS);
  206. pause();
  207. break;
  208. case 2:
  209. addContact(uContacts, MAXCONTACTS);
  210. pause();
  211. break;
  212. case 3:
  213. updateContact(uContacts, MAXCONTACTS);
  214. pause();
  215. break;
  216. case 4:
  217. deleteContact(uContacts, MAXCONTACTS);
  218. pause();
  219. break;
  220. case 5:
  221. searchContacts(uContacts, MAXCONTACTS);
  222. pause();
  223. break;
  224. case 6:
  225. sortContacts(uContacts, MAXCONTACTS);
  226. pause();
  227. break;
  228. case 0:
  229. printf("\nExit the program? (Y)es/(N)o: "); //Asks user if they would wish to exit the program
  230. yn = yes();
  231. printf("\n");
  232. if (yn)printf("Contact Management System: terminated\n"); //exit statement
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. }
  239.  
  240. //function that displays as a header before the list of all the contacts when the user inputs to display them
  241. void displayContactHeader(void)
  242. {
  243. printf("\n");
  244. printf("+-----------------------------------------------------------------------------+\n");
  245. printf("| Contacts Listing |\n");
  246. printf("+-----------------------------------------------------------------------------+\n");
  247. }
  248.  
  249. //Footer function that displays the total contacts in the system, just underneath a line separating from the list of contacts
  250. void displayContactFooter(int total)
  251. {
  252. printf("+-----------------------------------------------------------------------------+\n");
  253. printf("Total contacts: %d\n\n", total);
  254. }
  255.  
  256. //Function for gathering the mandatory phone number information for each contact, the number must be 10 digits and cannot
  257. //contain symbols, dashes, uderlines, or characters from the alphabet, otherwise it will not accept the number
  258. void getTenDigitPhone(char telNum[])
  259. {
  260. int needInput = 1;
  261. int i;
  262. while (needInput == 1) {
  263. scanf("%10s", telNum);
  264. clearKeyboard();
  265.  
  266. if (strlen(telNum) == 10) {
  267. needInput = 0;
  268.  
  269. for (i = 0; i <= 9; i++) {
  270. if (telNum[i] != '0' && telNum[i] != '1' && telNum[i] != '2' && telNum[i] != '3' && telNum[i] != '4' && telNum[i] != '5' && telNum[i] != '6' && telNum[i] != '7' && telNum[i] != '8' && telNum[i] != '9' && telNum[i] != 10) {
  271. needInput = 1;
  272. }
  273. }
  274. if (needInput == 1) {
  275. printf("Enter a 10-digit phone number: ");
  276. }
  277. }
  278. else {
  279. printf("Enter a 10-digit phone number: ");
  280. }
  281. }
  282. }
  283.  
  284. //search index function for when the user wants to find contacts
  285. //when the user enters a phone number to search for a contact, this function scans each of the contacts for the phone number provided
  286. int findContactIndex(const struct Contact uContacts[], int index, const char cellNum[])
  287. {
  288. int i = 0;
  289. for (i = 0; i <= index; i = i + 1) {
  290. if (strcmp(cellNum, uContacts[i].numbers.cell) == 0) {
  291. return i;
  292. }
  293. }
  294. return -1;
  295. }
  296.  
  297.  
  298.  
  299. //function that displays the direct information about each contact in the system
  300. //displays all the information that is known about each contact (unless not require/input by the user)
  301. void displayContact(const struct Contact* uContact)
  302. {
  303. printf(" %s ", uContact->name.firstName);
  304. if (strlen(uContact->name.middleInitial) != 0) printf("%s ", uContact->name.middleInitial);
  305. printf("%s\n", uContact->name.lastName);
  306. printf(" C: %-10s H: %-10s B: %-10s\n", uContact->numbers.cell, uContact->numbers.home, uContact->numbers.business);
  307. printf(" %d %s, ", uContact->address.streetNumber, uContact->address.street);
  308. if (uContact->address.apartmentNumber > 0) printf("Apt# %d, ", uContact->address.apartmentNumber);
  309. printf("%s, %s\n", uContact->address.city, uContact->address.postalCode);
  310. }
  311.  
  312. //unlike the displayContact function, this one is disigned for ordering and logistics of displaying the contacts
  313. //Takes the information from the displayContact function and then counts to keep displaying for each member in the system
  314. void displayContacts(const struct Contact uContacts[], int size)
  315. {
  316. int i;
  317. int count = 0;
  318. displayContactHeader();
  319. for (i = 0; i < size; i++)
  320. {
  321. if (strlen(uContacts[i].numbers.cell) != 0)
  322. {
  323. displayContact(&uContacts[i]);
  324. count++;
  325. }
  326. }
  327. displayContactFooter(count);
  328. }
  329.  
  330. //function for searching for contacts by phone number
  331. //user enters the phone number for a (existing) contact to display their information
  332. void searchContacts(const struct Contact uContacts[], int size)
  333. {
  334. char cellnum[11];
  335. int foundIndex;
  336.  
  337. printf("\nEnter the cell number for the contact: "); //asks the user for the contacts cell phone number they wish to views information
  338. getTenDigitPhone(cellnum);
  339. foundIndex = findContactIndex(uContacts, MAXCONTACTS, cellnum);
  340.  
  341. if (foundIndex == -1) {
  342. printf("*** Contact NOT FOUND ***\n"); //if the user enters a phone number not in the system then the contact is not found
  343. printf("\n");
  344. }
  345. else {
  346. printf("\n");
  347. displayContact(&uContacts[foundIndex]);
  348. printf("\n");
  349. }
  350. }
  351.  
  352. //function for adding new contacts into the system
  353. //runs the contact.c source file to add contacts unless the maximum (5) is reached
  354. void addContact(struct Contact uContacts[], int size)
  355. {
  356. int i;
  357. for (i = 0; i < size; i++)
  358. {
  359. if (strlen(uContacts[i].numbers.cell) == 0) {
  360. break;
  361. }
  362. }
  363. if (i == size) printf("\n*** ERROR: The contact list is full! ***\n\n"); //if the system already has 5 contacts then this error is displayed
  364. else {
  365. printf("\n");
  366. getContact(&uContacts[i]);
  367. printf("--- New contact added! ---\n"); //otherwise, runs the contact.c file to get information on the new contact and adds it to the program
  368. printf("\n");
  369. }
  370. }
  371.  
  372. //function for updating existing contacts
  373. //Asks the user to input the phone number for the contact they wish to update
  374. void updateContact(struct Contact uContacts[], int size)
  375. {
  376. char cellnum[11];
  377. int foundIndex;
  378. int yn;
  379.  
  380. //Asks the user for the contacts cell phone number
  381. printf("\nEnter the cell number for the contact: ");
  382. getTenDigitPhone(cellnum);
  383.  
  384. foundIndex = findContactIndex(uContacts, MAXCONTACTS, cellnum);
  385. if (foundIndex == -1) printf("*** Contact NOT FOUND ***\n\n"); //if the user does not enter a correct phone number (existing contact)
  386. else {
  387. printf("\nContact found:\n"); //if the user does enter a correct phone number (existing contact)
  388. displayContact(&uContacts[foundIndex]);
  389. printf("\n");
  390. printf("Do you want to update the name? (y or n): "); //Asks the user if they want to update the name, if they do then run getName function
  391. yn = yes();
  392. if (yn == 1) getName(&uContacts[foundIndex].name);
  393. printf("Do you want to update the address? (y or n): "); //Asks the user if they want to update the contacts address, if they do then run getAddress function
  394. yn = yes();
  395. if (yn == 1) getAddress(&uContacts[foundIndex].address);
  396. printf("Do you want to update the numbers? (y or n): "); //Asks the user if they want to update the conacts phone numbers, if they do then run getNumbers function
  397. yn = yes();
  398. if (yn == 1) getNumbers(&uContacts[foundIndex].numbers);
  399. printf("--- Contact Updated! ---\n");
  400. printf("\n");
  401. }
  402. }
  403.  
  404. //this function asks for the contacts phone number you wish to delete
  405. //the user must input the correct phone number (of an existing contact) in order to delete it
  406. void deleteContact(struct Contact uContacts[], int size)
  407. {
  408. char cellnum[11];
  409. int foundIndex;
  410. int yn;
  411.  
  412. //Asks user for the contacts cell phone number they wish to delete
  413. printf("\nEnter the cell number for the contact: ");
  414. getTenDigitPhone(cellnum);
  415. foundIndex = findContactIndex(uContacts, MAXCONTACTS, cellnum);
  416.  
  417. if (foundIndex == -1) printf("*** Contact NOT FOUND ***\n\n"); //if the user enters the wrong number then the contact is not found
  418. else {
  419. printf("\nContact found:\n"); //if the user enters the correct number (existing contact) then the contact IS found
  420. displayContact(&uContacts[foundIndex]);
  421. printf("\n");
  422. printf("CONFIRM: Delete this contact? (y or n): "); //conformation before deleting the contact ( double check the user is content on their choice)
  423. yn = yes();
  424. if (yn == 1)
  425. {
  426. uContacts[foundIndex].numbers.business[0] = '\0'; //removes all the phone numbers for the contact removing it from the contact list
  427. uContacts[foundIndex].numbers.cell[0] = '\0';
  428. uContacts[foundIndex].numbers.home[0] = '\0';
  429. printf("--- Contact deleted! ---\n");
  430. printf("\n");
  431. }
  432. }
  433. }
  434.  
  435. //Optional function for sorting contacts by phone number, using an array - this function gathers the information of every
  436. //contacts cellphone number and sorts its numerically
  437. void sortContacts(struct Contact uContact[], int size)
  438. {
  439. int i; //variable i for counting array
  440. int j; //variable j for sorting array
  441. struct Contact temp;
  442. for (i = 0; i < size - 1; i++)
  443. {
  444. for (j = i + 1; j < size; j++)
  445. {
  446. if (strlen(uContact[i].numbers.cell) != 0 && strlen(uContact[j].numbers.cell) != 0) {
  447. if (strcmp(uContact[i].numbers.cell, uContact[j].numbers.cell) > 0) {
  448. temp = uContact[i];
  449. uContact[i] = uContact[j];
  450. uContact[j] = temp;
  451. }
  452. }
  453. }
  454. }
  455. printf("\n--- Contacts sorted! ---\n");
  456. printf("\n");
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement