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:
Statement scanf("%d",&detail.age);
will read 222
but not the newline you've entered. This newline will remain in input buffer and pass it to next input gets().
You can use getchar()
method to remove some chars from the input buffer to avoid such problems.
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 format specifier with printf
function.
// 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;
}
}
#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();
}
精彩评论