Answer to Question #259710 in UNIX/Linux Programming for ret

Question #259710

Write a C program to declare a structure name Employee consisting of the members (give appropriate data

type) name[30], salary, employee_ID[12], and age. Create three structure variables (emp1, emp2 and emp3) and

display the name and employee_ID of the person drawing highest salary.


1
Expert's answer
2021-11-01T08:57:37-0400
  // To Developed Employee Details using LogIn Screen
   // And access to add employee,view details by id, update data, delete
  // using structure 
   #include <stdio.h>
   #include<conio.h>
   #include<stdlib.h>
#include<string.h>
#include<dos.h>
#include<windows.h>

/*structure declaration*/
struct employee
{
   char  fname[20],lname[20];
   int   emp_id;    
   float emp_salary;
};
void main()
{
    FILE *outfile, *infile,*temp;
    struct employee input;          
    int a,op,o,n;
    char pass[20],uname[30];
    char ch;
    int j,k,i;
    char pass1[5]="amit", un[5]="amit",fnam[20],lnam[20];

    //Design Part
        printf("\n\n\t");
            for(i=0;i<11;i++)   {
            printf("*");
            usleep(50000);
            }        

                usleep(500000); printf(" Welcome");usleep(500000);printf(" To ");usleep(500000);
                printf("A");    usleep(500000); printf("X"); usleep(500000);    printf("I");  usleep(500000); printf("O");  usleep(500000); printf("M");    
                usleep(500000); printf(" SOFTECH");usleep(500000);printf(" PVT.");usleep(500000);printf(" LTD. ");usleep(50000);    

         for(i=0;i<11;i++)  
         {
            printf("*");
            usleep(50000);
            }

login:       

     printf("\n\n\t\t\t ~~~~~~~~~~~~~~~~~~~~~");
      printf("\n\t\t\t |  AUTHORIZE LOGIN  |");
      printf("\n\t\t\t ~~~~~~~~~~~~~~~~~~~~~");
      printf("\n\n\t\t*-----------------------------------------*");
      printf("\n\t\t  User Name :- ");
        gets(uname);
     printf("\n\t\t  Password(Provided By programmer) :- ");

    for(i=0;i<4;i++)
     {
        ch = getch();
        pass[i] = ch;
        ch = '*' ;
        printf("%c",ch);
    }
 printf("\n\t\t*-----------------------------------------*");
     pass[i] = '\0';
 getch();

     if((strcmp(uname,un)==0) && (strcmp(pass,pass1)==0))
         { 
         printf("\n\n\n\t\t    @-------- Login Successful --------@");      //login successful then data will
    menu:   

        printf("\n\n    \t+++++++++++++++++ AMIT BEHERE PVT. LTD. +++++++++++++++++");          

         printf("\n\n\t\t    #```````````````````````````#");
                        printf("\n\t\t    | AB Employee Management |");
                        printf("\n\t\t    #'''''''''''''''''''''''''''#");

         printf("\n\t\t    *---------------------------*");
         printf("\t\n\t\t    |\t1. Add Employee \t|\n\t\t    |\t2. Display Employee\t|\n\t\t    |\t3. Search Employee\t|\n\t\t    |\t4. Update Employee\t|\n\t\t    |\t5. Delete Employe\t|");
         printf("\n\t\t    *---------------------------*");
         printf("\n\t Enter Option :- ");
         scanf("%d",&op);

         switch(op)
         {
            case 1: //Add Employee
                    outfile = fopen ("accounts.dat","a+");
                    if (outfile == NULL)
                     {
                    fprintf(stderr, "\n\tError opening accounts.dat\n\n");
                     }
                     printf("\n\tEnter Count Of Employee To Add :- ");
                    scanf("%d",&n);
                    for(i=0;i<n;i++)
                    {
                            printf("\n\t Enter First Name  :- ");
                                scanf ("%s", input.fname);
                            printf("\n\t Enter Last Name   :- ");
                                scanf ("%s", input.lname);
                            printf("\n\t Enter Employee Id :- ");
                                scanf ("%d", &input.emp_id);
                            printf("\n\t Enter Salary      :- ");
                                scanf ("%f", &input.emp_salary);

                        // write entire structure to Accounts file
                        fwrite (&input, sizeof(struct employee), 1, outfile);
                    }
                     fclose(outfile);        
                     break;

             case 2: //Display All Records
                    infile = fopen ("accounts.dat","r");

                       if (infile == NULL)
                        {
                        fprintf(stderr, "\nError opening accounts.dat\n\n");                    
                        }
            rewind(infile);
                    while (fread (&input, sizeof(struct employee), 1, infile)==1)
                        {
                        printf("\n\t#---------------------------------------#");  
                        printf ("\n\t| Name Of Employee %d -> %5s %5s \t|\n\t| Employee Id     -> %4d \t\t|\n\t| Salary        -> %8.2f\t\t|\n",i,
                        input.fname, input.lname, input.emp_id, input.emp_salary);
                        printf("\t#---------------------------------------#");
                        printf("\n");
                        i++;
                        }
                        i=1;
                    fclose(infile);
                   break;

             case 3:    //Search Employee   
                    infile = fopen ("accounts.dat","r");
                    if (infile == NULL)
                        {
                        fprintf(stderr, "\nError opening accounts.dat\n\n");

                        }

                            printf("\n\t Enter First name of Employee :-");
                            scanf("%s",&fnam); 
                            printf("\n\t Enter Last name of Employee :-");
                            scanf("%s",&lnam); 
       rewind(infile);

                while(fread(&input,sizeof(input),1,infile)==1)
                    { 
                    if((strcmp(input.fname,fnam)==0)&&(strcmp(input.lname,lnam)==0))
                        {
                        printf("\n\t#-----------------------------------------------#");
                        printf ("\n\t| Name Of Employee  -> %5s %5s \t\t|\n\t| Employee Id     -> %4d \t\t\t|\n\t| Salary        -> %8.2f\t\t\t|\n",
                        input.fname, input.lname, input.emp_id, input.emp_salary);

                        printf("\t#-----------------------------------------------#");
                        }   

                    }                           
                         fclose(infile);
                break;  
            case 4:  //Employee Update
                    infile = fopen ("accounts.dat","rb+");
                    if (infile == NULL)
                        {
                        fprintf(stderr, "\nError opening accounts.dat\n\n");

                        }     
                        printf("\n\tEnter the Employee Name to Modify: ");
                        scanf("%s",fnam);
                        printf("\n\tEnter Last Name of Employee :-");
                        scanf("%s",&lnam);
                        rewind(infile);
                         while(fread(&input,sizeof(input),1,infile)==1)/// fetch all record from file
                         {
                            if( (strcmp(input.fname,fnam)==0) && (strcmp(input.lname,lnam)==0))   ///if entered name matches with that in file
                            {
                        update:
                        printf("\n\t\t  *````````````````````````````*");
                        printf("\n\t\t  | Employee Details Parameter |");
                        printf("\n\t\t  *''''''''''''''''''''''''''''*");
                        printf("\n\t\t    +-------------------+");
                        printf("\t\n\t\t    |\t1. First Name \t|\n\t\t    |\t2. Last Name\t|\n\t\t    |\t3. Employee Id\t|\n\t\t    |\t4. Salary\t|\n\t\t    ");
                        printf("+-------------------+");
                        printf("\n");



                        printf("\n\t Enter Field Option to Update :- ");
                        scanf("%d",&o);
                        switch(o)
                    {
                            case 1:
                                printf("\n\t Enter First Name  :- ");
                                scanf ("%s", input.fname);

                            break;
                            case 2: 
                                printf("\n\t Enter Last Name   :- ");
                                scanf ("%s", input.lname);
                            break;
                            case 3:    
                                printf("\n\t Enter Employee Id :- ");
                                scanf ("%d", &input.emp_id);
                            break;
                            case 4:    
                                printf("\n\t Enter Salary      :- ");
                                scanf ("%f", &input.emp_salary);
                            break;
                    }
                 printf("\n Do you Wish to Continue Updation for Given Employee[Y/N]  ");
                     scanf("%s",&ch);
                     if(ch=='y'||ch=='Y')
                     {
                     goto update;
                     }
                fseek(infile,-sizeof(struct employee),SEEK_CUR); /// move the cursor 1 step back from current position
                fwrite(&input,sizeof(struct employee),1,infile); /// override the record

              fclose(infile);

            }

        }
                    break;      
            case 5: //Delete Employee
                    outfile = fopen ("accounts.dat","rb+");
                    printf("\n\tEnter First Name of Employee :-  ");
                    scanf("%s",fnam);
                    printf("\n\tEnter Last Name of Employee :-");
                    scanf("%s",&lnam);
                    temp = fopen("temp.dat","wb");                           /// temporary file is created
                    rewind(outfile);                                        /// move record to starting of file
                while(fread(&input,sizeof(struct employee),1,outfile) == 1)                /// read all records from file
                    {
                    if(strcmp(input.fname,fnam) != 0 &&(strcmp(input.lname,lnam)!=0) )                /// if the entered record match
                        {
                        fwrite(&input,sizeof(struct employee),1,temp);                  // move all records except the one that is to be deleted to temp file
                        }
                    }
                    fclose(outfile);
                    fclose(temp);
                    remove("accounts.dat");                             // Delete orginal file
                    rename("temp.dat","accounts.dat");                  // rename the temp file to original file name
                    outfile = fopen("accounts.dat", "rb+");
                    printf("\n\tRecord Deleted Successfully...\n");

                break;             
             default: printf("\n\t\t Enter Valid Option");
                    goto menu;   
         }

         printf("\n Do you Wish to Continue With Employee Management Service[Y/N]  ");
                     scanf("%s",&ch);
                     if(ch=='y'||ch=='Y')
                     {
                     goto menu;
                     }

         }
    else
        {
            system("cls");
            printf("\n\n    \t++++++++++++ Welcome To AMIT BEHERE World. ++++++++++++");
            printf("\n\n\t\tInvalid UserName or Password");
            printf("\n\t\tTry Again");
         goto login;
         }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS