Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Define a struct called address as follows:

    Code Block
    struct address 
    { 
        char name[40];
        char street[50];
        char city[40];
        char state[2];
        int zip;
    }
  2. Add a function called printAddresses. It's declaration will be

    Code Block
    void printAddresses(struct address addrArray[], int count);

    It will print all the addresses in an array of struct address as follows:

    Code Block
    Address number 0:
        Name: Bill Rideout
        Street: South Harbor Road
        City: Townsend
        State: MA
        Zip: 01469
    Address number 1:
        and so on...
  3. In the main method:

    1. Create an array of four "struct address": struct address addrArray[4];

    2. Set all the values in the four addresses.

    3. Call printAddresses

...

  1. Define the same struct called address as follows:

    Code Block
    struct address 
    { 
        char name[40];
        char street[50];
        char city[40];
        char state[2];
        int zip;
    }
  2. Add a function called printAddress. It's declaration will be

    Code Block
    void printAddress(struct address addr);

    It will print a single struct address as follows:

    Code Block
    Name: Bill Rideout
        Street: South Harbor Road
        City: Townsend
        State: MA
        Zip: 01469
  3. Add a function called findAddress. It's declaration will be

    Code Block
    int findAddress(struct address addrArray[], int count, char * name);

    This method will return the index of addrArray where address.name matches name. You will need to use the strcmp method to test whether the names match. If you find a match, return the index of addrArray where the names match. If you don't find a match, return -1.

  4. In the main method:

    1. print "Please input a name to search for: "

    2. scanf the input name into a string. You will need to use strcpy

    3. Create an array of four "struct address": struct address addrArray[4];

    4. Set all the values in the four addresses.

    5. Call findAddress to see if you have a match.

    6. If you do, call printAddress(addrArray[index]) where index is the index returned by findAddress.

    7. If there is no match, print "Sorry, no matches found!"

...

  1. Define the same struct called address as follows:

    Code Block
    struct address 
    { 
        char name[40];
        char street[50];
        char city[40];
        char state[2];
        int zip;
    }
  2. Add a function called writeAddress. It's declaration will be

    Code Block
    void printAddress(FILE * fp, struct address addr);

    It will write a single struct address as follows to an open writable file:

    Code Block
        Name: Bill Rideout
        Street: South Harbor Road
        City: Townsend
        State: MA
        Zip: 01469
  3. Reuse the function called findAddress from Exercise 4. It's declaration will be

    Code Block
    int findAddress(struct address addrArray[], int count, char * name);

    This method will return the index of addrArray where address.name matches name. If you find a match, return the index of addrArray where the names match. If you don't find a match, return -1.

  4. In the main method:

    1. print "Please input the name of the file with the names to use: "

    2. scanf the input name into a string. You will need to use strcpy

    3. print "Please input the name of the output file where the addresses will be written: "

    4. scanf the output file name into a string. You will need to use strcpy

    5. Create an array of four "struct address": struct address addrArray[4];

    6. Set all the values in the four addresses.

    7. Open the input file with names for reading.

    8. Open the output file for writing.

    9. Loop through all the names in the input file. For each name:

      1. Call findAddress to see if you have a match.

      2. If you do, call writeAddress(fp, addrArray[index]) where index is the index returned by findAddress.

      3. If there is no match, do not write anything to the output file.

    10. Close both files at the end.

...