> ~ Online tutorial


/*********************************************************/
/*                                                       */
/* Structures Demo                                       */
/*                                                       */
/*********************************************************/

   /* Simple program to initialize some structures */
   /* and to print them out again. Does no error   */
   /* checking, so be wary of string sizes etc..   */

#include <stdio.h>

#define NAMESIZE    30
#define ADDRSIZE    80
#define NOOFPERSONS 20
#define NEWLINE()   putchar('\n');

/*********************************************************/

typedef struct
  {
  char *Name;
  char *Address;
  int YearOfBirth;
  int MonthOfBirth;
  int DayOfBirth;
  }
PersonDat;

/*********************************************************/

main ()                          /* Make some records */

{ PersonDat record[NOOFPERSONS];
  PersonDat PersonalDetails();
  int person;

printf ("Birth Records For Employees");
printf ("\n---------------------------");
printf ("\n\n");

printf ("Enter data\n");

for (person = 0; person < NOOFPERSONS; person++)
   {
   record[person] = PersonalDetails();
   NEWLINE();
   }

DisplayRecords (record);
}

/*********************************************************/

PersonDat PersonalDetails()     /* No error checking! */

{ PersonDat dat;
  char strbuff[ADDRSIZE], *malloc();

printf ("Name :");
dat.Name = malloc(NAMESIZE);
strcpy (dat.Name,gets(strbuff));

printf ("Address :");
dat.Address = malloc(ADDRSIZE);
strcpy (dat.Address,gets(strbuff));

printf ("Year of birth:");
dat.YearOfBirth = getint (1900,1987);

printf ("Month of birth:");
dat.MonthOfBirth = getint (1,12);

printf ("Day of birth:");
dat.DayOfBirth = getint(1,31);

return (dat);
}

/**********************************************************/

DisplayRecords (rec)

PersonDat rec[NOOFPERSONS];

{ int pers;

for (pers = 0; pers < NOOFPERSONS; pers++)
   {
   printf ("Name : %s\n", rec[pers].Name);
   printf ("Address : %s\n", rec[pers].Address);
   printf("Date of Birth: %1d/%1d/%1d\n",rec[pers].DayOfBirth,
              rec[pers].MonthOfBirth,rec[pers].YearOfBirth);
   NEWLINE();
   }
}

/**********************************************************/
/* Toolkit                                                */
/**********************************************************/

getint (a,b)             /* return int between a and b */

int a,b;

{ int  p, i = a - 1;

for (p=0; ((a > i) || (i > b)); p++)
   {
   printf ("? : ");
   scanf ("%d",&i);
   if (p > 2)
      {
      skipgarb();
      p = 0;
      }
   }
skipgarb();
return (i);
}

/**********************************************************/

skipgarb()       /* Skip input garbage corrupting scanf */

{
while (getchar() != '\n')
   {
   }
}

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: