Hey guys, back again with yet another "intelligent" question
So, I finished my linked list assignment to build a telephone directory, or phone book.
The teacher then gave us the second part, which is to provide the user with an option to modify the pre-existing phone numbers. Basically, you enter in a phone number, you print the list and realize you entered the phone number wrong. Instead of just deleting it and entering the all the information again, just modify the phone number alone.
This is what I know:
I know I have to ask the user to enter the old number and compare that number to the numbers in the list (I am using a strcmp function). If the numbers match, ask the user for the new number and replace the old with the new.
This is what I don't know:
How do I "link" the new entry to the same name and replace the corresponding phone number? I know I have to set the old list equal to the new list in order to replace the old list (right ) but I am just not getting it. I have tried it over and over and it is just kicking my A$$!!!
Here is my function:
Code:
void modify_list (void) // --> BEGIN modify_list FUNCTION
{
//// LOCAL DEFINITIONS
char area[MAXLEN]; /* area code to look for */
char phone[MAXLEN]; /* phone number to look for */
char new_area[MAXLEN]; /* new area number */
char new_phone[MAXLEN]; /* new phone number */
NODE *search_ptr; /* Pointer to search list for area code */
//// STATEMENTS
while (pList==NULL){
printf("\aNO NUMBERS IN LIST\n");
return;
}
printf ("Enter the area code of the telephone number you wish to modify and key ENTER: ");
fgets (area, MAXLEN, stdin);
area[strlen(area) -1] = '\0';
printf ("Enter the last 7 digits of the telephone number you wish to modify\n");
printf ("Enter in the format XXX-XXXX and key ENTER: ");
fgets (phone, MAXLEN, stdin);
phone[strlen(phone) -1] = '\0';
search_ptr= pList;
while (search_ptr != NULL) {
if ((strcmp (search_ptr->area, area) == 0) &&
(strcmp (search_ptr->phone, phone) == 0)) {
printf ("\nEnter new area code and key ENTER: ");
fgets (new_area, MAXLEN, stdin);
new_area[strlen(new_area) -1] = '\0';
printf ("Enter new phone number and key ENTER: ");
fgets (new_phone, MAXLEN, stdin);
new_phone[strlen(new_phone) -1] = '\0';
return;
}
search_ptr= search_ptr->link; /* Move to next item */
}
printf ("\aNUMBERS NOT FOUND\n");
return;
}
The area coded in red is where I am comparing the old with the new. right now, it works, but it just does replace the old number with the new entry (obviously because I am not telling it to, but that is my question).
Any thoughts?