Thread: Program Using Structures To Organize Address List

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    8

    Question Program Using Structures To Organize Address List

    First C programming class and beginning to learn programming. I am currently learning how to use structures in C and this is a learning task to help understand this process.

    The program should accept an input of addresses and organize them by zipcode from lowest zipcode to highest zipcode.

    The program and input/output below is what I have so far. I would like to dump the data into a file using file redirection. For example:

    ConsoleApplication1.exe < input.txt > testout.txt

    Right now all that gets dumped into the test file is:


    Code:
    Processed 18 sets of data.
    Obviously because of the printf statement I have in the 'takedate' function to test the process.

    My question is, what would be the best way to take the input, process it, then dump the output in zipcode order into the file? Can anyone kindly recommend a function/statement that can be added in order to achieve this?


    Thank you so very much in advance for your help, time, and guidance in getting this program working!

    Code:
    
    
    
    //header file intiating other headers
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct Person{
        char name[50];
        char address[50];
        char citystate[30];
        char zip[10];
    }Person;
    
    
    int takedata(Person *arr[]);
    void sortdata(Person * arr[], int noElements);
    
    
    int main(void)
    {
        //Intiate program
        int noElements = 0;
        Person *arr[50];
    
    
        //call input function
        //input(work, &count);
        takedata(arr);
    
    
        //sort files
        //call function
        //swap(work, &count);
        sortdata(arr, &noElements);
    
    
        //call output function
        //output(work, &count);
    
    
        //end program
        return 0;
    }
    
    
    
    
    void sortdata(Person * arr[], int noElements) {
        /* temporary pointer to Person data type to aid with swapping */
        //Person * tempptr = (Person *)malloc(sizeof(Person));
    
    
        int i, j, compare;
        Person * tempptr = NULL;
    
    
        for (i = 0; i <= (noElements - 1); i++); {
            for (j = (i + 1); j <= noElements; j++) {
                compare = strcmp(arr[i] -> zip, arr[j] -> zip);
                if (compare > 0) {
                    printf("attempted sort %d times.\n", j);
                    /* stores value in index i for array inside of temporary pointer  */
                    tempptr = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tempptr;
                }
    
    
            }
        }
    }
    
    
    int takedata(Person *arr[])
    {
        /* counter variable */
        int i = 0;
        char teststring[25];
    
    
        while ((gets(teststring)) != NULL && i < 50)
        {
            /* dynamically allocates memory for each index of the array */
            arr[i] = (Person *)malloc(sizeof(Person));
    
    
            /* takes in data from user/ input file */
    
    
            strcpy(arr[i]->name, teststring);
            gets(arr[i]->address);
            gets(arr[i]->citystate);
            gets(arr[i]->zip);
    
    
            i++;
        }
    
    
    
    
    
    
        printf("Processed %d sets of data.\n\n", i);
    
    
        return (i - 1);
    }
    Input data being used:

    Code:
    A1, A220294 Lorenzana Dr
    Woodland Hills, CA
    91364
    B1, B2
    19831 Henshaw St
    Culver City, CA
    94023
    C1, C2
    5142 Dumont Pl
    Azusa, CA
    91112
    D1, D2
    20636 De Forest St
    Woodland Hills, CA
    91364
    A1, A2
    20294 Lorenzana Dr
    Woodland Hills, CA
    91364
    E1, E2
    4851 Poe Ave
    Woodland Hills, CA
    91364
    F1, F2
    20225 Lorenzana Dr
    Los Angeles, CA
    91111
    G1, G2
    20253 Lorenzana Dr
    Los Angeles, CA
    90005
    H1, H2
    5241 Del Moreno Dr
    Los Angeles, CA
    91110
    I1, I2
    5332 Felice Pl
    Stevenson Ranch, CA
    94135
    J1, J2
    5135 Quakertown Ave
    Thousand Oaks, CA
    91362
    K1, K2
    720 Eucalyptus Ave 105
    Inglewood, CA
    89030
    L1, L2
    5021 Dumont Pl
    Woodland Hills, CA
    91364
    M1, M2
    4819 Quedo Pl
    Westlake Village, CA
    91362
    I1, I2
    5332 Felice Pl
    Stevenson Ranch, CA
    94135
    I1, I2
    5332 Felice Pl
    Stevenson Ranch, CA
    94135
    N1, N2
    20044 Wells Dr
    Beverly Hills, CA
    90210
    O1, O2
    7659 Mckinley Ave
    Los Angeles, CA
    90001

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by st4evr
    My question is, what would be the best way to take the input, process it, then dump the output in zipcode order into the file? Can anyone kindly recommend a function/statement that can be added in order to achieve this?
    I recommend that you write functions that do one thing and do it well. For example, instead of takedata, I would have two functions:
    Code:
    /* Returns 1 if data for the person was read from standard input; otherwise return 0 */
    int read_person(Person *person);
    
    /* Returns the number of persons read from standard input in the list */
    int read_person_list(Person *person_list[], int list_max_size);
    Likewise, when you want to print the list:
    Code:
    /* Prints the data for the person to standard output */
    int print_person(const Person *person);
    
    /* Prints the data for the persons in the list to standard output */
    void print_person_list(const Person *person_list[], int list_size);
    Instead of using gets, which is inherently vulnerable to buffer overflow, use fgets, but note that fgets saves the newline entered into the input buffer if there is space for it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    8
    Went a different route. Answer:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    #define BUFF_SIZE 32
    #define STRUCT_SIZE 512
    
    
    struct info {
        char name[BUFF_SIZE];
        char stAddress[BUFF_SIZE];
        char cityAndState[BUFF_SIZE];
        char zip[BUFF_SIZE];
    };
    
    
    void selectionSort(struct info *ptrStruct[], int size);
    
    
    int main(void){
        int count, size;
        char buffer[512];
        struct info *ptrStruct[STRUCT_SIZE];
    
    
        for (count = 0; count < STRUCT_SIZE; count++){
            ptrStruct[count] = (struct info*) malloc(sizeof(struct info));
            if (EOF == scanf("%511[^\n]%*c", buffer)){
                free(ptrStruct[count]);
                break;
            };
            strcpy(ptrStruct[count]->name, buffer);
            scanf("%511[^\n]%*c", buffer);
            strcpy(ptrStruct[count]->stAddress, buffer);
            scanf("%511[^\n]%*c", buffer);
            strcpy(ptrStruct[count]->cityAndState, buffer);
            scanf("%511[^\n]%*c", buffer);
            strcpy(ptrStruct[count]->zip, buffer);
        }
    
    
        size = count;
        selectionSort(ptrStruct, size);
    
    
        printf("\n\nLEAST TO GREATEST\n");
        for (count = 0; count < size; count++)
        {
            printf("%s\n", ptrStruct[count]->name);
            printf("%s\n", ptrStruct[count]->stAddress);
            printf("%s\n", ptrStruct[count]->cityAndState);
            printf("%s\n", ptrStruct[count]->zip);
            free(ptrStruct[count]);
        }
    }
    
    
    void selectionSort(struct info *ptrStruct[], int size)
    {
        int count1, count2;
        int minIndex;
        struct info *ptrTemporary;
    
    
        for (count2 = 0; count2 < size - 1; count2++)
        {
            minIndex = count2;
            for (count1 = count2 + 1; count1 < size; count1++)
            {
                if (strcmp(ptrStruct[count1]->zip, ptrStruct[minIndex]->zip) < 0)
                    minIndex = count1;
            }
            if (minIndex != count2){
                ptrTemporary = ptrStruct[count2];
                ptrStruct[count2] = ptrStruct[minIndex];
                ptrStruct[minIndex] = ptrTemporary;
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures & Linked List
    By Dylan Forsyth in forum C Programming
    Replies: 2
    Last Post: 05-05-2016, 06:45 AM
  2. Get list of all network interfaces (also with MAC address) in C
    By ulisse89 in forum Networking/Device Communication
    Replies: 0
    Last Post: 02-09-2012, 01:44 PM
  3. List of structures
    By jpfrazao in forum C Programming
    Replies: 1
    Last Post: 12-14-2010, 08:36 PM
  4. Making an address book in C++ with arrays inside of structures?
    By boblablabla in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 10:18 PM
  5. a list of structures
    By stevey in forum C Programming
    Replies: 6
    Last Post: 11-15-2001, 09:01 AM

Tags for this Thread