I am having trouble searching a Linked List for contacts, when the program goes to scan in the last name it reads it in over and over. I am wondering why it doesn't just take the name and continue after that? The problems I am having are in the:
void Search(struct contact *ptr) function. Here is the code:
And adding this which I though was the problem just causes it to go through but segment:Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> //global: struct contact *ptrFirst = NULL;//empty list (ptrFirst is the head of the list struct contact *ptrLast = NULL;//empty list (ptrLast is the tail of the list) //declare a structure called contact struct contact //contact struct for holding contact info { char firstname[40]; //holds the first name of ith contact char lastname[40]; //holds last name of ith contact char address[100]; //holds address of ith contact char postalcode[7]; //holds postalcode of ith contact char phone[10]; //holds phone of ith contact struct contact *ptrNext; // self referential pointer }; //Prototypes: struct contact *CreateContact(); void PrintList(struct contact *ptr); void ResetList(); struct contact *AddToEnd(struct contact *ptrNew); struct contact* InputRecord(struct contact *ptrNew); // used by Add to interactively get the values from the user int Save(const char* filename); int Load(const char* filename); char *WordCap(char *word); void Search(struct contact *ptr); int i=0; int main() { int run=1; int input=1; int result=0; int count=0; printf("*** Personal Contact Book v1.0 ***"); printf("\n");//newline for neatness while(run==1) { char temp[50]; char again[10]={'Y'}; //if after a contact is entered if will store if user wants to enter another one printf( "1. Add new contact\n" "2. Display current contacts\n" "3. Search for a contact\n" "4. Save contacts to file\n" "6. Delete an existing record\n" "7. Exit\n" ); scanf("%d", &input); fflush(stdin); //flush because scanf was used i=0; switch(input) { case 1: { while(again[0]=='Y') { AddToEnd(InputRecord(CreateContact())); printf("Add another contact?: ");//ask if they want to enter another contact gets(again);//scan in answer again[0]=toupper(again[0]);//make again cap so that it will correctly compare printf("\n");//newline for neatness } break; } case 2: { PrintList(ptrFirst); break; } case 3: { Search(ptrFirst); } case 4: { } case 5: { } case 6: { } case 7: { ResetList(ptrFirst); run = 0; break; } default: { printf("Invalid entry, try again\n\n"); break; } } } } //CreateContact creates using dynamic memory allocation a new record of type contact //Input: assumes enough dynamic memory is available //Output: returns a pointer to the enw contact, or NULL if not enough memory struct contact *CreateContact() { return (struct contact *)(malloc(sizeof(struct contact))); } //PrintList will print out the contents of the list void PrintList(struct contact *ptr) { if(ptr!=NULL) { printf("First name: %s\n",ptr->firstname); printf("Last name : %s\n",ptr->lastname); printf("Address : %s\n",ptr->address); printf("Postal Code: %s\n",ptr->postalcode); printf("Phone : %s\n",ptr->phone);//ask for phone printf("\n");//newline for neatness PrintList(ptr->ptrNext); } } void Search(struct contact *ptr) { char name[40]; printf("What is the contact's last name?: "); gets(name); strcpy(name,WordCap(name)); //copy name back into ith lastname after wordcap opperated on it while(ptr!=NULL) { if(strcmp(name,ptr->lastname)==0) { i++; } ptr->ptrNext; } printf("Found %d record(s):\n", i); } //ResetList will empty or free() the contents of the list void ResetList() { struct contact *ptrDel=ptrFirst; if (ptrDel != NULL) // empty list! nothing to erase... quit { ResetList(ptrDel->ptrNext); free(ptrDel); } } //AddToEnd will add a contact to the end of the list struct contact *AddToEnd(struct contact *ptrNew) { //case 1: ptrNew is not valid(ie. NULL - nothing to add!) if(ptrNew==NULL) return NULL; //case 2: add the record to an empty list if(ptrFirst==NULL) { ptrFirst=ptrNew; ptrLast=ptrNew; } //case 3: add the record to an existing list else { ptrLast->ptrNext = ptrNew; ptrLast = ptrNew; } } //InputRecord sets the initial values of a new record //Input: a pointer to an existing record //Output: sets the int value to a value from the keyboard, and null to all pointers // and returns the same pointer to that initialized record struct contact* InputRecord(struct contact *ptrNew) { if(ptrNew==NULL) //fail-safe: check for valid pointer return NULL; int valid=0; //used when determining if user entered a valid phone or postalcode printf("First name: "); gets(ptrNew->firstname); strcpy(ptrNew->firstname,WordCap(ptrNew->firstname)); //copy word back into ith firstname after wordcap opperated on it printf("Last name : "); gets(ptrNew->lastname); strcpy(ptrNew->lastname,WordCap(ptrNew->lastname)); //copy word back into ith lastname after wordcap opperated on it printf("Address : "); gets(ptrNew->address); printf("PostalCode: "); gets(ptrNew->postalcode); while(valid==0) //while not valid { //if all the correct positions are words and numbers respectivly it will become valid if(isalpha(ptrNew->postalcode[0]) && isdigit(ptrNew->postalcode[1]) && isalpha(ptrNew->postalcode[2]) && isdigit(ptrNew->postalcode[3]) && isalpha(ptrNew->postalcode[4]) && isdigit(ptrNew->postalcode[5])) { valid=9; //update valid if it is valid } else { printf("Invalid please re-enter PostalCode: "); //else tell user not valid gets(ptrNew->postalcode);//scan in again to get new postal code } } printf("Phone : ");//ask for phone gets(ptrNew->phone);//scan in phone valid=0;//assume not valid while(valid==0)//while not valid { //check if it is all digits if(isdigit(ptrNew->phone[0]) && isdigit(ptrNew->phone[1]) && isdigit(ptrNew->phone[2]) && isdigit(ptrNew->phone[3]) && isdigit(ptrNew->phone[4]) && isdigit(ptrNew->phone[5]) && isdigit(ptrNew->phone[6]) && isdigit(ptrNew->phone[7]) && isdigit(ptrNew->phone[8]) && isdigit(ptrNew->phone[9])) { valid=9;//update validity } else { printf("Invalid please re-enter Phone: ");//else print not valid gets(ptrNew->phone);//re scan in phone } } ptrNew->ptrNext=NULL; return ptrNew; } char *WordCap(char *word) { int i=1; //keeps track of how far in the word it is *word=toupper(*word); //capatalize the first char while(*(word+i)!='\0') //while not the end of the word { *(word+i)=tolower(*(word+i)); //it will make the rest lower case i++; //increment i } *(word+i)=tolower(*(word+i)); //it will make the rest lower case return word; //return now capatalized }
ptr=ptr->ptrNext;