...
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; }
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...
In the main method:
Create an array of four "struct address":
struct address addrArray[4];
Set all the values in the four addresses.
Call printAddresses
...
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; }
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
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.
In the main method:
print "Please input a name to search for: "
scanf the input name into a string. You will need to use
strcpy
Create an array of four "struct address":
struct address addrArray[4];
Set all the values in the four addresses.
Call
findAddress
to see if you have a match.If you do, call
printAddress(addrArray[index])
where index is the index returned by findAddress.If there is no match, print "Sorry, no matches found!"
...
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; }
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
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.
In the main method:
print "Please input the name of the file with the names to use: "
scanf the input name into a string. You will need to use
strcpy
print "Please input the name of the output file where the addresses will be written: "
scanf the output file name into a string. You will need to use
strcpy
Create an array of four "struct address":
struct address addrArray[4];
Set all the values in the four addresses.
Open the input file with names for reading.
Open the output file for writing.
Loop through all the names in the input file. For each name:
Call
findAddress
to see if you have a match.If you do, call
writeAddress(fp, addrArray[index])
where index is the index returned by findAddress.If there is no match, do not write anything to the output file.
Close both files at the end.
...