Mailing list: A structure example


Design a structure to hold the data for a mailing list. Read and print the data.

C Program:

#include<stdio.h>
#include<string.h>
struct mailing_list {                                 /*    structure definition   */
         char name[50];
         char address_line1[50];
         char address_line2[50];
         char city[50];
         char state[20];
         long  int zip;
};
int main()
{
         int i,n;
         printf("Number of customers: ");
         scanf("%d",&n);
         fflush(stdin);
         struct mailing_list customer[n];
         printf("\n");
         for(i=0;i<n;i++)
         {
                 printf("customer %d address\n",i);
                 printf("name: ");
                 scanf("%[^\n]",customer[i].name);
                 printf("address_line1: ");
                 scanf(" %[^\n]",customer[i].address_line1);
                 printf("address_line2: ");
                 scanf(" %[^\n]",customer[i].address_line2);
                 printf("City: ");
                 scanf(" %[^\n]",customer[i].city);
                 printf("State: ");
                 scanf(" %[^\n]",customer[i].state);
                 printf("zip: ");
                 scanf("%ld",&customer[i].zip);
                 fflush(stdin);
          }
          printf("\n");
          for(i=0;i<n;i++)
          {
                 printf("customer %d address\n",i);
                 printf("Name: %s\n",customer[i].name);
                 printf("Address line1: %s\n",customer[i].address_line1);
                 printf("Address line2: %s\n",customer[i].address_line2);
                 printf("City: %s\n",customer[i].city);
                 printf("State: %s\n",customer[i].state);
                 printf("Zip: %ld\n",customer[i].zip);
                 printf("\n");
          }
          return 0;
}

Input:

Number of customers: 2

customer 0 address
name: pranith
address_line1: c nagar
address_line2: program nagar
City: c program
State: program
zip: 500001

customer 1 address
name: mahadev
address_line1: basic c nagar
address_line2: structure nagar
City: C
State: programming nagar
zip: 500002


Output:
customer 0 address
Name: pranith
Address line1: c nagar
Address line2: program nagar
City: c program
State: program
Zip: 500001


customer 1 address
Name: mahadev
Address line1: basic c nagar
Address line2: structure nagar
City: C
State: programming nagar
Zip: 500002



 

 

No comments: