Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Hunter Wright
- hwright5
- 104039185
- IPC144 - SYY
- hwright5@myseneca.ca
- Assignment 2 - Milestone 4
- 2018-11-22
- */
- /*
- This source file contains all of the helping code for the entire program. It states all of the functions from contactHelpers.h
- 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
- clearKeyboard doesnt have to be re-coded (or copy and pasted) numerous times.
- */
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include "contactHelpers.h"
- #define MAXCONTACTS 5
- // Function Definitions
- // Clear the standard input buffer
- //this function creates a 'buffer' between each segment of the program
- void clearKeyboard(void)
- {
- //clear the buffer
- while (getchar() != '\n');
- }
- // pause: Empty function definition goes here:
- //this pause function clears the keyboard and the program from any function that was just occuring
- void pause(void)
- {
- printf("(Press Enter to Continue)");
- clearKeyboard();
- printf("\n");
- }
- // getInt: Empty function definition goes here:
- //getInt gathers information from the user in integer(number) values
- //if the user enters anything other than an integer number, then it outputs an error and asks the user to try again
- int getInt(void)
- {
- int value;
- char NL = 'x';
- scanf(" %d%c", &value, &NL);
- while (NL != '\n')
- {
- clearKeyboard();
- printf("*** INVALID INTEGER *** <Please enter an integer>: ");
- scanf(" %d%c", &value, &NL);
- }
- return value;
- }
- // getIntInRange: Empty function definition goes here:
- //this function works similar to getInt, but similar to if/else statements it has restrictions, if the user enters any number
- //outside what the restrictions are set to, then the programm outputs an error and asks the user to try again
- int getIntInRange(int inferiorLimit, int superiorLimit)
- {
- int iLoc = getInt();
- while ((iLoc < inferiorLimit) || (iLoc > superiorLimit))
- {
- printf("*** OUT OF RANGE *** <Enter a number between %d and %d>: ", inferiorLimit, superiorLimit);
- iLoc = getInt();
- }
- return iLoc;
- }
- // getIntPositive: Empty function definition goes here:
- //this function is used specifically for the contacts address portion of the program, where if the user enters a number that is negative
- //then it outputs an error message - similar to the getIntInRange function
- int getIntPositiveStreet(int inferiorLimit, int superiorLimit)
- {
- int iLoc = getInt();
- while ((iLoc < inferiorLimit) || (iLoc > superiorLimit))
- {
- printf("Please enter the contact's street number: ");
- iLoc = getInt();
- }
- return iLoc;
- }
- // getIntPositive: Empty function definition goes here:
- //this function is used specifically for the contacts address portion of the program, where if the user enters a number that is negative
- //then it outputs an error message - similar to the getIntInRange function
- int getIntPositiveApartment(int inferiorLimit, int superiorLimit)
- {
- int iLoc = getInt();
- while ((iLoc < inferiorLimit) || (iLoc > superiorLimit))
- {
- printf("Please enter the contact's apartment number: ");
- iLoc = getInt();
- }
- return iLoc;
- }
- // yes: Empty function definition goes here:
- //this yes function is used for yes or no questions
- //exmaple: Would you like to enter a middle inital(s)? (y)es or (n)o
- int yes(void)
- {
- //variable response is used as a flag to determine whether or not the reponse is valid
- //if the user enters the correct response then the value == 1/0, and the program may move forward, otherwise
- //response == -1 and it is an error, and the program will not accept that
- char lChar = 'x';
- char lChar2 = 'x';
- int response = -1;
- scanf(" %c%c", &lChar, &lChar2);
- //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
- while (!((lChar2 == '\n') && ((lChar == 'Y') || (lChar == 'y') ||
- (lChar == 'n') || (lChar == 'N'))))
- {
- clearKeyboard();
- printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
- scanf(" %c%c", &lChar, &lChar2);
- }
- //exmaple of response == 1/0 or -1, to determine whether or not the program will accept the anser
- if ((lChar == 'Y') || (lChar == 'y')) {
- response = 1;
- }
- else if ((lChar == 'N') || (lChar == 'n')) {
- response = 0;
- }
- return response;
- }
- // menu: Empty function definition goes here:
- //this menu function displays the list of every function available in the contact management system function
- int menu(void)
- {
- printf("Contact Management System\n");
- printf("-------------------------\n");
- printf("1. Display contacts\n");
- printf("2. Add a contact\n");
- printf("3. Update a contact\n");
- printf("4. Delete a contact\n");
- printf("5. Search contacts by cell phone number\n");
- printf("6. Sort contacts by cell phone number\n");
- printf("0. Exit\n");
- printf("\n");
- printf("Select an option:> ");
- return getIntInRange(0, 6); //restrictions set so the user must enter an option listed
- printf("\n");
- }
- // ContactManagerSystem: Empty function definition goes here:
- //this function is for the (not fully implemented) contact manager system, which will allow the user to edit, enter new, and sort
- //the contacts they have input into the system
- void contactManagerSystem(void)
- {
- struct Contact uContacts[MAXCONTACTS] =
- {
- //Manually entering and giving value to these initial contacts, used for the testing of adding, updating and deleting contacts
- {
- { "Rick", { '\0' }, "Grimes" },
- { 11, { '\0' }, "Trailer Park", "A7A 2J2", "King City" },
- { "4161112222", "4162223333", "4163334444" }
- },{
- { "Maggie", "R.", "Greene" },
- { 55, { '\0' }, "Hightop House", "A9A 3K3", "Bolton" },
- { "9051112222", "9052223333", "9053334444" }
- },{
- { "Morgan", "A.", "Jones" },
- { 11, { '\0' }, "Cottage Lane", "C7C 9Q9", "Peterborough" },
- { "7051112222", "7052223333", "7053334444" }
- },{
- { "Sasha", { '\0' }, "Williams" },
- { 55, { '\0' }, "Hightop House", "A9A 3K3", "Bolton" },
- { "9052223333", "9052223333", "9054445555" }
- },
- };
- //Switch cases for each of the different options in the contact management system (maximum 5 contacts)
- int selection;
- int yn = 0;
- while (yn == 0) {
- selection = menu();
- switch (selection)
- {
- case 1:
- displayContacts(uContacts, MAXCONTACTS);
- pause();
- break;
- case 2:
- addContact(uContacts, MAXCONTACTS);
- pause();
- break;
- case 3:
- updateContact(uContacts, MAXCONTACTS);
- pause();
- break;
- case 4:
- deleteContact(uContacts, MAXCONTACTS);
- pause();
- break;
- case 5:
- searchContacts(uContacts, MAXCONTACTS);
- pause();
- break;
- case 6:
- sortContacts(uContacts, MAXCONTACTS);
- pause();
- break;
- case 0:
- printf("\nExit the program? (Y)es/(N)o: "); //Asks user if they would wish to exit the program
- yn = yes();
- printf("\n");
- if (yn)printf("Contact Management System: terminated\n"); //exit statement
- break;
- default:
- break;
- }
- }
- }
- //function that displays as a header before the list of all the contacts when the user inputs to display them
- void displayContactHeader(void)
- {
- printf("\n");
- printf("+-----------------------------------------------------------------------------+\n");
- printf("| Contacts Listing |\n");
- printf("+-----------------------------------------------------------------------------+\n");
- }
- //Footer function that displays the total contacts in the system, just underneath a line separating from the list of contacts
- void displayContactFooter(int total)
- {
- printf("+-----------------------------------------------------------------------------+\n");
- printf("Total contacts: %d\n\n", total);
- }
- //Function for gathering the mandatory phone number information for each contact, the number must be 10 digits and cannot
- //contain symbols, dashes, uderlines, or characters from the alphabet, otherwise it will not accept the number
- void getTenDigitPhone(char telNum[])
- {
- int needInput = 1;
- int i;
- while (needInput == 1) {
- scanf("%10s", telNum);
- clearKeyboard();
- if (strlen(telNum) == 10) {
- needInput = 0;
- for (i = 0; i <= 9; i++) {
- 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) {
- needInput = 1;
- }
- }
- if (needInput == 1) {
- printf("Enter a 10-digit phone number: ");
- }
- }
- else {
- printf("Enter a 10-digit phone number: ");
- }
- }
- }
- //search index function for when the user wants to find contacts
- //when the user enters a phone number to search for a contact, this function scans each of the contacts for the phone number provided
- int findContactIndex(const struct Contact uContacts[], int index, const char cellNum[])
- {
- int i = 0;
- for (i = 0; i <= index; i = i + 1) {
- if (strcmp(cellNum, uContacts[i].numbers.cell) == 0) {
- return i;
- }
- }
- return -1;
- }
- //function that displays the direct information about each contact in the system
- //displays all the information that is known about each contact (unless not require/input by the user)
- void displayContact(const struct Contact* uContact)
- {
- printf(" %s ", uContact->name.firstName);
- if (strlen(uContact->name.middleInitial) != 0) printf("%s ", uContact->name.middleInitial);
- printf("%s\n", uContact->name.lastName);
- printf(" C: %-10s H: %-10s B: %-10s\n", uContact->numbers.cell, uContact->numbers.home, uContact->numbers.business);
- printf(" %d %s, ", uContact->address.streetNumber, uContact->address.street);
- if (uContact->address.apartmentNumber > 0) printf("Apt# %d, ", uContact->address.apartmentNumber);
- printf("%s, %s\n", uContact->address.city, uContact->address.postalCode);
- }
- //unlike the displayContact function, this one is disigned for ordering and logistics of displaying the contacts
- //Takes the information from the displayContact function and then counts to keep displaying for each member in the system
- void displayContacts(const struct Contact uContacts[], int size)
- {
- int i;
- int count = 0;
- displayContactHeader();
- for (i = 0; i < size; i++)
- {
- if (strlen(uContacts[i].numbers.cell) != 0)
- {
- displayContact(&uContacts[i]);
- count++;
- }
- }
- displayContactFooter(count);
- }
- //function for searching for contacts by phone number
- //user enters the phone number for a (existing) contact to display their information
- void searchContacts(const struct Contact uContacts[], int size)
- {
- char cellnum[11];
- int foundIndex;
- printf("\nEnter the cell number for the contact: "); //asks the user for the contacts cell phone number they wish to views information
- getTenDigitPhone(cellnum);
- foundIndex = findContactIndex(uContacts, MAXCONTACTS, cellnum);
- if (foundIndex == -1) {
- printf("*** Contact NOT FOUND ***\n"); //if the user enters a phone number not in the system then the contact is not found
- printf("\n");
- }
- else {
- printf("\n");
- displayContact(&uContacts[foundIndex]);
- printf("\n");
- }
- }
- //function for adding new contacts into the system
- //runs the contact.c source file to add contacts unless the maximum (5) is reached
- void addContact(struct Contact uContacts[], int size)
- {
- int i;
- for (i = 0; i < size; i++)
- {
- if (strlen(uContacts[i].numbers.cell) == 0) {
- break;
- }
- }
- 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
- else {
- printf("\n");
- getContact(&uContacts[i]);
- printf("--- New contact added! ---\n"); //otherwise, runs the contact.c file to get information on the new contact and adds it to the program
- printf("\n");
- }
- }
- //function for updating existing contacts
- //Asks the user to input the phone number for the contact they wish to update
- void updateContact(struct Contact uContacts[], int size)
- {
- char cellnum[11];
- int foundIndex;
- int yn;
- //Asks the user for the contacts cell phone number
- printf("\nEnter the cell number for the contact: ");
- getTenDigitPhone(cellnum);
- foundIndex = findContactIndex(uContacts, MAXCONTACTS, cellnum);
- if (foundIndex == -1) printf("*** Contact NOT FOUND ***\n\n"); //if the user does not enter a correct phone number (existing contact)
- else {
- printf("\nContact found:\n"); //if the user does enter a correct phone number (existing contact)
- displayContact(&uContacts[foundIndex]);
- printf("\n");
- 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
- yn = yes();
- if (yn == 1) getName(&uContacts[foundIndex].name);
- 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
- yn = yes();
- if (yn == 1) getAddress(&uContacts[foundIndex].address);
- 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
- yn = yes();
- if (yn == 1) getNumbers(&uContacts[foundIndex].numbers);
- printf("--- Contact Updated! ---\n");
- printf("\n");
- }
- }
- //this function asks for the contacts phone number you wish to delete
- //the user must input the correct phone number (of an existing contact) in order to delete it
- void deleteContact(struct Contact uContacts[], int size)
- {
- char cellnum[11];
- int foundIndex;
- int yn;
- //Asks user for the contacts cell phone number they wish to delete
- printf("\nEnter the cell number for the contact: ");
- getTenDigitPhone(cellnum);
- foundIndex = findContactIndex(uContacts, MAXCONTACTS, cellnum);
- if (foundIndex == -1) printf("*** Contact NOT FOUND ***\n\n"); //if the user enters the wrong number then the contact is not found
- else {
- printf("\nContact found:\n"); //if the user enters the correct number (existing contact) then the contact IS found
- displayContact(&uContacts[foundIndex]);
- printf("\n");
- printf("CONFIRM: Delete this contact? (y or n): "); //conformation before deleting the contact ( double check the user is content on their choice)
- yn = yes();
- if (yn == 1)
- {
- uContacts[foundIndex].numbers.business[0] = '\0'; //removes all the phone numbers for the contact removing it from the contact list
- uContacts[foundIndex].numbers.cell[0] = '\0';
- uContacts[foundIndex].numbers.home[0] = '\0';
- printf("--- Contact deleted! ---\n");
- printf("\n");
- }
- }
- }
- //Optional function for sorting contacts by phone number, using an array - this function gathers the information of every
- //contacts cellphone number and sorts its numerically
- void sortContacts(struct Contact uContact[], int size)
- {
- int i; //variable i for counting array
- int j; //variable j for sorting array
- struct Contact temp;
- for (i = 0; i < size - 1; i++)
- {
- for (j = i + 1; j < size; j++)
- {
- if (strlen(uContact[i].numbers.cell) != 0 && strlen(uContact[j].numbers.cell) != 0) {
- if (strcmp(uContact[i].numbers.cell, uContact[j].numbers.cell) > 0) {
- temp = uContact[i];
- uContact[i] = uContact[j];
- uContact[j] = temp;
- }
- }
- }
- }
- printf("\n--- Contacts sorted! ---\n");
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement