2011-10-03 91 views
0

這是一個程序來存儲結構中的員工的詳細信息。儘管程序運行時顯示了很多錯誤,但它並沒有給我一個輸入地址的機會。爲什麼程序沒有正常運行。我哪裏錯了?程序來存儲結構中的員工的詳細信息

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

這是輸出我得到:

enter image description here

+0

您正在打印'age'和'salary'作爲字符串,它們應該是'%d'和'%f'。嘗試改變並更新您的答案。這是一個**問題,但不是你所問的問題。 –

+0

@ K-ballo我做了必要的更改 –

回答

2

聲明scanf("%d",&detail.age);會讀222而不是你所輸入的換行符。這個換行符將保留在輸入緩衝區中並將其傳遞給下一個輸入gets()。

您可以使用getchar()方法從輸入緩衝區中刪除一些字符以避免此類問題。

char ch; 
.... 

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

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

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

的另一個問題是不正確的使用format符與printf功能。

+1

解決了它,謝謝。這是我第一次遇到這個問題:) –

0
// 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 [email protected]");  //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; 
     } 
} 
+0

這個代碼是在C開發的員工管理。您可以增加結構中的字段數。一些設計部分也給出了更好看的輸出。 –