Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Assignment 9: Assume that you run a business and keep a computerized file of

ID: 3838422 • Letter: L

Question

Lab Assignment 9: Assume that you run a business and keep a computerized file of all of your customers. The address file is formatted as follows: Ms. Peggy Marcher Sterling Enterprise Rentals 888 E. Circle Detroit, MI 54301 Mr. Rudolph Valentino Hollywood Silverware 21 Hollywood Ave. Hollywood, CA 10202 Presant Gevy Bush Assume that each entry for a customer takes exactly four lines. A separate file contains the body. of a very polite "Bill Overdue" letter that asks for the customer to pay up as soon as possible. Write a program that prompts the user for a customer's name and the current date; searches the address file for the customer's name; and, if successful, generates an output file that contains an individualized letter with the customer's name and address together in the header of the document with the body of the "Bill Overdue" letter. The output file should be similar to the below: President George Bush 1 Whitehouse Boulevard Washington, DC 2001 Date: 11/24/2016 Dear President George Bush You're bill is overdue... (the rest of the letter)

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include <sys/types.h>

char* readAddress(char* name) { //search for an name, if found return addtess, else NULL

   FILE *fp = fopen("addressFile.txt", "r");
   char * line = NULL, *out;
       size_t len = 0;
       ssize_t read;
   out = malloc(sizeof(char) * 250);
   out[0] = '';
   while ((read = getline(&line, &len, fp)) != -1) { //read one line at a time
       strcpy(out,line);
       line[strlen(line) - 2] = ''; //this eats up the last endline character ' ' from the string
       if(strcmp(line,name)==0) {    //if name matches
           getline(&line, &len, fp); //read remaining 3 lines and concat them
           strcat(out,line);
           getline(&line, &len, fp);
           strcat(out,line);
           getline(&line, &len, fp);
           strcat(out,line);
           return out;
       }
       else { //if not found
           getline(&line, &len, fp); //consume more 3 lines
           getline(&line, &len, fp);
           getline(&line, &len, fp);
       }
   }
   return NULL; //nam not found
}
char* getName(char *address) { //method to extract the name (first line) from the address
   char* name = malloc(sizeof(char)*60);
   int i = 0;
   for (;address[i] != ' '; i++)
       name[i] = address[i];
   name[i] = '';
   return name;
}

void writeLetter(char* address) { //method to write the lettrer to file letter,txt
   FILE *fp = fopen("letter.txt", "w");
   fprintf(fp,"%s ",address);
   fprintf(fp," Date: 11/24/2014 ");
   fprintf(fp,"Dear %s, ", getName(address));
   fprintf(fp,"Your bill is due... (the rest of the letter) ...");
   fclose(fp);
}

void main() {
   char name[50], *address;
   printf("Enter name: ");
   gets(name);
   address = readAddress(name);
   if (address!=NULL) {
       writeLetter(address);
       printf("letter printed to file letter.txt");
   }
   else
       printf("Name not found");
}

/*
AddressFile.txt:
Ms. Peggy Marches
Sterling Enterprise Rentals
888 E. Circle
Detroit, MI 54301
Samba
5
6
7
Zupo
8
9
0
President George Bush
1 Whitehouse Boulevard
Washington,DC 2001

*/

The code is commneted. Even agter going through the comments, you are facing trouble, please feel free to comment below. I shall try my best to resolve all your issues