Logo 
Search:

C Programming Articles

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

Program to count number of male employee in a company using structures/file perform this job

Posted By: Mohammed Evans     Category: C Programming     Views: 3983

For the employee problem above, we want to count and print the number of only male employees in the age range of 25 and 30. Assume that the input record contains SEXCODE and AGE fields to provide this information. Write a program using structures/file perform this job.

Code for Program to count number of male employee in a company using structures/file perform this job in C Programming

#include <stdio.h>
#include <conio.h>
#include "valid.c"struct emp_detail
{  int emp_no;
   char emp_name[10];
   int emp_age;
   int emp_sexcode;
};

 void getWriteFileName(char *str);
 void getReadFileName(char *str);
 void enterData();
 int getSexCode();
 void getTotalCount();
 void getName(char *);

 FILE *fp;
 int flag=1;
 int count=0;
 int male_count=0;

 char filename[14];
 struct emp_detail emp_cur;

 void main()
 {
    char option;
    clrscr();
    printf("\nDo you want to create the data file type 'y' for yes and 'n' for no.");
    scanf("%c", &option);
    fflush(NULL);

      if( option == 'y' || option == 'Y')
       getWriteFileName(filename);
      elseif( option == 'n' || option == 'N')
       {   getReadFileName(filename);
       getTotalCount();
       }

    getch();
 }

 void enterData()
 {
    printf("\nEnter data .....");
    while(flag)
    {
    //printf("\nEnter employee no. : ");//scanf("%d", &emp_cur.emp_no);
    getPosInteger("Enter employee no. : ", &emp_cur.emp_no);
    fflush(NULL);

    printf("\nEnter employee name : ");
       //scanf("%[^\n]", emp_cur.emp_name);
    getName(emp_cur.emp_name);
    fflush(NULL);

    //printf("\nEnter employee age : ");//scanf("%d", &emp_cur.emp_age);
    getPosInteger("Enter employee age : ", &emp_cur.emp_age);
    fflush(NULL);

    printf("Enter employee sex (1 Male / 2 Female)  : ");
    emp_cur.emp_sexcode = getSexCode();
    //printf("Sex code : %d", emp_cur.emp_sexcode);

    fflush(NULL);
    if(emp_cur.emp_sexcode == 1)
    {
       if(emp_cur.emp_age >= 25 && emp_cur.emp_age <= 30)
         male_count++;
    }
    fprintf(fp,"%d %-10s %d %d", emp_cur.emp_no, emp_cur.emp_name, emp_cur.emp_age, emp_cur.emp_sexcode);

    if( flag > 0 )
       fprintf(fp,"\n");

    flag++;

    if(emp_cur.emp_no != 9999)
    {   printf("\nEnter 1 to continue and 0 to exit.");
        scanf("%d", &flag);
        fflush(NULL);
    }
    else flag=0;
    count++;
    }

    fclose(fp);
    printf("\nTotal number of male employee between 25 to 30 age is : %d", male_count);
 }

    void getName(char *str)
    {
    int count=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 = readch;

        count++;
        getstr++;
        if( count >= 10 )
          break;
       }

       if(errorflag)
          getName(emp_cur.emp_name);

      // *getstr='\0';
    }

   int getSexCode()
   {
     int code;
     int rval;
     rval =scanf("%d", &code);

     if( ( rval == 0) ||   ( ( code != 1) && (code != 2)  ) )
     {  fflush(NULL);
    printf("\n Invalid sex code entered. Enter 1 for male and 2 for female. :");
    code = getSexCode();
     }
      return code;
   }

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

     if( ( fp = fopen(str, "wt") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
     else
    enterData();
   }

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

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

    void getTotalCount()
    {  flag =0;
       while( feof(fp) == 0 )
       {
      fscanf(fp, "%d %10s %d %d", &emp_cur.emp_name, emp_cur.emp_name, &emp_cur.emp_age, &emp_cur.emp_sexcode);
      flag++;
      if(emp_cur.emp_sexcode == 1)
      {
         if(emp_cur.emp_age >= 25 && emp_cur.emp_age <= 30)
           male_count++;
      }
       }
      fclose(fp);
      printf("\nTotal number of male employee between 25 to 30 age is : %d", male_count);

    }

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


Do you want to create the data file type 'y' for yes and 'n' for no.y
Enter Output File name maxium 14 character : output.txt

Enter data .....
Enter employee no. : 1

Enter employee name : chintan

Enter employee age : 25
Enter employee sex (1 Male / 2 Female) : 1


Enter employee no. : 9999

Enter employee name : nilay

Enter employee age : 22
Enter employee sex (1 Male / 2 Female) : 1

Total number of male employee between 25 to 30 age is : 1


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



Mohammed Evans
Mohammed Evans author of Program to count number of male employee in a company using structures/file perform this job is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
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!