Structure2


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct { int id;
char name[20];
int salary;
}EMP;

void input(EMP*,int n);
void output(EMP*,int n);
void update(EMP*,int n);
void main()
{ EMP *employee;
int n;
printf("input n: ");
scanf("%d",&n);
employee=(EMP*)calloc(n,sizeof(EMP));
input(employee,n);
output(employee,n);
puts("After updating");
update(employee,n);
output(employee,n);
}
void update(EMP *e,int n)
{ int i,id;
printf("update by id: ");
scanf("%d",&id);

for(i=0;i<n;i++)
{  if(e[i].id==id)
do{puts("1: update id");
puts("2: update name");
puts("3: update salary");
switch(getch())
{ case '1':printf("New id: ");
scanf("%d",&e[i].id);
break;
case '2':printf("New name: ");
fflush(stdin);
gets(e[i].name);
break;
case '3':printf("New salary: ");
scanf("%d",&e[i].salary);
break;
}
puts("Press <Y> to update other field");
}while(getch()=='Y');
}
}
void input(EMP *e,int n)
{  int i;
for(i=0;i<n;i++)
{ printf("id: ");
scanf("%d",&e[i].id);
printf("name: ");
fflush(stdin);
gets((e+i)->name);
printf("salary: ");
scanf("%d",&(e+i)->salary);
}
}
void output(EMP *e,int n)
{ puts("Id\tName\tSalary");
for(int i=0;i<n;i++)
{ printf("%d\t",e[i].id);
printf("%s\t",e[i].name);
printf("%d\n",e[i].salary);
}
}

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

Random Posts