Loops and structures

This is a discussion on Loops and structures within the C Programming forums, part of the General Programming Boards category; I am trying to do a swap. I have to use the -> operator. The purpose of this is to ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    14

    Loops and structures

    I am trying to do a swap. I have to use the -> operator. The purpose of this is to delete a contact in a phonebook. The previously mentioned operator has to be used because it is the easiest way to get the data to all functions.

    Code:
    for(i = 0; i < * ptrX; i++){
             if((strcmp(fn, pb[i]->firstName) ==0) && (strcmp(ln, pb[i]->lastName) ==0)){
                for(j = 0; j < * ptrX; j++){
                strcpy(pb[j]->firstName, pb[j+1]->firstName);
                strcpy(pb[j]->clastName, pb[j+1]->lastName);
                strcpy(pb[j]->homeNumber, pb[j+1]->homeNumber);
                strcpy(pb[j]->cellNumber, pb[j+1]->cellNumber);
                }
             }
        }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    I have to use the -> operator
    Are you sure? That would only be true if pb were an array of pointers to your structure.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    14

    Full code. Check out the Delfriend function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_NAME_LENGTH 40
    
    
    typedef struct {
        char cfirstName[MAX_NAME_LENGTH];
        char clastName[MAX_NAME_LENGTH];
        char chomeNumber[MAX_NAME_LENGTH];
        char ccellNumber[MAX_NAME_LENGTH];   
        char swap[MAX_NAME_LENGTH];
        char swap2[MAX_NAME_LENGTH];
        char swap3[MAX_NAME_LENGTH];
        char swap4[MAX_NAME_LENGTH];
           char * firstName;
           char * lastName;
           char * homeNumber;   
           char * cellNumber;
    } entry;
    
    
    typedef struct {
        entry *entries;
        int current_size;
        int count;
    } book; 
    
    
    //Phonebook
    
    
    void menu();
    void addfriend(entry * pb, book * xb, int * ptrX);
    void delfriend(entry * pb, book * xb, int * ptrX);
    void showfriend(entry * pb, book * xb, int * ptrX);
    void showpb(entry * pb, book * xb, int * ptrX);
    void set_firstName(entry * pb, book * xb, int * ptrX, char * firstName);
    void set_lastName(entry * pb, book * xb, int * ptrX, char * firstName);
    void set_homeNumber(entry * pb, book * xb, int * ptrX, char * firstName);
    void set_cellNumber(entry * pb, book * xb, int * ptrX, char * firstName);
    
    
    
    
    main()
    {
        entry * pb;
        book * xb;
        pb = (entry *)malloc(sizeof (entry) * 2);
        xb = (book *)malloc(sizeof (book) * 2);
        int option = 0;        
        int X = 0;
        int * ptrX;
        ptrX = &X;
        
        //Variables
            
            do{    
            menu();
            scanf("%d", &option);
          
            if(option == 1){addfriend(pb, xb, ptrX);}
            if(option == 2){delfriend(pb, xb, ptrX);}
            if(option == 3){showfriend(pb, xb, ptrX);}
            if(option == 4){showpb(pb, xb, ptrX);}
            if(option == 5){
                free(pb);
                free(xb);
                printf("Good Bye!");
                exit(0);
            }
            }
            while(1);
            //Functions for phonebook
            system("PAUSE");
    }
    
    
    //Menu
    void menu(){    
        printf("\nPhone Book Application\n");
        printf("1) Add friend\n");
        printf("2) Delete friend\n");
        printf("3) Show a friend\n");
        printf("4) Show phone book\n");
        printf("5) Quit\n\n\n");
        printf("What would you like to do?:");
    }     
    
    
    void set_firstName(entry * pb, book * xb, int * ptrX, char * firstName){
        int length = strlen(firstName);
        
    }
    
    
    void set_lastName(entry * pb, book * xb, int * ptrX, char * firstName){
        
    }
    
    
    void set_homeNumber(entry * pb, book * xb, int * ptrX, char * firstName){
        
    }
    
    
    void set_cellNumber(entry * pb, book * xb, int * ptrX, char * firstName){
        
    }
    
    
    //Addfriend Function
    void addfriend(entry * pb, book * xb, int * ptrX){
        realloc(pb, *ptrX * sizeof(entry)); 
    
    
        printf("First name: ");
        scanf("%s", pb->cfirstName);
        printf("Last name: ");
        scanf("%s", pb->clastName);
        printf("Phone number (home): ");
        scanf("%s", pb->chomeNumber);
        printf("Phone number (cell): ");
        scanf("%s", pb->ccellNumber);
       
        printf("....Record added to the phone book\n\n\n\n\n");
    
    
       *ptrX += 1;   
    }
    
    
    //Deletefriend Function
    void delfriend(entry * pb, book * xb, int * ptrX){
        int i = 0;
        int j = 0;
        char fn[20];
        char ln[20];
    
    
        printf("Firstname: ");
        scanf("%s", fn);
        printf("Lastname: ");
        scanf("%s", ln);
        printf("...Friend deleted\n");
        
        for(i = 0; i < * ptrX; i++){
             if((strcmp(fn, pb->cfirstName) ==0) && (strcmp(ln, pb->clastName) ==0)){
                strcpy(pb->swap, pb->cfirstName);
                strcpy(pb->swap, pb->clastName);
                strcpy(pb->swap, pb->chomeNumber);
                strcpy(pb->swap, pb->ccellNumber);
             }
        }
    
    
       *ptrX -= 1;
    }
    
    
    //Show friend Function
    void showfriend(entry * pb, book * xb, int * ptrX){
        int i = 0;
        int x = 0;
        char fn[20];
        char ln[20];
    
    
        printf("Firstname: ");
        scanf("%s", fn);
        printf("Lastname: ");
        scanf("%s", ln);    
      
                if((strcmp(fn, pb->cfirstName) ==0) && (strcmp(ln, pb->clastName) ==0)){
                    printf("\n      %s\n", pb->cfirstName);
                    printf("      %s\n", pb->clastName);
                    printf("(home)%s\n", pb->chomeNumber);
                    printf("(cell)%s\n", pb->ccellNumber);
                    x = 1;
                }
            if(x != 1){
                printf("\nSorry, there is nobody with that name in phonebook.\n");
            }
    
    
    }
    //Show phonebook Function
    void showpb(entry * pb, book * xb, int * ptrX){
        int i = 0;
        
        for(i = 0; i < *ptrX; i++){
               printf("\n      %s\n", pb->cfirstName);
               printf("      %s\n", pb->clastName);
               printf("(home)%s\n", pb->chomeNumber);
               printf("(cell)%s\n", pb->ccellNumber);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. loops and structures
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:11 AM
  4. Replies: 5
    Last Post: 04-11-2002, 11:29 AM
  5. combo for loops with if structures
    By Nova in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21