Answer to Question #333496 in C for Lota

Question #333496

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

1
Expert's answer
2022-04-26T14:51:18-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS