Thread: Problem with my address book program. (Delete function)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    Problem with my address book program. (Delete function)

    Okay, I got 3 problems remaining(listing, editing and deleting) but I'll focus on the deleting function first. Okay, so here's my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define max_con 30
    
    int add_contact();
    void edit_contact();
    int del_contact(int);
    void list_contact(int);
    
    char addressbook[5][30][30];
    char dummy[5][30][30]; //will use this for editing part
    char mode;
    int address_count = 0, current_record = 0;
    
    int main(void)
    {    
        int add = 0;
        do
        {
        printf("address count: %d, current record(array): %d \n", address_count, current_record);
        printf("Welcome: This is an address book program brought to you by\nJohn Ranniel G. Sison\n");
        printf("\n          Select the mode you want\n");
        printf("            1 -> Add contact\n"); 
        printf("            2 -> Edit contact\n");
        printf("            3-> Delete contact\n");
        printf("            4 -> List contact\n");
        printf("            5 -> Exit the program\n");
        scanf("%d", &mode);
        
            switch(mode){ //switch (determine which action to take)
        
            case 1:
            add_contact();
            break;
            case 2:
            edit_contact();
            break;
            case 3:
            del_contact(address_count);
            break;
            case 4:
            list_contact(address_count);
            break;
            case 5:
            return 0;
            default:
                while(mode<5&&mode>=1)
                printf("Please choose from the given options.\n");
                scanf("%c", &mode);
            } 
        } 
        while(mode != 5);
         
         
    }
    
    int add_contact()
    {
        char entry[30];
        int i, length, flag;
        if(mode==1)
            if (address_count == max_con){ //checks if the number of contacts is has not yet reached the maximum which is max_con
            printf("Sorry, the %d entry limit has been reached.\nChoose other functions other than '1'", max_con);
            return;
            }
            else {
            address_count++; //variable for number of entries
            current_record = address_count-1; //variable for the current entry
            length = strlen(addressbook[0][current_record]);
            }
           
       //asks the user to input the entry's first name (Alphabet, spaces) and it must not exceed 20 characters    
    
        do
        {
            printf("Enter the first name:"); //asks the user to input the first name of the entry
            scanf("%s", addressbook[0][current_record]);
            length = strlen(addressbook[0][current_record]); //gets the length of the input (should be less than 20)
                for(i=0; i<=20; i++)
                {
                    if((isalpha(addressbook[0][current_record][i]))||(addressbook[0][current_record][i]==' ')&&length<20)
                    { //checks if the input characters in first name are alphabets/a space and strlen is less than 20
                        flag = 1;
                        break; //
                    }//exits once it sees that the characters are alpha numeric
                    else 
                    {
                        flag = 0;
                        printf("%d", flag);
                        printf("\nYou can only use alphabets and space and a maximum of 20 characters.\nFirst name?\n"); //puts a message that prompts the user to enter alphabets/space alone
                        break;
                    }  
                } 
        } //end do
        while(flag!=1);
            
      //asks the user to input the surname of the entry (Alphabet, spaces) and it must not exceed 20 characters
        flag = 0;
        do
        {
            printf("%d", flag);
            printf("Enter the surname:"); 
            scanf(" %s", addressbook[1][current_record]);
            length = strlen(addressbook[1][current_record]); //gets the length of the input (should be less than 20)
                for(i=0; i<=20; i++)
                {
                    if((isalpha(addressbook[1][current_record][i]))||(addressbook[1][current_record][i]==' ')&&(length<20))
                    { //checks if the input characters in surname are alphabets/a space and strlen is less than 20
                        
                        flag = 1;
                        break; 
                    } //exits once it sees that the characters are alpha numeric
                    else 
                    {
                        flag = 0;
                        printf("%d", flag);
                        printf("\nYou can only use alphabets and space and a maximum of 20 characters.\nSurname?\n"); //puts a message that prompts the user to enter alphabets/space alone
                        break;
                    }    
                } 
        } 
        while(flag!=1); 
        
        //asks the user to input the degree program of the entry(Alphabet, spaces) and it must not exceed 20 characters
        
        do{
            printf("Enter the Degree Program the entry's in:"); 
            scanf("%s", addressbook[2][current_record]);  
            length = strlen(addressbook[2][current_record]); //gets the length of the input (should be less than 20)
                for(i=0; i<=20; i++)
                {
                    if((isalpha(addressbook[2][current_record][i]))||(addressbook[2][current_record][i]==' ')&&length<20)
                    { //checks if the input characters in the degree program are alphabets/a space and strlen is less than 20
                        flag = 1;
                        break; 
                    } //exits once it sees that the characters are alpha numeric
                    else 
                    {
                        flag = 0;
                        printf("\nYou can only use alphabets and space and a maximum of 20 characters.\nCourse?\n"); //puts a message that prompts the user to enter alphabets/space alone
                        break;
                    } 
                } 
        } 
        
        while(flag!=1);
        
        do
        {
            printf("Enter the entry's Student ID:");
            scanf("%s", addressbook[3][current_record]);
            length = strlen(addressbook[3][current_record]); //gets the length of the input (should be 10)
            addressbook[3][current_record];
                for(i=0; i<10; i++)
                {
                    if((addressbook[3][current_record][i]>='0')&&(addressbook[3][current_record][i]<'4')||(addressbook[3][current_record][i]>'4')&&(addressbook[3][current_record][i]<=9))
                    {
                      //checks if the input characters are only numbers and the 4th element of the array is a '-' 
                        if((addressbook[3][current_record][4]=='-')&&(strlen(addressbook[3][current_record])==10))
                        { //checks if the input characters are only numbers and the 4th element of the array is a '-' 
                            flag = 1;
                            break;
                        }
                        else 
                        { //prints restrictions/parameters for the student ID
                            printf("The format of your input should be like this:\nxxxx-yyyyy\nWhere: the x's and y's represent ONLY numbers");
                            break; 
                        } 
                    }
                } 
        }
        while(flag!=1);
        
        flag = 0;
        do
        { 
        printf("Enter the entry's cellphone number:");
            scanf("%s", addressbook[4][current_record]);
            length = strlen(addressbook[4][current_record]); //gets the length of the input (should be 10)
            //checks if the input is composed of 11 elements wherein the first 2 are 0 and 9 respectively
                for(i=0; i<11; i++){
                    if((addressbook[4][current_record][0] == '0')&&addressbook[4][current_record][1] == '9'&&(addressbook[4][current_record][i]>='0')&&(addressbook[4][current_record][i]<='9')&&(length)==11)
                    {   
                        flag = 1;
                        break;
                    }
                    else 
                    { //prints restrictions/parameters for the cellphone number
                        printf("The format of the input is: 09xxyyyyyyy\nwherein x and y are integers from 0 -> 9\n");
                        break; 
                    } 
            } 
        } 
        while(flag!=1);
        
        flag = 0;
        do  
        {
            printf("Enter the entry's email address:");
            scanf("%s", addressbook[5][current_record]);
            length = strlen(addressbook[5][current_record]); //gets the length of the input (should be 10)
            //checks if input is composed of letters//digits/underscores/dots/@ &max of 30 characters
                for(i=0; i<30; i++){
                    if((addressbook[5][current_record][i]>='A')&&(addressbook[5][current_record][i]<='z')||(addressbook[5][current_record][i]=='.')||
                    (addressbook[5][current_record][i]=='@')||(addressbook[5][current_record][i]=='_')||(addressbook[5][current_record][i]>='0')&&(addressbook[5][current_record][i]<='9')&&length<=30)
                  {
                       flag = 1;
                       break;
                  }
                
                    else 
                    { //prints restrictions/parameters for the cellphone number
                        printf("The email address entry can only be composed of letters, numbers, '.', '_' and '@' \n");
                        break; 
                    } 
            } 
        } //end do
        while(flag!=1);
          
        return address_count;
    } //end add_contact function
    
    void edit_contact()
    {
        char j, k, flag = 0, length;
        int i;
        int num, inp;
        
        printf("Edit which entry?\n");
        printf("------------------\n");
        printf("Enter the number/position of the entry you want to edit:");
        printf("For example:\nIf you want to edit the first entry type '1'\n");
        scanf("%d", &inp); 
        //edits first name of entry
        num = inp - 1; //will access the position in the array so it's n-1 since array starts with element 1 as 0
        do
        {
            printf("%d", flag);
            printf("Edit the first name:"); //asks the user to input the first name of the entry
            scanf("%s", addressbook[0][num]);
            length = strlen(addressbook[0][num]); //gets the length of the input (should be less than 20)
           
                for(i=0; i<=20; i++)
                {     
                    if((isalpha(addressbook[0][num][i]))||(addressbook[0][num][i]==' ')&&length<20)
                    { //checks if the input characters in first name are alphabets/a space and strlen is less than 20
                        flag = 1;
                        break; 
                        
                    } //exits once it sees that the characters are composed of alphabets/spaces
                    else 
                    {
                        printf("You can only use alphabets and space and a maximum of 20 characters. First name?\n"); //puts a message that prompts the user to enter alphabets/space alone
                        break;
                    } //end else    
                } 
            
        
        } 
        while(flag!=1);
      //edits surname
      //asks the user to input the surname of the entry (Alphabet, spaces) and it must not exceed 20 characters
        flag = 0;
        do
        {
            printf("%d", flag);
            printf("Edit the surname:"); 
            scanf("%s", addressbook[1][num]);
            length = strlen(addressbook[1][num]); //gets the length of the input (should be less than 20)
                for(i=0; i<=20; i++)
                {
                    if((isalpha((addressbook[1][num][i]))||addressbook[1][num][i]==' ')&&length<20)
                    { //checks if the input characters in surname are alphabets/a space and strlen is less than 20
                        flag = 1;
                        break; //
                    } //exits once it sees that the characters are composed of alphabets/spaces
                    else 
                    {
                        printf("You can only use alphabets and space and a maximum of 20 characters. First name?\n"); //puts a message that prompts the user to enter alphabets/space alone
                        break;
                        } //end else    
                } // end for
        } //end do
        while(flag!=1); 
        //edits the course
        //asks the user to input the degree program of the entry(Alphabet, spaces) and it must not exceed 20 characters
        do{
            printf("Enter the Degree Program the entry's in:"); 
            scanf("%s", addressbook[2][num]);  
            length = strlen(addressbook[2][num]); //gets the length of the input (should be less than 20)
                for(i=0; i<=20; i++){
                    if((isalpha((addressbook[2][num][i]))||addressbook[2][num][i]==' ')&&length<20)
                    { //checks if the input characters in the degree program are alphabets/a space and strlen is less than 20
                        flag = 1;
                    break; 
                    } //exits once it sees that the characters are composed of alphabets and spaces
                    else 
                    {
                        printf("You can only use alphabets and space and a maximum of 20 characters. First name?\n"); //puts a message that prompts the user to enter alphabets/space alone
                        break;
                    } //end else    
                } // end for
        } //end do
        
        while(flag!=1);
        // edits student ID of entrynumber
        flag = 0;
        do
        {
            printf("Edit the entry's Student ID:");
            scanf("%s", addressbook[3][num]);
            length = strlen(addressbook[3][num]); //gets the length of the input (should be 10)
            addressbook[3][current_record];
                for(i=0; i<10; i++)
                {
                    if((addressbook[3][num][i]>='0')&&(addressbook[3][num][i]<'4')||(addressbook[3][num][i]>'4')&&(addressbook[3][num][i]<=9))
                    {
                      //checks if the input characters are only numbers and the 4th element of the array is a '-' 
                    if((addressbook[3][num][4]=='-')&&(strlen(addressbook[3][num])=='10'))
                    { //checks if the input characters are only numbers and the 4th element of the array is a '-' 
                        flag = 1;
                    break;
                    }
                    else 
                    { //prints restrictions/parameters for the student ID
                    printf("The format of your input should be like this:\nxxxx-yyyyy\nWhere: the x's and y's represent ONLY numbers");
                    break; 
                    } //end else
                    }   
                } //end for
        } // end do
        while(flag!=1);
       //edit cellphone number
        flag = 0; 
        do
        { 
        printf("Edit the entry's cellphone number:");
            scanf("%s", addressbook[4][num]);
            length = strlen(addressbook[4][num]); //gets the length of the input (should be 10)
            //checks if the input is composed of 11 elements wherein the first 2 are 0 and 9 respectively
                for(i=0; i<11; i++){
                    if ((addressbook[4][num][0] == '0') && (addressbook[4][num][1] == '9') || (addressbook[4][num][i]>='0') && (addressbook[4][num][i]<='9'))
                    {
                        if(length == 11){
                            flag = 1;
                            break;                        
                        }
                    }
                 
                    else { //prints restrictions/parameters for the cellphone number
                    printf("The format of the input is: 09xxyyyyyyy\nwherein x and y are integers from 0 -> 9\n");
                    break; } //end else
            } //end for
        } // end do
        while(flag!=1); 
        
       //edit email address 
        flag = 0;
        do
        { 
        printf("Enter the entry's email address:");
            scanf("%s", addressbook[5][num]);
            length = strlen(addressbook[5][num]); 
            
            //checks if input is composed of letters//digits/underscores/dots/@ &max of 30 characters
                for(i=0; i<30; i++){
                if((addressbook[5][num][i]>='A')&&(addressbook[5][num][i]<='z')||(addressbook[5][num][i]=='.')||
                (addressbook[5][num][i]=='@')||(addressbook[5][num][i]=='_')||(addressbook[5][num][i]>='0')||(addressbook[5][num][i]<='9')&&length<=30){
                       flag = 1;
                    break;
                    }
                //prints restrictions/parameters for the cellphone number
                    else { 
                    printf("The email address entry can only be composed of letters, numbers, '.', '_' and '@' \n");
                    break; } 
            } 
        } 
        while(flag!=1);     
       
    } //end edit_contact
    
    int del_contact(int address_count)
    { 
       int get, del, i;
       address_count--; //since you're gonna delete an entry
       
       printf("%s", addressbook[0][0]);
       printf("\nEnter the number of entry/contact you want to delete.\n");
       printf("For example:\nIf you want to delete your first input type '1'\n");
       printf("Contact number:");
       scanf("%d", &get);
       
       del = get-1;
       //for loop to assign '/0' as the content for the given entry
           for(i=0; i<6; i++)
           {
                strcpy(addressbook[i][del], addressbook[6][0]); //address book[6][0] has no value
           }
        //after deletion of the contents of the entries, add count and record are reset to 0
        
        return address_count;
    } //end delete function
    
    void list_contact(int address_count)
    {//start function
    int i, j, k;
        //prints the stored entries in add_contact
    
        printf("\nList all entries.\n"); 
        for(i=0; i<address_count; i++){
            printf("Entry number %d.\nName: %s %s\n", i+1, addressbook[0][i], addressbook[1][i]); //list the first name and surname input
            printf("Degree program: %s\n", addressbook[2][i]);
            printf("Student ID: %s\n", addressbook[3][i]);
            printf("Cellphone number: %s\n", addressbook[4][i]);
            printf("Email Address: %s\n\n", addressbook[5][i]);
        } //end for
    }
    It's kinda long. But my problem's with the address_count variable in the delete function. For example, when I (add) a contact, it goes up by 1. So basically, it keeps track of the number of contacts. But in my delete function it doesn't diminish by one when I delete a contact. How do I do it? Please help. Thankss!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unless you bring in the address of "address_count", you will be adjusting only a COPY of the actual address_count variable. So the adjustment will be lost when the function ends.

    int *address_count is what your parameter should be for the function, and the call should be &address_count, so add the & to that call (line #40). Same goes for any function that makes permanent changes to any parameter - it needs the address of it, to do it.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Doesn't work. D: could you please post an example pls?

  4. #4
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    38
    Quote Originally Posted by johnjrgs View Post
    Doesn't work. D: could you please post an example pls?
    As Adak said, you need a pointer as the function argument. A pass by value doesn't do the job.

    An example:

    Code:
    #include <stdio.h>
    
    void delContactByValue(int param_addressCounter)
    {
        --param_addressCounter;
    }
    
    
    void delContactByPtr(int *param_addressCounter)
    {
        --(*param_addressCounter);
    }
    
    
    int main(int argc, char *argv[])
    {
        int addressCounter = 5;
        printf("The address counter is: %d\n", addressCounter);
        
        delContactByValue(addressCounter);
        printf("The address counter is: %d\n", addressCounter);
        
        delContactByPtr(&addressCounter);
        printf("The address counter is: %d\n", addressCounter);
    
        return 0;
    }
    And the result is:

    Code:
    $ gcc -Wall testscript.c -o testscript
    $ ./testscript
    The address counter is: 5
    The address counter is: 5
    The address counter is: 4
    $

    Regards,
    Dariyoosh

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Great explanation Dariyoosh. Thanks! Now all that's left is the edit function.

    BTW, I found something wrong with the add_contact function. In the 4th part of the input where the user is asked to input the Student ID, it prints.
    "Enter the entry's Student ID twice. D: I don't know why.. I'll edit my main code.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Haven't looked too closely at the code but I did notice this:
    Code:
    ...
    char mode;
    int address_count = 0, current_record = 0;
    
    int main(void)
    {    
        int add = 0;
        do
        {
        printf("address count: %d, current record(array): %d \n", address_count, current_record);
        printf("Welcome: This is an address book program brought to you by\nJohn Ranniel G. Sison\n");
        printf("\n          Select the mode you want\n");
        printf("            1 -> Add contact\n"); 
        printf("            2 -> Edit contact\n");
        printf("            3-> Delete contact\n");
        printf("            4 -> List contact\n");
        printf("            5 -> Exit the program\n");
        scanf("%d", &mode);
    
        ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi guys. Address book problem:
    By johnjrgs in forum C Programming
    Replies: 6
    Last Post: 02-17-2013, 08:18 PM
  2. binarySearchTree delete function problem
    By tms43 in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 09:13 PM
  3. address book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-01-2002, 08:19 PM
  4. address book problem
    By datainjector in forum C Programming
    Replies: 13
    Last Post: 11-27-2002, 08:14 PM
  5. Address Book program
    By sundeeptuteja in forum C++ Programming
    Replies: 0
    Last Post: 07-28-2002, 02:08 AM