Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program of maintaining employee detail

Posted By: Emily Brown     Category: C Programming     Views: 13380

The Alumni association of a particular educational institute maintains records of passed out students in a file. The data for each student includes; roll-number,name, year of passing out, department, home address, and employer's name and place.The alumni association has received a query from an old student asking names of his colleagues (same year and department) who are presently employed in BOMBAY. Write a program to prepare an answer for him.

Code for Program of maintaining employee detail in C Programming

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include "valid.c"struct student_detail
{
   int rollno;
   char name[10];
   int pass_year;
   char department[10];
   char address[20];
   char employer[20];
   char place[20];
};


 void getWriteFileName(char *);
 void getReadFileName(char *);
 void enterData();
 void searchFor();
 void startSearch();
 void getName(char *);
 void getAddress(char *);
 int charToNumber(char chr);
 int compare(char *source, char *csource);

 struct student_detail st_cur, st_search;
 int flag=1, option=0;
 char filename [14];
 char data[94];
 char num[4];


 FILE *fp;

  void main()
  {
    clrscr();
    printf("\nSelect any of the option from below.");
    printf("\n1. Add New Records");
    printf("\n2. Find student detail\n");

    scanf("%d", &option);
    fflush(NULL);

      if( option == 1)
     getWriteFileName(filename);
      elseif( option == 2)
      {
     getReadFileName(filename);
     searchFor();
     startSearch();
      }

    getch();
  }

 void enterData()
 {
    printf("\nEnter data ..... Use value 9999 for roll no to terminate data entry.");
    while(flag)
    {
    getPosInteger("Enter Record No. : ", &st_cur.rollno);
    fflush(NULL);

    printf("\nEnter Student Name : ");
    getName(st_cur.name);
    fflush(NULL);

    getPosInteger("Enter Passing out Year : ", &st_cur.pass_year);
    fflush(NULL);

    printf("\nEnter Department Name : ");
    getName(st_cur.department);
    fflush(NULL);

    printf("\nEnter Student Address : ");
    getAddress(st_cur.address);
    fflush(NULL);

    printf("\nEnter Employer's Name : ");
    getAddress(st_cur.employer);
    fflush(NULL);

    printf("\nEnter Employment Place : ");
    getAddress(st_cur.place);
    fflush(NULL);

    fprintf(fp,"%-4d %-10s %-4d %-10s %-20s %-20s %-20s", st_cur.rollno, st_cur.name, st_cur.pass_year, st_cur.department, st_cur.address, st_cur.employer, st_cur.place);

    flag++;

      if(st_cur.rollno == 9999)
    flag = 0;
      else
     fprintf(fp,"\n");
    }

    fclose(fp);
 }

 void searchFor()
 {
    printf("\nEnter search parameter.");

    getPosInteger("Enter Passing out Year : ", &st_search.pass_year);
    fflush(NULL);

    printf("\nEnter Department Name : ");
    getName(st_search.department);
    fflush(NULL);

    printf("\nEnter Employment Place : ");
    getAddress(st_search.place);
    fflush(NULL);

 }
    void getName(char *str)
    {
    int count1=0;
    char *getstr;
    char readch='\0';
    int errorflag = 0;

       fflush(NULL);
       getstr = str;

       while(1)
       {
        scanf("%c", &readch);
        if( readch == '\n')
           break;
        if( isalpha(readch) == 0)
        {   printf("\nInvalid entry. Enter characters only.\nEnter name again :");
            errorflag = 1;
            break;
        }
        else
            *getstr = tolower(readch);

        count1++;
        getstr++;
        if( count1 >= 10 )
          break;
       }
       fflush(NULL);

       if(errorflag)
          getName(str);

       *getstr='\0';
    }

    void getAddress(char *str)
    {
    int count1=0;
    char *getstr;
    char readch='\0';

       fflush(NULL);
       getstr = str;

       while(1)
       {
        scanf("%c", &readch);

        if( readch == '\n')
           break;
        else
            *getstr = tolower(readch);

        count1++;
        getstr++;
        if( count1 >= 20 )
          break;
       }
       fflush(NULL);

       *getstr='\0';
    }

   void getWriteFileName(char *fname)
   {
     printf("Enter Output File name maxium 14 character : ");
     scanf("%s", fname);

     if( ( fp = fopen(fname, "a+t") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
     else
    enterData();
  }

   void getReadFileName(char *fname)
   {
     printf("Enter Input File name maxium 14 character : ");
     scanf("%s", fname);

     if( ( fp = fopen(fname, "r+t") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
   }

   void startSearch()
   {
      int count1=0, count2=0, count3=0;

      while ( feof(fp) == 0)
      {
    // fscanf(fp, "%-4d %-10s %-4d %-10s %-20s %-20s %-20s", &st_cur.rollno, st_cur.name, &st_cur.pass_year, st_cur.department, st_cur.address, st_cur.employer, st_cur.place);//printf("%-4d %-10s %-4d %-10s %-20s %-20s %-20s", st_cur.rollno, st_cur.name, st_cur.pass_year, st_cur.department, st_cur.address, st_cur.employer, st_cur.place);
      st_cur.pass_year=0;
      for(count1=0; count1<94; count1++)
      {
          data[count1] = getc(fp);
         if( data[count1] == '\n')
           break;

          if( count1 >=16 && count1 <= 19)
          {
         if( isdigit(data[count1]) )
           if( st_cur.pass_year == 0)
             st_cur.pass_year = charToNumber(data[count1]);
           else
           {
              st_cur.pass_year *= 10;
              st_cur.pass_year += charToNumber(data[count1]);
           }
          }

          elseif( count1 >=21 && count1 <= 30)
          {    if( isalpha(data[count1]) )
           {
             st_cur.department[count2] = data[count1];
             count2++;
           }
          }
          elseif( count1 >=74 && count1 <= 93)
          {
          st_cur.place[count3] = data[count1];
          count3++;
          }
          //else count2=0;
      }

        data[count1] ='\0';
        st_cur.department[count2] = '\0';
        st_cur.place[count3] = '\0';
        count2=0;
        count3=0;

        if( (st_cur.pass_year == st_search.pass_year) && compare(st_cur.department, st_search.department) && compare(st_cur.place, st_search.place) )
        printf("\n%s",data);
      }
       fclose(fp);
   }

   int charToNumber(char chr)
   {
       int val=0;
       switch(chr)
       {
      case'0': val = 0;
            break;
      case'1': val = 1;
            break;
      case'2': val = 2;
            break;
      case'3': val = 3;
            break;
      case'4': val = 4;
            break;
      case'5': val = 5;
            break;
      case'6': val = 6;
            break;
      case'7': val = 7;
            break;
      case'8': val = 8;
            break;
      case'9': val = 9;
            break;
       }
       return val;
   }

    int compare(char *source, char *csource)
    {
       int cflag = 0;
       char *tsource = source;
       char *str = csource;
        while(1)
        {
          if( *tsource == *str)
        cflag = 1;
          else {
             cflag = 0;
             break;
           }
           tsource++;
           str++;
           if(  *tsource == '\0' || *str=='\0' )
          break;
        }
        return cflag;
    }

    /*********************************  Input ***********************


Select any of the option from below.
1. Add New Records
2. Find student detail
1
Enter Output File name maxium 14 character : output.txt

Enter data ..... Use value 9999 for roll no to terminate data entry.
Enter Record No. : 1

Enter Student Name : chintan

Enter Passing out Year : 1999

Enter Department Name : development

Enter Employer's Name : TCS

Enter Employment Place : pune

Enter Student Address : sabarmat


Select any of the option from below.
1. Add New Records
2. Find student detail
2
Enter Input File name maxium 14 character : output.txt

Enter search parameter.
Enter Passing out Year : 1999

Enter Department Name : development

Enter Employment Place : pune

************************************************************************/
  
Share: 


Didn't find what you were looking for? Find more on Program of maintaining employee detail Or get search suggestion and latest updates.

Emily Brown
Emily Brown author of Program of maintaining employee detail is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!