C语言 将员工的详细信息存储在结构中的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7630890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:47:30  来源:igfitidea点击:

program to store details of an employee in a structure

c

提问by Failed_Noob

This is a program to store details of an employee in a structure. Although the program runs it shows many errors, it does not give me a chance to enter address. Why is the program not running properly . Where am I going wrong ?

这是一个将员工详细信息存储在结构中的程序。虽然程序运行它显示了很多错误,但它并没有给我输入地址的机会。为什么程序运行不正常。我哪里错了?

#include <stdio.h>
#include <conio.h>

struct details
{
 char name[30];
 int age;
 char address[500];
 float salary;
};

int main()
{
 struct details detail;
 clrscr();
 printf("\nEnter name:\n");
 gets(detail.name);
 printf("\nEnter age:\n");
 scanf("%d",&detail.age);
 printf("\nEnter Address:\n");
 gets(detail.address);
 printf("\nEnter Salary:\n");
 scanf("%f",&detail.salary);


 printf("\n\n\n");
 printf("Name of the Employee : %s \n",detail.name);
 printf("Age of the Employee : %d \n",detail.age);
 printf("Address of the Employee : %s \n",detail.address);
 printf("Salary of the Employee : %f \n",detail.salary);

 getch();
}

This is the output I get:

这是我得到的输出:

enter image description here

在此处输入图片说明

回答by adatapost

Statement scanf("%d",&detail.age);will read 222but not the newline you've entered. This newline will remain in input buffer and pass it to next input gets().

语句 scanf("%d",&detail.age);将读取,222但不会读取您输入的换行符。此换行符将保留在输入缓冲区中并将其传递给下一个输入gets()。

You can use getchar()method to remove some chars from the input buffer to avoid such problems.

您可以使用getchar()method 从输入缓冲区中删除一些字符以避免此类问题。

char ch;
....

printf("\nEnter age:\n");
scanf("%d",&detail.age);

while((ch = getchar()) != '\n' && ch != EOF) { }

printf("\nEnter Address:\n");
gets(detail.address);

Another problem is the incorrect use of formatspecifier with printffunction.

另一个问题是格式说明符与printf函数的错误使用。

回答by Amit Behere

    // 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] = '
#include <stdio.h>
#include <conio.h>
#include<string.h>

struct details
{
 char name[30];
 int age;
 char address[500];
 float salary;
};

int main()
{
 struct details detail;
 printf("\nEnter name:\n");
 gets(detail.name);
 printf("\nEnter age:\n");
 scanf("%d",&detail.age);
 printf("\nEnter Address:\n");
scanf("%s",&detail.address);
 printf("\nEnter Salary:\n");
 scanf("%f",&detail.salary);


 printf("\n\n\n");
 printf("Name of the Employee : %s \n",detail.name);
 printf("Age of the Employee : %d \n",detail.age);
 printf("Address of the Employee : %s \n",detail.address);
 printf("Salary of the Employee : %f \n",detail.salary);

 getch();
}
'; 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; } }

回答by Aasiq AK

##代码##