Create a staff/worker information system using C Struct where you’ll be able to –
a ) Insert staff/worker details with id, name and monthlySalary
b ) Display staff/worker details (searched by id)
c ) Update monthlySalary of a staff/worker
#include <stdio.h>
#define STRUCT_SIZE 100
struct staff{
int id;
char name[100];
float salary;
};
void printStaff(struct staff workers[],int i)
{
printf("id:%i name:%s salary:%3.2f\n",workers[i].id,workers[i].name,workers[i].salary);
}
int findStaff(struct staff workers[],int id)
{
for(int i=0;i<STRUCT_SIZE;i++)
{
if (workers[i].id==id) return i;
}
return -1;
}
int main()
{
struct staff workers[STRUCT_SIZE];
int mode = 0;
int cursor = 0;
int id = 0;
int newSalary;
while(1)
{
printf("---\n0<-exit\n1<-new worker\n2<-find worker by id\n3<-set salary\n---\n");
scanf("%i",&mode);
if (mode==0)break;
else if (mode==1)
{
printf("Input id, name, salary\n");
scanf("%i",&workers[cursor].id);
scanf("%s",workers[cursor].name);
scanf("%f",&workers[cursor].salary);
cursor++;
}
else if(mode==2)
{
printf("Enter id\n");
scanf("%i",&id);
if((id=findStaff(workers,id))!=-1)
{
printStaff(workers,id);
}
}
else if(mode==3)
{
printf("Enter id\n");
scanf("%i",&id);
if((id=findStaff(workers,id))!=-1)
{
printStaff(workers,id);
printf("Enter new worker salary\n");
scanf("%i",&newSalary);
workers[id].salary=newSalary;
}
}
}
return 0;
}
Comments
Leave a comment